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

The following examples show how to use de.flapdoodle.embed.mongo.config.MongodConfigBuilder. 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: 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 #2
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 #3
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 #4
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 #5
Source File: EmbeddedMongoDb.java    From render with GNU General Public License v2.0 6 votes vote down vote up
public EmbeddedMongoDb(final String dbName)
        throws IOException {

    this.version = Version.Main.V4_0;
    this.port = 12345;

    // see MongodForTestsFactory for example verbose startup options
    this.mongodExecutable = STARTER.prepare(
            new MongodConfigBuilder()
                    .version(version)
                    .net(new Net(port, Network.localhostIsIPv6()))

                    // use ephemeralForTest storage engine to fix super slow run times on Mac
                    // see https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo/issues/166
                    .cmdOptions(new MongoCmdOptionsBuilder().useStorageEngine("ephemeralForTest").build())

                    .build());

    this.mongodProcess = mongodExecutable.start();

    this.mongoClient = new MongoClient("localhost", port);

    this.db = mongoClient.getDatabase(dbName);
}
 
Example #6
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 #7
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 #8
Source File: MongoLockProviderIntegrationTest.java    From ShedLock with Apache License 2.0 5 votes vote down vote up
@BeforeAll
public static void startMongo() throws IOException {
    mongodExe = starter.prepare(new MongodConfigBuilder()
        .version(Version.Main.V3_6)
        .build());
    mongod = mongodExe.start();

    mongo = MongoClients.create("mongodb://localhost:"+mongod.getConfig().net().getPort());
}
 
Example #9
Source File: ReactiveStreamsMongoLockProviderIntegrationTest.java    From ShedLock with Apache License 2.0 5 votes vote down vote up
@BeforeAll
public static void startMongo() throws IOException {
    mongodExe = starter.prepare(new MongodConfigBuilder()
        .version(Version.Main.V3_6)
        .build());
    mongod = mongodExe.start();

    mongo = MongoClients.create("mongodb://localhost:" + mongod.getConfig().net().getPort());
}
 
Example #10
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 #11
Source File: MongoImplTest.java    From eagle with Apache License 2.0 5 votes vote down vote up
public static void before() {
    try {
        MongodStarter starter = MongodStarter.getDefaultInstance();
        mongodExe = starter.prepare(new MongodConfigBuilder().version(Version.V3_2_1)
            .net(new Net(27017, Network.localhostIsIPv6())).build());
        mongod = mongodExe.start();
    } catch (Exception e) {
        LOG.error("start embed mongod failed, assume some external mongo running. continue run test!", e);
    }
}
 
Example #12
Source File: SimpleEmbedMongo.java    From eagle with Apache License 2.0 5 votes vote down vote up
public void start() throws Exception {
    MongodStarter starter = MongodStarter.getDefaultInstance();
    mongodExe = starter.prepare(new MongodConfigBuilder().version(Version.V3_2_1)
            .net(new Net(27017, Network.localhostIsIPv6())).build());
    mongod = mongodExe.start();

    client = new MongoClient("localhost");
}
 
Example #13
Source File: MongodbLocalServer.java    From hadoop-mini-clusters with Apache License 2.0 5 votes vote down vote up
@Override
public void configure() throws Exception {
    conf = new MongodConfigBuilder()
            .version(Version.Main.PRODUCTION)
            .net(new Net(ip, port, false))
            .build();
}
 
Example #14
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 #15
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 #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: 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 #18
Source File: MongoDBSessionDataStorageTest.java    From pippo with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void setUpClass() throws IOException {
    Integer port = Network.getFreeServerPort();
    MongodStarter starter = MongodStarter.getDefaultInstance();
    mongodExe = starter.prepare(
            new MongodConfigBuilder()
            .version(Version.Main.PRODUCTION)
            .net(new Net(port, Network.localhostIsIPv6()))
            .build());
    mongod = mongodExe.start();
    mongoClient = new MongoClient(HOST, port);
}
 
Example #19
Source File: ErrorLogsCounterManualTest.java    From tutorials with MIT License 5 votes vote down vote up
private MongoTemplate initMongoTemplate() throws IOException {
    mongodExecutable = starter.prepare(new MongodConfigBuilder()
      .version(Version.Main.PRODUCTION)
      .net(new Net(SERVER, PORT, Network.localhostIsIPv6()))
      .build());
    mongoDaemon = mongodExecutable.start();

    MongoClient mongoClient = new MongoClient(SERVER, PORT);
    db = mongoClient.getDatabase(DB_NAME);

    return new MongoTemplate(mongoClient, DB_NAME);
}
 
Example #20
Source File: EmbeddedMongoDBSetup.java    From tutorials with MIT License 5 votes vote down vote up
private void initdb() throws IOException {
    _mongodExe = starter.prepare(new MongodConfigBuilder()
            .version(Version.Main.DEVELOPMENT)
            .net(new Net(MONGODB_HOST, MONGODB_PORT, Network.localhostIsIPv6()))
            .build());
    _mongod = _mongodExe.start();
}
 
Example #21
Source File: MongoDbInit.java    From tutorials with MIT License 5 votes vote down vote up
public static void startMongoDb() throws IOException {
    _mongodExe = starter.prepare(new MongodConfigBuilder()
            .version(Version.Main.DEVELOPMENT)
            .net(new Net("localhost", 27017, Network.localhostIsIPv6()))
            .build());
    _mongod = _mongodExe.start();
}
 
Example #22
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 #23
Source File: MongoDb4TestRule.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Override
public Statement apply(final Statement base, final Description description) {
    return new Statement() {

        @Override
        public void evaluate() throws Throwable {
            final String value = Objects.requireNonNull(System.getProperty(portSystemPropertyName),
                    "System property '" + portSystemPropertyName + "' is null");
            final int port = Integer.parseInt(value);
            mongodExecutable = starter.prepare(
            // @formatter:off
                    new MongodConfigBuilder().version(Version.Main.PRODUCTION)
                            .timeout(new Timeout(BUILDER_TIMEOUT_MILLIS))
                            .net(new Net("localhost", port, Network.localhostIsIPv6())).build());
            // @formatter:on
            mongodProcess = mongodExecutable.start();
            mongoClient = MongoClients.create("mongodb://localhost:" + port);
            try {
                base.evaluate();
            } finally {
                if (mongodProcess != null) {
                    mongodProcess.stop();
                    mongodProcess = null;
                }
                if (mongodExecutable != null) {
                    mongodExecutable.stop();
                    mongodExecutable = null;
                }
            }
        }
    };
}
 
Example #24
Source File: MongoDb3TestRule.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Override
public Statement apply(final Statement base, final Description description) {
    return new Statement() {

        @Override
        public void evaluate() throws Throwable {
            final String value = Objects.requireNonNull(System.getProperty(portSystemPropertyName),
                    "System property '" + portSystemPropertyName + "' is null");
            final int port = Integer.parseInt(value);
            mongodExecutable = starter.prepare(
            // @formatter:off
                    new MongodConfigBuilder()
                        .version(Version.Main.PRODUCTION)
                        .timeout(new Timeout(BUILDER_TIMEOUT_MILLIS))
                        .net(
                                new Net("localhost", port, Network.localhostIsIPv6()))
                        .build());
            // @formatter:on
            mongodProcess = mongodExecutable.start();
            mongoClient = new MongoClient("localhost", port);
            try {
                base.evaluate();
            } finally {
                if (mongodProcess != null) {
                    mongodProcess.stop();
                    mongodProcess = null;
                }
                if (mongodExecutable != null) {
                    mongodExecutable.stop();
                    mongodExecutable = null;
                }
            }
        }
    };
}
 
Example #25
Source File: SingleMongoDBResource.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
protected void before() throws Throwable {
  final IMongodConfig config = new MongodConfigBuilder()
      .version(getVersion())
      .build();

  executable = Environment.prepareMongod(config);
  process = executable.start();

  setPort(config.net().getPort());

  super.before();
}
 
Example #26
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 #27
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();
}
 
Example #28
Source File: MongoTestBase.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@BeforeAll
public static void startMongoDatabase() throws IOException {
    String uri = getConfiguredConnectionString();
    // This switch allow testing against a running mongo database.
    if (uri == null) {
        Version.Main version = Version.Main.V4_0;
        int port = 27018;
        LOGGER.infof("Starting Mongo %s on port %s", version, port);
        IMongodConfig config = new MongodConfigBuilder()
                .version(version)
                .net(new Net(port, Network.localhostIsIPv6()))
                .build();
        MONGO = getMongodExecutable(config);
        try {
            MONGO.start();
        } catch (Exception e) {
            //every so often mongo fails to start on CI runs
            //see if this helps
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ignore) {

            }
            MONGO.start();
        }
    } else {
        LOGGER.infof("Using existing Mongo %s", uri);
    }
}
 
Example #29
Source File: MongoWithReplicasTestBase.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private static IMongodConfig buildMongodConfiguration(String url, int port, final boolean configureReplicaSet)
        throws IOException {
    final MongodConfigBuilder builder = new MongodConfigBuilder()
            .version(Version.Main.V4_0)
            .net(new Net(url, port, Network.localhostIsIPv6()));
    if (configureReplicaSet) {
        builder.withLaunchArgument("--replSet", "test001");
        builder.cmdOptions(new MongoCmdOptionsBuilder()
                .syncDelay(10)
                .useSmallFiles(true)
                .useNoJournal(false)
                .build());
    }
    return builder.build();
}
 
Example #30
Source File: MongoTestBase.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@BeforeAll
public static void startMongoDatabase() throws IOException {
    String uri = getConfiguredConnectionString();
    // This switch allow testing against a running mongo database.
    if (uri == null) {
        Version.Main version = Version.Main.V4_0;
        int port = 27018;
        LOGGER.infof("Starting Mongo %s on port %s", version, port);
        IMongodConfig config = new MongodConfigBuilder()
                .version(version)
                .net(new Net(port, Network.localhostIsIPv6()))
                .build();
        MONGO = getMongodExecutable(config);
        try {
            MONGO.start();
        } catch (Exception e) {
            //every so often mongo fails to start on CI runs
            //see if this helps
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ignore) {
            }
            MONGO.start();
        }
    } else {
        LOGGER.infof("Using existing Mongo %s", uri);
    }
}