Package com.mongodb

Examples of com.mongodb.MongoClient


public class ExportMongoDBUtil {
  private static final Logger logger = LoggerFactory.getLogger(ExportMongoDBUtil.class);

  public void publishMongoDB(JSONArray JSONArray) throws UnknownHostException {
    MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
    DB db = mongoClient.getDB("test");
   
    DBCollection coll = db.getCollection("testCollection");
   
    // removes the existing documents
    coll.drop();
View Full Code Here


                  .toCharArray()));
      }
     
      // Create the MongoClient.
      mongo =
        new MongoClient(
          new ServerAddress(getDatabaseAddress(), getDatabasePort()),
          credentials);
    }
    catch(UnknownHostException e) {
      throw new OmhException("The database could not setup.", e);
View Full Code Here

    private Set<String> collections;

    @Before
    public void connectToDb() throws Exception {
        if (environment.containsKey(dbHostKey)) {
            mongo = new MongoClient(environment.get(dbHostKey));
        } else {
            mongo = new MongoClient("localhost", DbManager.PORT);
        }

        db = mongo.getDB("unittest");
        collections = new HashSet<String>();
    }
View Full Code Here

        loadConfiguration();

        // Create Mongo driver and open the database
        if( username.isEmpty() )
        {
            mongo = new MongoClient( serverAddresses );
        }
        else
        {
            MongoCredential credential = MongoCredential.createMongoCRCredential( username, databaseName, password );
            mongo = new MongoClient( serverAddresses, Arrays.asList( credential ) );
        }
        db = mongo.getDB( databaseName );

        // Create index if needed
        db.requestStart();
View Full Code Here

    @Override
    public Object getObjectInstance(final Object obj,
                                    final Name name,
                                    final Context nameCtx,
                                    final Hashtable<?, ?> environment) throws Exception {
        final MongoClient mongoClient;
        final MongoClientURI clientURI;
        final String uri = (String) environment.get("uri");
        if (uri != null) {
            clientURI = new MongoClientURI(uri);
        } else {
View Full Code Here

        server = new MongoServer(new MemoryBackend());

        // bind on a random local port
        InetSocketAddress serverAddress = server.bind();

        client = new MongoClient(new ServerAddress(serverAddress));
        collection = client.getDB("testdb").getCollection("testcollection");
    }
View Full Code Here

    @Before
    public void setUp() throws Exception {
        MemoryBackend memoryBackend = new MemoryBackend();
        writeableServer = new MongoServer(memoryBackend);
        writeClient = new MongoClient(new ServerAddress(writeableServer.bind()));

        mongoServer = new MongoServer(new ReadOnlyProxy(memoryBackend));
        readOnlyClient = new MongoClient(new ServerAddress(mongoServer.bind()));
    }
View Full Code Here

    }

    @Test(timeout = 10000)
    public void testShutdownNow() {
        MongoServer server = new MongoServer();
        MongoClient client = null;
        InetSocketAddress serverAddress = server.bind();
        client = new MongoClient(new ServerAddress(serverAddress));

        // request something to open a connection
        client.getDB("admin").command("serverStatus").throwOnError();

        server.shutdownNow();
    }
View Full Code Here

    @Test(timeout = 10000)
    public void testShutdownAndRestart() throws Exception {
        MongoServer server = new MongoServer();
        InetSocketAddress serverAddress = server.bind();
        {
            final MongoClient client = new MongoClient(new ServerAddress(serverAddress));

            // request something to open a connection
            client.getDB("admin").command("serverStatus").throwOnError();

            server.shutdownNow();

            try {
                client.getDB("admin").command("serverStatus");
                fail("MongoException expected");
            } catch (MongoException e) {
                // okay
            }

            server.bind(serverAddress);

            client.close();
        }
        {
            // Explicitly reconnect the client.
            // Fails otherwise with mongo-java-driver 2.12.0 unless we would use
            // a Thread.sleep(100) or so.
            final MongoClient client = new MongoClient(new ServerAddress(serverAddress));
            client.getDB("admin").command("serverStatus").throwOnError();
            client.close();
        }
        server.shutdownNow();
    }
View Full Code Here

public class MongoServerTest {

    @Test(timeout = 10000)
    public void testStopListenting() throws Exception {
        MongoServer server = new MongoServer();
        MongoClient client = null;
        try {
            InetSocketAddress serverAddress = server.bind();
            client = new MongoClient(new ServerAddress(serverAddress));
            // request something
            client.getDB("admin").command("serverStatus").throwOnError();

            server.stopListenting();

            // existing clients must still work
            client.getDB("admin").command("serverStatus").throwOnError();

            // new clients must fail
            client.close();
            Socket socket = new Socket();
            try {
                socket.connect(serverAddress);
                fail("IOException expected");
            } catch (IOException e) {
                // expected
            } finally {
                socket.close();
            }

        } finally {
            if (client != null) {
                client.close();
            }
            server.shutdownNow();
        }
    }
View Full Code Here

TOP

Related Classes of com.mongodb.MongoClient

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.