@BeforeClass public static void startMongo() throws Exception { IMongodConfig config = new MongodConfigBuilder() .version(Version.Main.PRODUCTION) .net(new Net(27018, Network.localhostIsIPv6())) .build(); mongoDb = MongodStarter.getDefaultInstance().prepare(config); mongoDb.start(); }
@BeforeClass public static void startMongoDB() throws UnknownHostException, IOException { MongodStarter starter = MongodStarter.getDefaultInstance(); IMongodConfig mongodConfig = new MongodConfigBuilder() .version(Version.Main.PRODUCTION) .net(new Net(PORT, Network.localhostIsIPv6())) .build(); mongodExecutable = null; try { mongodExecutable = starter.prepare(mongodConfig); MongodProcess mongod = mongodExecutable.start(); } catch (Exception e) { if (mongodExecutable != null) mongodExecutable.stop(); } }
@BeforeClass public static void initializeDB() throws IOException { /* * IStreamProcessor stream = new NullProcessor(); MongodStarter runtime * = MongodStarter.getInstance(new RuntimeConfigBuilder() * .defaults(Command.MongoD) .processOutput(new ProcessOutput(stream, * stream, stream)) .artifactStore(new ArtifactStoreBuilder() * .defaults(Command.MongoD) .executableNaming(new UserTempNaming()) * .build()) .build()); mongodExecutable = runtime.prepare(new * MongodConfigBuilder() .version(Version.Main.PRODUCTION) .net(new * Net(MONGO_TEST_PORT, Network.localhostIsIPv6())) .build()); */ MongodStarter runtime = MongodStarter.getDefaultInstance(); MongodConfig mongodConfig = new MongodConfig(Version.V2_4_1, 27017, Network.localhostIsIPv6()); mongodExecutable = runtime.prepare(mongodConfig); mongodProcess = mongodExecutable.start(); }
public void start() { if (!this.active) { try { this.mongodProcess = MongodStarter.getDefaultInstance() .prepare( new MongodConfigBuilder() .version(Version.Main.V3_0) .net(new Net(this.mongoHost, this.mongoPort, this.mongoIPv6)) .build()) .start(); LoggerFactory.getLogger(EmbeddedMongo.class) .info("Successfully started EmbeddedMongoDB @ {}:{}", this.mongoHost, this.mongoPort); this.active = true; } catch (IOException e) { LoggerFactory.getLogger(EmbeddedMongo.class).error("Failed to start EmbeddedMongoDB", e); } } }
// ### Usage public void testStandard() throws UnknownHostException, IOException { int port = 12345; MongodConfig mongodConfig = new MongodConfig(Version.Main.V2_0, port, Network.localhostIsIPv6()); MongodStarter runtime = MongodStarter.getDefaultInstance(); MongodExecutable mongodExecutable = null; try { mongodExecutable = runtime.prepare(mongodConfig); MongodProcess mongod = mongodExecutable.start(); Mongo mongo = new Mongo("localhost", port); DB db = mongo.getDB("test"); DBCollection col = db.createCollection("testCol", new BasicDBObject()); col.save(new BasicDBObject("testDoc", new Date())); } finally { if (mongodExecutable != null) mongodExecutable.stop(); } }
// ### ... automagic public void testFreeServerPortAuto() throws UnknownHostException, IOException { MongodConfig mongodConfig = new MongodConfig(Version.Main.V2_0); MongodStarter runtime = MongodStarter.getDefaultInstance(); MongodExecutable mongodExecutable = null; try { mongodExecutable = runtime.prepare(mongodConfig); MongodProcess mongod = mongodExecutable.start(); Mongo mongo = new Mongo( new ServerAddress( mongodConfig.net().getServerAddress(), mongodConfig.net().getPort())); DB db = mongo.getDB("test"); DBCollection col = db.createCollection("testCol", new BasicDBObject()); col.save(new BasicDBObject("testDoc", new Date())); } finally { if (mongodExecutable != null) mongodExecutable.stop(); } }
public static void main(String[] args) { try { int port = 12345; MongodProcess mongod = null; MongodConfig mongodConfig = new MongodConfig(Version.Main.V2_2, port, Network.localhostIsIPv6()); MongodStarter runtime = MongodStarter.getDefaultInstance(); MongodExecutable mongodExecutable = runtime.prepare(mongodConfig); mongod = mongodExecutable.start(); Mongo mongo = new Mongo("localhost", port); DB db = mongo.getDB("test"); DBCollection col = db.createCollection("testCol", new BasicDBObject()); col.save(new BasicDBObject("testDoc", new Date())); Thread.sleep(5000); mongod.stop(); } catch (Exception e) { } }
public static void init() throws Exception { ezConfiguration = new EzConfiguration(new ClasspathConfigurationLoader()).getProperties(); ezConfiguration.setProperty(EzBakePropertyConstants.MONGODB_HOST_NAME, "localhost"); ezConfiguration.setProperty(EzBakePropertyConstants.MONGODB_PORT, Integer.toString(port)); MongodStarter starter = MongodStarter.getDefaultInstance(); IMongodConfig mongodConfig = new MongodConfigBuilder() .version(Version.Main.PRODUCTION) .net(new Net(port, Network.localhostIsIPv6())) .build(); mongod = starter.prepare(mongodConfig).start(); MongoConfigurationHelper mc = new MongoConfigurationHelper( ezConfiguration, new SystemConfigurationHelper(ezConfiguration).getTextCryptoProvider()); MongoClient mongoClient = new MongoClient("localhost", port); DB db = mongoClient.getDB(mc.getMongoDBDatabaseName()); db.addUser(mc.getMongoDBUserName(), mc.getMongoDBPassword().toCharArray()); }
private void initMongoInstances() throws Exception { logger.debug("*** initMongoInstances ***"); CommandResult cr; // Create 3 mongod processes mongodConfig1 = new MongodConfig( new GenericVersion(mongoVersion), new Net(mongoPort1, Network.localhostIsIPv6()), new Storage(null, REPLICA_SET_NAME, 20), new Timeout()); MongodStarter starter = MongodStarter.getDefaultInstance(); mongodExe1 = starter.prepare(mongodConfig1); mongod1 = mongodExe1.start(); mongodConfig2 = new MongodConfig( new GenericVersion(mongoVersion), new Net(mongoPort2, Network.localhostIsIPv6()), new Storage(null, REPLICA_SET_NAME, 20), new Timeout()); mongodExe2 = starter.prepare(mongodConfig2); mongod2 = mongodExe2.start(); mongodConfig3 = new MongodConfig( new GenericVersion(mongoVersion), new Net(mongoPort3, Network.localhostIsIPv6()), new Storage(null, REPLICA_SET_NAME, 20), new Timeout()); mongodExe3 = starter.prepare(mongodConfig3); mongod3 = mongodExe3.start(); String server1 = Network.getLocalHost().getHostName() + ":" + mongodConfig1.net().getPort(); String server2 = Network.getLocalHost().getHostName() + ":" + mongodConfig2.net().getPort(); String server3 = Network.getLocalHost().getHostName() + ":" + mongodConfig3.net().getPort(); logger.debug("Server #1: {}", server1); logger.debug("Server #2: {}", server2); logger.debug("Server #3: {}", server3); Thread.sleep(2000); MongoClientOptions mco = MongoClientOptions.builder() .autoConnectRetry(true) .connectTimeout(15000) .socketTimeout(60000) .build(); mongo = new MongoClient( new ServerAddress(Network.getLocalHost().getHostName(), mongodConfig1.net().getPort()), mco); mongoAdminDB = mongo.getDB(ADMIN_DATABASE_NAME); cr = mongoAdminDB.command(new BasicDBObject("isMaster", 1)); logger.debug("isMaster: " + cr); // Initialize replica set cr = mongoAdminDB.command( new BasicDBObject( "replSetInitiate", (DBObject) JSON.parse( "{'_id': '" + REPLICA_SET_NAME + "', 'members': [{'_id': 0, 'host': '" + server1 + "'}, {'_id': 1, 'host': '" + server2 + "'}, {'_id': 2, 'host': '" + server3 + "', 'arbiterOnly' : true}]} }"))); logger.debug("replSetInitiate: " + cr); Thread.sleep(5000); cr = mongoAdminDB.command(new BasicDBObject("replSetGetStatus", 1)); logger.info("replSetGetStatus: " + cr); // Check replica set status before to proceed while (!isReplicaSetStarted(cr)) { logger.debug("Waiting for 3 seconds..."); Thread.sleep(3000); cr = mongoAdminDB.command(new BasicDBObject("replSetGetStatus", 1)); logger.debug("replSetGetStatus: " + cr); } mongo.close(); mongo = null; // Initialize a new client using all instances. List<ServerAddress> mongoServers = new ArrayList<ServerAddress>(); mongoServers.add( new ServerAddress(Network.getLocalHost().getHostName(), mongodConfig1.net().getPort())); mongoServers.add( new ServerAddress(Network.getLocalHost().getHostName(), mongodConfig2.net().getPort())); mongoServers.add( new ServerAddress(Network.getLocalHost().getHostName(), mongodConfig3.net().getPort())); mongo = new MongoClient(mongoServers, mco); Assert.assertNotNull(mongo); mongo.setReadPreference(ReadPreference.secondaryPreferred()); mongo.setWriteConcern(WriteConcern.REPLICAS_SAFE); }