de.flapdoodle.embed.mongo.config.IMongodConfig Java Examples

The following examples show how to use de.flapdoodle.embed.mongo.config.IMongodConfig. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MongoNotebookRepoTest.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws IOException {
  String bindIp = "localhost";
  ServerSocket socket = new ServerSocket(0);
  int port = socket.getLocalPort();
  socket.close();

  IMongodConfig mongodConfig = new MongodConfigBuilder()
      .version(Version.Main.PRODUCTION)
      .net(new Net(bindIp, port, Network.localhostIsIPv6()))
      .build();

  mongodExecutable = MongodStarter.getDefaultInstance()
      .prepare(mongodConfig);
  mongodExecutable.start();

  System.setProperty(ZEPPELIN_NOTEBOOK_MONGO_URI.getVarName(), "mongodb://" + bindIp + ":" + port);
  zConf = new ZeppelinConfiguration();
  notebookRepo = new MongoNotebookRepo();
  notebookRepo.init(zConf);
}
 
Example #2
Source File: EmbeddedClient.java    From graviteeio-access-management with Apache License 2.0 6 votes vote down vote up
@Override
public void afterPropertiesSet() throws Exception {

    final IMongodConfig mongodConfig = new MongodConfigBuilder()
            .version(Version.Main.PRODUCTION).build();

    IRuntimeConfig runtimeConfig = new RuntimeConfigBuilder()
            .defaultsWithLogger(Command.MongoD, logger)
            .processOutput(ProcessOutput.getDefaultInstanceSilent())
            .build();

    MongodStarter runtime = MongodStarter.getInstance(runtimeConfig);

    MongodExecutable mongodExecutable = runtime.prepare(mongodConfig);
    mongod = mongodExecutable.start();

    // cluster configuration
    ClusterSettings clusterSettings = ClusterSettings.builder().hosts(Collections.singletonList(new ServerAddress(mongodConfig.net().getServerAddress().getHostName(), mongodConfig.net().getPort()))).build();
    // codec configuration
    CodecRegistry pojoCodecRegistry = fromRegistries(MongoClients.getDefaultCodecRegistry(),
            fromProviders(PojoCodecProvider.builder().automatic(true).build()));

    MongoClientSettings settings = MongoClientSettings.builder().clusterSettings(clusterSettings).codecRegistry(pojoCodecRegistry).writeConcern(WriteConcern.ACKNOWLEDGED).build();
    mongoClient = MongoClients.create(settings);
    mongoDatabase = mongoClient.getDatabase(databaseName);
}
 
Example #3
Source File: EmbeddedClient.java    From graviteeio-access-management with Apache License 2.0 6 votes vote down vote up
@Override
public void afterPropertiesSet() throws Exception {
    final IMongodConfig mongodConfig = new MongodConfigBuilder()
            .version(Version.Main.PRODUCTION).build();

    IRuntimeConfig runtimeConfig = new RuntimeConfigBuilder()
            .defaultsWithLogger(Command.MongoD, logger)
            .processOutput(ProcessOutput.getDefaultInstanceSilent())
            .build();

    MongodStarter runtime = MongodStarter.getInstance(runtimeConfig);

    MongodExecutable mongodExecutable = runtime.prepare(mongodConfig);
    mongod = mongodExecutable.start();

    // cluster configuration
    ClusterSettings clusterSettings = ClusterSettings.builder().hosts(Collections.singletonList(new ServerAddress(mongodConfig.net().getServerAddress().getHostName(), mongodConfig.net().getPort()))).build();
    // codec configuration
    CodecRegistry pojoCodecRegistry = fromRegistries(MongoClients.getDefaultCodecRegistry(),
            fromProviders(PojoCodecProvider.builder().automatic(true).build()));

    MongoClientSettings settings = MongoClientSettings.builder().clusterSettings(clusterSettings).codecRegistry(pojoCodecRegistry).writeConcern(WriteConcern.ACKNOWLEDGED).build();
    mongoClient = MongoClients.create(settings);
    mongoDatabase = mongoClient.getDatabase(databaseName);
}
 
Example #4
Source File: MongodManager.java    From jpa-unit with Apache License 2.0 6 votes vote down vote up
private void startMongod(final MongodConfiguration config) {

        List<IMongodConfig> mongodConfigList;
        try {
            if (config.getHosts().size() == 1) {
                mongodConfigList = buildMongodConfiguration(config.getHosts(), false);
                startMongo(mongodConfigList);
            } else if (config.getHosts().size() >= 3) {
                mongodConfigList = buildMongodConfiguration(config.getHosts(), true);
                startMongo(mongodConfigList);
                initializeReplicaSet(mongodConfigList);
            } else {
                throw new RuntimeException("It looks like replica set is configured. A replica set however requires at least 3 hosts");
            }

        } catch (final IOException | InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
 
Example #5
Source File: MongoDbReadWriteIT.java    From beam with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setUp() throws Exception {
  int port = NetworkTestHelper.getAvailableLocalPort();
  LOG.info("Starting MongoDB embedded instance on {}", port);
  IMongodConfig mongodConfig =
      new MongodConfigBuilder()
          .version(Version.Main.PRODUCTION)
          .configServer(false)
          .replication(new Storage(MONGODB_LOCATION.getRoot().getPath(), null, 0))
          .net(new Net(hostname, port, Network.localhostIsIPv6()))
          .cmdOptions(
              new MongoCmdOptionsBuilder()
                  .syncDelay(10)
                  .useNoPrealloc(true)
                  .useSmallFiles(true)
                  .useNoJournal(true)
                  .verbose(false)
                  .build())
          .build();
  mongodExecutable = mongodStarter.prepare(mongodConfig);
  mongodProcess = mongodExecutable.start();
  client = new MongoClient(hostname, port);

  mongoSqlUrl = String.format("mongodb://%s:%d/%s/%s", hostname, port, database, collection);
}
 
Example #6
Source File: EmbeddedMongoHelper.java    From edison-microservice with Apache License 2.0 6 votes vote down vote up
public static void startMongoDB() throws IOException {
    if (!started.compareAndSet(false, true)) {
        throw new RuntimeException("Embedded mongo already running, call stopMongoDB before starting it again!");
    }
    final String bindIp = "localhost";
    try {
        final int port = Network.getFreeServerPort();
        final IMongodConfig mongodConfig = new MongodConfigBuilder()
                .version(Version.Main.PRODUCTION)
                .net(new Net(bindIp, port, Network.localhostIsIPv6()))
                .build();
        final MongodStarter runtime = MongodStarter.getInstance(new RuntimeConfigBuilder()
                .defaultsWithLogger(Command.MongoD, LOG)
                .build());
        mongodExecutable = runtime.prepare(mongodConfig, Distribution.detectFor(Version.Main.PRODUCTION));
        mongodProcess = mongodExecutable.start();
        mongoClient = new MongoClient(bindIp, port);
    } catch (final IOException e) {
        stopMongoDB();
        throw e;
    }
}
 
Example #7
Source File: MongoBaseTest.java    From vertx-auth with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void startMongo() throws Exception {
  String uri = getConnectionString();
  if (uri == null ) {
    Version.Main version = Version.Main.V3_4;
    int port = 27018;
    System.out.println("Starting Mongo " + version + " on port " + port);
    IMongodConfig config = new MongodConfigBuilder().
      version(version).
      net(new Net(port, Network.localhostIsIPv6())).
      build();
    exe = MongodStarter.getDefaultInstance().prepare(config);
    exe.start();
  } else {
    System.out.println("Using existing Mongo " + uri);
  }
}
 
Example #8
Source File: MyFirstVerticleTest.java    From df_data_service with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void initialize() throws IOException {
  MongodStarter starter = MongodStarter.getDefaultInstance();

  IMongodConfig mongodConfig = new MongodConfigBuilder()
      .version(Version.Main.PRODUCTION)
      .net(new Net(MONGO_PORT, Network.localhostIsIPv6()))
      .build();

  MongodExecutable mongodExecutable = starter.prepare(mongodConfig);
  MONGO = mongodExecutable.start();
}
 
Example #9
Source File: ManualEmbeddedMongoDbIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@BeforeEach
void setup() throws Exception {
    String ip = "localhost";
    int randomPort = SocketUtils.findAvailableTcpPort();

    IMongodConfig mongodConfig = new MongodConfigBuilder().version(Version.Main.PRODUCTION)
        .net(new Net(ip, randomPort, Network.localhostIsIPv6()))
        .build();

    MongodStarter starter = MongodStarter.getDefaultInstance();
    mongodExecutable = starter.prepare(mongodConfig);
    mongodExecutable.start();
    mongoTemplate = new MongoTemplate(new MongoClient(ip, randomPort), "test");
}
 
Example #10
Source File: MongoDBITBase.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
public void startDB() throws Exception {
    MongodStarter starter = MongodStarter.getDefaultInstance();

    String bindIp = "localhost";
    int port = 27018;

    IMongodConfig mongodConfig = new MongodConfigBuilder()
            .version(Version.Main.PRODUCTION)
            .net(new Net(bindIp, port, Network.localhostIsIPv6()))
            .build();

    MongodExecutable mongodExecutable = null;

    mongodExecutable = starter.prepare(mongodConfig);

    //give time for previous DB close to finish and port to be released"
    Thread.sleep(200L);
    mongod = mongodExecutable.start();
    setClient();
}
 
Example #11
Source File: MongodManager.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
private void startMongo(final List<IMongodConfig> mongodConfigList) throws IOException {
    // @formatter:off
    final ProcessOutput processOutput = new ProcessOutput(
            logTo(LOGGER, Slf4jLevel.INFO),
            logTo(LOGGER, Slf4jLevel.ERROR),
            named("[console>]", logTo(LOGGER, Slf4jLevel.DEBUG)));

    final IRuntimeConfig runtimeConfig = new RuntimeConfigBuilder()
            .defaultsWithLogger(Command.MongoD,LOGGER)
            .processOutput(processOutput)
            .artifactStore(new ExtractedArtifactStoreBuilder()
                .defaults(Command.MongoD)
                .download(new DownloadConfigBuilder()
                    .defaultsForCommand(Command.MongoD)
                    .progressListener(new Slf4jProgressListener(LOGGER))
                    .build()))
            .build();
    // @formatter:on
    final MongodStarter starter = MongodStarter.getInstance(runtimeConfig);

    for (final IMongodConfig mongodConfig : mongodConfigList) {
        final MongodExecutable mongodExecutable = starter.prepare(mongodConfig);
        final MongodProcess mongod = mongodExecutable.start();

        mongoProcesses.put(mongod, mongodExecutable);
    }
}
 
Example #12
Source File: MongodManager.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
private void initializeReplicaSet(final List<IMongodConfig> mongodConfigList) throws UnknownHostException, InterruptedException {
    Thread.sleep(1000);
    final MongoClientOptions mo = MongoClientOptions.builder().connectTimeout(10).build();
    final ServerAddress arbitrerAddress = new ServerAddress(mongodConfigList.get(0).net().getServerAddress().getHostName(),
            mongodConfigList.get(0).net().getPort());

    try (MongoClient mongo = new MongoClient(arbitrerAddress, mo)) {
        final MongoDatabase mongoAdminDB = mongo.getDatabase("admin");

        Document cr = mongoAdminDB.runCommand(new Document("isMaster", 1));
        LOGGER.info("isMaster: {}", cr);

        // Build replica set configuration settings
        final Document rsConfiguration = buildReplicaSetConfiguration(mongodConfigList);
        LOGGER.info("replSetSettings: {}", rsConfiguration);

        // Initialize replica set
        cr = mongoAdminDB.runCommand(new Document("replSetInitiate", rsConfiguration));
        LOGGER.info("replSetInitiate: {}", cr);

        // Check replica set status before to proceed
        int maxWaitRounds = 10;
        do {
            LOGGER.info("Waiting for 1 second...");
            Thread.sleep(1000);
            cr = mongoAdminDB.runCommand(new Document("replSetGetStatus", 1));
            LOGGER.info("replSetGetStatus: {}", cr);
            maxWaitRounds--;
        } while (!isReplicaSetStarted(cr) || maxWaitRounds != 0);

        if (!isReplicaSetStarted(cr) && maxWaitRounds == 0) {
            throw new RuntimeException("Could not initialize replica set");
        }
    }
}
 
Example #13
Source File: MongodManager.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
private Document buildReplicaSetConfiguration(final List<IMongodConfig> configList) throws UnknownHostException {
    final Document replicaSetSetting = new Document();
    replicaSetSetting.append("_id", "test001");

    final List<Document> members = new ArrayList<>();
    int i = 0;
    for (final IMongodConfig mongoConfig : configList) {
        members.add(new Document().append("_id", i++).append("host",
                mongoConfig.net().getServerAddress().getHostName() + ":" + mongoConfig.net().getPort()));
    }

    replicaSetSetting.append("members", members);
    return replicaSetSetting;
}
 
Example #14
Source File: MongodManager.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
private List<IMongodConfig> buildMongodConfiguration(final List<HostAndPort> hosts, final boolean configureReplicaSet)
        throws IOException {
    final List<IMongodConfig> configs = new ArrayList<>(hosts.size());
    for (final HostAndPort hostAndPort : hosts) {
        configs.add(buildMongodConfiguration(hostAndPort, configureReplicaSet));
    }
    return configs;
}
 
Example #15
Source File: MongodManager.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
private IMongodConfig buildMongodConfiguration(final HostAndPort hostAndPort, final boolean configureReplicaSet) throws IOException {
    final InetAddress address = InetAddress.getByName(hostAndPort.getHost());

    // @formatter:off
    final MongodConfigBuilder builder = new MongodConfigBuilder()
            .version(Version.Main.PRODUCTION)
            .net(new Net(hostAndPort.getHost(), hostAndPort.getPort(), address instanceof Inet6Address));
    // @formatter:on

    if (configureReplicaSet) {
        builder.replication(new Storage(null, "test001", 0));
    }
    return builder.build();
}
 
Example #16
Source File: MongoTestBase.java    From vertx-mongo-client with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void startMongo() throws Exception {
  String uri = getConnectionString();
  if (uri == null ) {
    Version.Main version = Version.Main.V3_4;
    int port = 27018;
    System.out.println("Starting Mongo " + version + " on port " + port);
    IMongodConfig config = new MongodConfigBuilder().
      version(version).
      net(new Net(port, Network.localhostIsIPv6())).
      build();
    exe = MongodStarter.getDefaultInstance().prepare(config);
    exe.start();
  } else {
    System.out.println("Using existing Mongo " + uri);
  }
}
 
Example #17
Source File: MongoDbIOTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void beforeClass() throws Exception {
  port = NetworkTestHelper.getAvailableLocalPort();
  LOG.info("Starting MongoDB embedded instance on {}", port);
  IMongodConfig mongodConfig =
      new MongodConfigBuilder()
          .version(Version.Main.PRODUCTION)
          .configServer(false)
          .replication(new Storage(MONGODB_LOCATION.getRoot().getPath(), null, 0))
          .net(new Net("localhost", port, Network.localhostIsIPv6()))
          .cmdOptions(
              new MongoCmdOptionsBuilder()
                  .syncDelay(10)
                  .useNoPrealloc(true)
                  .useSmallFiles(true)
                  .useNoJournal(true)
                  .verbose(false)
                  .build())
          .build();
  mongodExecutable = mongodStarter.prepare(mongodConfig);
  mongodProcess = mongodExecutable.start();
  client = new MongoClient("localhost", port);

  LOG.info("Insert test data");
  List<Document> documents = createDocuments(1000);
  MongoCollection<Document> collection = getCollection(COLLECTION);
  collection.insertMany(documents);
}
 
Example #18
Source File: MongosSystemForTestFactory.java    From spring-data-examples with Apache License 2.0 5 votes vote down vote up
public void start() throws Throwable {
	this.mongodProcessList = new ArrayList<>();
	this.mongodConfigProcessList = new ArrayList<>();
	for (Entry<String, List<IMongodConfig>> entry : replicaSets.entrySet()) {
		initializeReplicaSet(entry);
	}
	for (IMongodConfig config : configServers) {
		initializeConfigServer(config);
	}
	initializeMongos();
	configureMongos();
}
 
Example #19
Source File: ExampleMongoConfiguration.java    From edison-microservice with Apache License 2.0 5 votes vote down vote up
@Bean
public MongoClient mongoClient(final MongoProperties mongoProperties) throws IOException {
    String bindIp = mongoProperties.getHost()[0];
    int port = Network.getFreeServerPort();
    IMongodConfig mongodConfig = new MongodConfigBuilder()
            .version(Version.Main.PRODUCTION)
            .net(new Net(bindIp, port, Network.localhostIsIPv6()))
            .build();
    final MongodStarter runtime = MongodStarter.getInstance(new RuntimeConfigBuilder()
            .defaultsWithLogger(Command.MongoD, LOG)
            .build());
    MongodExecutable mongodExecutable = runtime.prepare(mongodConfig, Distribution.detectFor(Version.Main.PRODUCTION));
    mongodExecutable.start();
    return new MongoClient(bindIp, port);
}
 
Example #20
Source File: MongosSystemForTestFactory.java    From spring-data-examples with Apache License 2.0 5 votes vote down vote up
private void initializeConfigServer(IMongodConfig config) throws Exception {
	if (!config.isConfigServer()) {
		throw new Exception(
				"Mongo configuration is not a defined for a config server.");
	}
	MongodStarter starter = MongodStarter.getDefaultInstance();
	MongodExecutable mongodExe = starter.prepare(config);
	MongodProcess process = mongodExe.start();
	mongodProcessList.add(process);
}
 
Example #21
Source File: Helpers.java    From entref-spring-boot with MIT License 5 votes vote down vote up
/**
 * Setup a mongo instance
 * @param net the net instance to bind to
 * @return the mongo instance
 * @throws IOException thrown when unable to bind
 */
static MongodExecutable SetupMongo(Net net) throws IOException {
    MongodStarter starter = MongodStarter.getDefaultInstance();

    IMongodConfig mongodConfig = new MongodConfigBuilder()
            .version(Version.Main.DEVELOPMENT)
            .net(net)
            .build();

    MongodExecutable mongoProc = starter.prepare(mongodConfig);
    mongoProc.start();

    return mongoProc;
}
 
Example #22
Source File: MyFirstVerticleTest.java    From my-vertx-first-app with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void initialize() throws IOException {
  MongodStarter starter = MongodStarter.getDefaultInstance();

  IMongodConfig mongodConfig = new MongodConfigBuilder()
      .version(Version.Main.PRODUCTION)
      .net(new Net(MONGO_PORT, Network.localhostIsIPv6()))
      .build();

  MongodExecutable mongodExecutable = starter.prepare(mongodConfig);
  MONGO = mongodExecutable.start();
}
 
Example #23
Source File: EmbeddedMongo.java    From spring-data-examples with Apache License 2.0 5 votes vote down vote up
private void doStart() {

			Map<String, List<IMongodConfig>> replicaSets = new LinkedHashMap<>();
			replicaSets.put(configServerReplicaSetName, initConfigServers());
			replicaSets.put(replicaSetName, initReplicaSet());

			// create mongos
			IMongosConfig mongosConfig = defaultMongosConfig(serverVersion, mongosPort, defaultCommandOptions(),
					configServerReplicaSetName, configServerPorts[0]);

			mongosTestFactory = new MongosSystemForTestFactory(mongosConfig, replicaSets, Collections.emptyList(),
					DEFAULT_SHARDING, DEFAULT_SHARDING, DEFAULT_SHARD_KEY, outputFunction);

			try {
				LOGGER.info(String.format("Starting config servers at ports %s",
						StringUtils.arrayToCommaDelimitedString(configServerPorts)));
				LOGGER.info(String.format("Starting replica set '%s' servers at ports %s", replicaSetName,
						StringUtils.arrayToCommaDelimitedString(serverPorts)));

				mongosTestFactory.start();

				LOGGER
						.info(String.format("Replica set '%s' started. Connection String: %s", replicaSetName, connectionString()));
			} catch (Throwable e) {
				throw new RuntimeException(" Error while starting cluster. ", e);
			}
		}
 
Example #24
Source File: EmbeddedMongo.java    From spring-data-examples with Apache License 2.0 5 votes vote down vote up
private List<IMongodConfig> initReplicaSet() {

			List<IMongodConfig> rs = new ArrayList<>();

			for (int port : serverPorts) {
				rs.add(defaultMongodConfig(serverVersion, port, defaultCommandOptions(), false, true, replicaSetName));
			}
			return rs;
		}
 
Example #25
Source File: EmbeddedMongo.java    From spring-data-examples with Apache License 2.0 5 votes vote down vote up
private List<IMongodConfig> initConfigServers() {

			List<IMongodConfig> configServers = new ArrayList<>(configServerPorts.length);

			for (Integer port : configServerPorts) {
				configServers.add(
						defaultMongodConfig(serverVersion, port, defaultCommandOptions(), true, false, configServerReplicaSetName));
			}
			return configServers;
		}
 
Example #26
Source File: EmbeddedMongo.java    From spring-data-examples with Apache License 2.0 5 votes vote down vote up
/**
 * Create a default {@code mongod} config.
 *
 * @param version
 * @param port
 * @param cmdOptions
 * @param configServer
 * @param shardServer
 * @param replicaSet
 * @return
 */
private static IMongodConfig defaultMongodConfig(IFeatureAwareVersion version, int port, IMongoCmdOptions cmdOptions,
		boolean configServer, boolean shardServer, String replicaSet) {

	try {

		MongodConfigBuilder builder = new MongodConfigBuilder() //
				.version(version) //
				.withLaunchArgument("--quiet") //
				.net(new Net(LOCALHOST, port, Network.localhostIsIPv6())) //
				.configServer(configServer).cmdOptions(cmdOptions); //

		if (StringUtils.hasText(replicaSet)) {

			builder = builder //
					.replication(new Storage(null, replicaSet, 0));

			if (!configServer) {
				builder = builder.shardServer(shardServer);
			} else {
				builder = builder.shardServer(false);
			}
		}

		return builder.build();
	} catch (IOException e) {
		throw new RuntimeException(e);
	}
}
 
Example #27
Source File: MongosSystemForTestFactory.java    From spring-data-examples with Apache License 2.0 5 votes vote down vote up
public MongosSystemForTestFactory(IMongosConfig config,
								  Map<String, List<IMongodConfig>> replicaSets,
								  List<IMongodConfig> configServers, String shardDatabase,
								  String shardCollection, String shardKey, Function<Command, ProcessOutput> outputFunction) {
	this.config = config;
	this.replicaSets = replicaSets;
	this.configServers = configServers;
	this.shardDatabase = shardDatabase;
	this.shardCollection = shardCollection;
	this.shardKey = shardKey;
	this.outputFunction = outputFunction;
}
 
Example #28
Source File: MongoWithReplicasTestBase.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private static void initializeReplicaSet(final List<IMongodConfig> mongodConfigList) throws UnknownHostException {
    final String arbitrerAddress = "mongodb://" + mongodConfigList.get(0).net().getServerAddress().getHostName() + ":"
            + mongodConfigList.get(0).net().getPort();
    final MongoClientSettings mo = MongoClientSettings.builder()
            .applyConnectionString(new ConnectionString(arbitrerAddress)).build();

    try (MongoClient mongo = MongoClients.create(mo)) {
        final MongoDatabase mongoAdminDB = mongo.getDatabase("admin");

        Document cr = mongoAdminDB.runCommand(new Document("isMaster", 1));
        LOGGER.infof("isMaster: %s", cr);

        // Build replica set configuration settings
        final Document rsConfiguration = buildReplicaSetConfiguration(mongodConfigList);
        LOGGER.infof("replSetSettings: %s", rsConfiguration);

        // Initialize replica set
        cr = mongoAdminDB.runCommand(new Document("replSetInitiate", rsConfiguration));
        LOGGER.infof("replSetInitiate: %s", cr);

        // Check replica set status before to proceed
        await()
                .pollInterval(100, TimeUnit.MILLISECONDS)
                .atMost(1, TimeUnit.MINUTES)
                .until(() -> {
                    Document result = mongoAdminDB.runCommand(new Document("replSetGetStatus", 1));
                    LOGGER.infof("replSetGetStatus: %s", result);
                    return !isReplicaSetStarted(result);
                });
    }
}
 
Example #29
Source File: DevelopmentConfig.java    From entref-spring-boot with MIT License 5 votes vote down vote up
/**
 * Attempts to start a mongo instance, using embedMongo
 * @param bind the net info to bind to
 * @return the instance
 * @throws IOException indicates a failure
 */
private MongodExecutable setupMongoEmbed(Net bind) throws IOException {
    MongodStarter starter;
    starter = MongodStarter.getDefaultInstance();

    IMongodConfig mongodConfig = new MongodConfigBuilder()
            .version(Version.Main.DEVELOPMENT)
            .net(bind)
            .build();

    MongodExecutable mongodExecutable = starter.prepare(mongodConfig);
    MongodProcess mongod = mongodExecutable.start();
    return mongodExecutable;
}
 
Example #30
Source File: WithEmbeddedMongo.java    From Hands-On-Reactive-Programming-in-Spring-5 with MIT License 5 votes vote down vote up
@BeforeClass
static void setUpMongo() throws IOException {
    MongodStarter starter = MongodStarter.getDefaultInstance();

    String bindIp = "localhost";
    int port = 27017;
    IMongodConfig mongodConfig = new MongodConfigBuilder()
            .version(Version.Main.DEVELOPMENT)
            .net(new Net(bindIp, port, Network.localhostIsIPv6()))
            .build();

    MongodExecutable mongodExecutable = starter.prepare(mongodConfig);
    MONGO_HOLDER.set(mongodExecutable);
    mongodExecutable.start();
}