Package com.mongodb

Examples of com.mongodb.ServerAddress


 
  @Override
  public void connect(String name, String host, int port) throws VException {
    Mongo mongo;
    try {
      mongo = new Mongo(new ServerAddress(host, port));
    } catch (UnknownHostException e) {
      throw new VException("Unknown host", e);
    }
    connectInternal(name, mongo);
  }
View Full Code Here


                        String password = config.get("password");

                        MongoClientOptions clientOptions = getClientOptions();
                        if (user != null && password != null) {
                            MongoCredential credential = MongoCredential.createMongoCRCredential(user, dbName, password.toCharArray());
                            client = new MongoClient(new ServerAddress(host, port), Collections.singletonList(credential), clientOptions);
                        } else {
                            client = new MongoClient(new ServerAddress(host, port), clientOptions);
                        }

                        this.db = client.getDB(dbName);

                        String databaseSchema = config.get("databaseSchema");
View Full Code Here

     */
    public Mongo createMongo() {
        Mongo mongo;
        if (mongoAddresses.isEmpty()) {
            try {
                mongo = new Mongo(new ServerAddress(), mongoOptions);
            } catch (UnknownHostException e) {
                throw new IllegalStateException(String.format(
                        "No addresses were provided, but could not find IP for default host: %s",
                        ServerAddress.defaultHost()), e);
            }
View Full Code Here

                    .getServers() : null;
            if (servers != null && !servers.isEmpty())
            {
                for (Server server : servers)
                {
                    addrs.add(new ServerAddress(server.getHost().trim(), Integer.parseInt(server.getPort().trim())));
                }
                mongo = new Mongo(addrs);
            }
            else
            {
View Full Code Here

     * @throws Exception If an error occurred while trying to connect.
     */
    public MongoConnection(String host, int port, String database) throws Exception {
        MongoOptions options = new MongoOptions();
        options.setThreadsAllowedToBlockForConnectionMultiplier(100);
        mongo = new Mongo(new ServerAddress(host, port), options);
        db = mongo.getDB(database);
    }
View Full Code Here

        Settings settings = Settings.defaultInstance().withPort(1234).withHost("localhost").withDbFactory(FakeDbFactory.class).withReadPreference(ReadPreference.nearest());

        FakeDbFactory dbFactory = (FakeDbFactory) settings.createDbFactory();

        assertThat(dbFactory.addresses.size(), is(1));
        ServerAddress serverAddress = dbFactory.addresses.get(0);
        assertThat(serverAddress.getHost(), is("localhost"));
        assertThat(serverAddress.getPort(), is(1234));
        assertThat(settings.authenticationRequired(), is(false));
        assertThat(dbFactory.getReadPreference(), is(ReadPreference.nearest()));
    }
View Full Code Here

        assertThat(dbFactory.getReadPreference(), is(ReadPreference.nearest()));
    }

    @Test
    public void canCreateDbFactoryWithMultipleAddresses() throws UnknownHostException {
        Settings settings = Settings.defaultInstance().withAddresses(Lists.newArrayList(new ServerAddress("localhost:1234"), new ServerAddress("localhost:1235"))).withDbFactory(FakeDbFactory.class);

        FakeDbFactory dbFactory = (FakeDbFactory) settings.createDbFactory();

        assertThat(dbFactory.addresses.size(), is(2));
    }
View Full Code Here

        assertThat(settings, notNullValue());
        DbFactory dbFactory = settings.createDbFactory();
        assertThat(dbFactory, notNullValue());
        assertThat(dbFactory, not(instanceOf(FakeDbFactory.class)));
        assertThat(dbFactory.getAddresses().size(), is(1));
        ServerAddress serverAddress = dbFactory.getAddresses().get(0);
        assertThat(serverAddress.getPort(), is(27017));
        assertThat(serverAddress.getHost(), is("127.0.0.1"));
        assertThat(settings.getDbName(), is("test"));
        assertThat(settings.getUpdateStrategy(), is(UpdateStrategies.OVERWRITE));
        assertThat(settings.getReadPreference(), is(ReadPreference.primary()));
    }
View Full Code Here

        return readPreference;
    }

    private static ServerAddress serverAddress(String host, int port) {
        try {
            return new ServerAddress(host, port);
        } catch (UnknownHostException e) {
            throw new MongoLinkError(e);
        }
    }
View Full Code Here

            member.config = new MongodConfigBuilder().version(Versions.withFeatures(new GenericVersion(rsSettings.get("version"))))
                .net(new de.flapdoodle.embed.mongo.config.Net(ports[i - 1], Network.localhostIsIPv6())).replication(storage).build();
            logger.trace("replSetName in config: {}", member.config.replication().getReplSetName());
            member.executable = type.starter.prepare(member.config);
            member.process = member.executable.start();
            member.address = new ServerAddress(Network.getLocalHost().getHostName(), member.config.net().getPort());
            logger.debug("Server #" + i + ": {}", member.address);
            builder.add(member);
        }
        ImmutableList<MongoReplicaSet.Member> members = builder.build();
        Thread.sleep(2000);
        MongoClientOptions mco = MongoClientOptions.builder().autoConnectRetry(true).connectTimeout(15000).socketTimeout(60000).build();
        Mongo mongo = new MongoClient(new ServerAddress(Network.getLocalHost().getHostName(), ports[0]), mco);
        DB mongoAdminDB = mongo.getDB(ADMIN_DATABASE_NAME);

        cr = mongoAdminDB.command(new BasicDBObject("isMaster", 1));
        logger.debug("isMaster: " + cr);
View Full Code Here

TOP

Related Classes of com.mongodb.ServerAddress

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.