List of usage examples for com.mongodb ServerAddress ServerAddress
public ServerAddress(@Nullable final String host, final int port)
From source file:com.sfelf.connectors.mongoOplogCursorConnector.java
License:Open Source License
/** * Method to parse and build a list of {@link ServerAddress} from the supplied servers string * /*w w w . j a v a 2 s .c o m*/ * @param servers A comma separated list of host port pairs separated by a colon (ex: localhost:27017,localhost:27018,localhost:27019). * @return {@link List}<{@link ServerAddress}> * @throws UnknownHostException */ private List<ServerAddress> buildServerList(String servers) throws UnknownHostException { List<ServerAddress> serverList = new ArrayList<ServerAddress>(); String[] serverArray = servers.split(","); for (int i = 0; i < serverArray.length; i++) { LOGGER.debug("Adding mongo server: " + serverArray[i]); if (!serverArray[i].contains(":")) { throw new UnknownHostException("Invalid host:port pair in servers string: " + serverArray[i]); } String[] server = serverArray[i].split(":", 2); int port = 0; try { port = Integer.parseInt(server[1]); } catch (NumberFormatException e) { throw new UnknownHostException( "Invalid port number in the host:port pair in servers string: " + serverArray[i]); } serverList.add(new ServerAddress(server[0], port)); } return serverList; }
From source file:com.sitewhere.mongodb.MongoDbClient.java
License:Open Source License
/** * Parse hostname(s) and port(s) into {@link ServerAddress} entries. * // w ww . j av a 2s. c o m * @return * @throws SiteWhereException */ protected List<ServerAddress> parseServerAddresses() throws SiteWhereException { String[] hosts = getHostname().getValue().split(","); String[] ports = getConfiguration().getPort().split(","); if (hosts.length != ports.length) { throw new SiteWhereException("Number of hosts does not match number of ports. Hosts(" + arrayAsString(hosts) + ") Ports(" + arrayAsString(ports) + ")."); } List<ServerAddress> addresses = new ArrayList<ServerAddress>(); for (int i = 0; i < hosts.length; i++) { try { addresses.add(new ServerAddress(hosts[i].trim(), Integer.parseInt(ports[i].trim()))); } catch (NumberFormatException e) { throw new SiteWhereException("Non-numeric port number specified for MQTT broker."); } } return addresses; }
From source file:com.socialsky.mods.MongoPersistor.java
License:Apache License
@Override public void start() { super.start(); address = getOptionalStringConfig("address", "vertx.mongopersistor"); host = getOptionalStringConfig("host", "localhost"); port = getOptionalIntConfig("port", 27017); dbName = getOptionalStringConfig("db_name", "default_db"); username = getOptionalStringConfig("username", null); password = getOptionalStringConfig("password", null); readPreference = ReadPreference.valueOf(getOptionalStringConfig("read_preference", "primary")); int poolSize = getOptionalIntConfig("pool_size", 10); autoConnectRetry = getOptionalBooleanConfig("auto_connect_retry", true); socketTimeout = getOptionalIntConfig("socket_timeout", 60000); useSSL = getOptionalBooleanConfig("use_ssl", false); JsonArray seedsProperty = config.getArray("seeds"); try {//from w w w . j a v a 2 s . c o m MongoClientOptions.Builder builder = new MongoClientOptions.Builder(); builder.connectionsPerHost(poolSize); builder.autoConnectRetry(autoConnectRetry); builder.socketTimeout(socketTimeout); builder.readPreference(readPreference); if (useSSL) { builder.socketFactory(SSLSocketFactory.getDefault()); } if (seedsProperty == null) { ServerAddress address = new ServerAddress(host, port); mongo = new MongoClient(address, builder.build()); } else { List<ServerAddress> seeds = makeSeeds(seedsProperty); mongo = new MongoClient(seeds, builder.build()); } db = mongo.getDB(dbName); if (username != null && password != null) { db.authenticate(username, password.toCharArray()); } } catch (UnknownHostException e) { logger.error("Failed to connect to mongo server", e); } eb.registerHandler(address, this); }
From source file:com.softinstigate.restheart.db.MongoDBClientSingleton.java
License:Open Source License
private void setup() throws UnknownHostException { if (initialized) { List<ServerAddress> servers = new ArrayList<>(); List<MongoCredential> credentials = new ArrayList<>(); for (Map<String, Object> mongoServer : mongoServers) { Object mongoHost = mongoServer.get(Configuration.MONGO_HOST_KEY); Object mongoPort = mongoServer.get(Configuration.MONGO_PORT_KEY); if (mongoHost != null && mongoHost instanceof String && mongoPort != null && mongoPort instanceof Integer) { servers.add(new ServerAddress((String) mongoHost, (int) mongoPort)); }/*from w w w .j ava 2 s .c om*/ } if (mongoCredentials != null) { for (Map<String, Object> mongoCredential : mongoCredentials) { Object mongoAuthDb = mongoCredential.get(Configuration.MONGO_AUTH_DB_KEY); Object mongoUser = mongoCredential.get(Configuration.MONGO_USER_KEY); Object mongoPwd = mongoCredential.get(Configuration.MONGO_PASSWORD_KEY); if (mongoAuthDb != null && mongoAuthDb instanceof String && mongoUser != null && mongoUser instanceof String && mongoPwd != null && mongoPwd instanceof String) { credentials.add(MongoCredential.createMongoCRCredential((String) mongoUser, (String) mongoAuthDb, ((String) mongoPwd).toCharArray())); } } } MongoClientOptions opts = MongoClientOptions.builder().readPreference(ReadPreference.primaryPreferred()) .writeConcern(WriteConcern.ACKNOWLEDGED).build(); mongoClient = new MongoClient(servers, credentials, opts); } }
From source file:com.softwear.plugins.mongodb.MongoDBScriptObject.java
License:BSD License
public MongoClient js_getMongo(String _host, int _port, String userName, String database, String password) { try {//from ww w.j a v a2 s . c om if (mongoClient == null) { char[] charPassword = password.toCharArray(); MongoCredential credential = MongoCredential.createMongoCRCredential(userName, database, charPassword); mongoClient = new MongoClient(new ServerAddress(_host, _port), Arrays.asList(credential)); //localhost 27017 mongoClient.setWriteConcern(WriteConcern.UNACKNOWLEDGED); } else { return mongoClient; } } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (MongoException e) { // TODO Auto-generated catch block e.printStackTrace(); } return mongoClient; }
From source file:com.speedment.connector.mongodb.MongoDbmsHandler.java
License:Open Source License
private MongoClient getMongoClient() { if (client == null) { final ServerAddress address = new ServerAddress(dbms.getIpAddress().orElse(DEFAULT_HOST), dbms.getPort().orElse(MongoDbmsType.MONGODB.getDefaultPort())); final List<MongoCredential> credentials = new ArrayList<>(); if (dbms.getUsername().isPresent() && dbms.getPassword().isPresent()) { credentials.add(createCredential(dbms.getUsername().get(), dbms.getName(), dbms.getPassword().get().toCharArray())); }/*from ww w. j av a 2 s . co m*/ client = new MongoClient(address, credentials); } return client; }
From source file:com.squid.kraken.v4.persistence.MongoDBHelper.java
License:Open Source License
private static MongoClient getMongo(String mongoName) { MongoClient mongoInstance;//from w ww. ja va 2 s . c o m String mongoHostProp = KrakenConfig.getProperty("kraken.mongodb.host"); String[] mongoHostList = mongoHostProp.split(","); String mongoPortProp = KrakenConfig.getProperty("kraken.mongodb.port"); String[] mongoPortList = mongoPortProp.split(","); MongoClientOptions opt = MongoClientOptions.builder().connectionsPerHost(250).build(); List<ServerAddress> addrs = new ArrayList<ServerAddress>(); for (int i = 0; i < mongoHostList.length; i++) { int mongoPort = Integer.parseInt(mongoPortList[i]); addrs.add(new ServerAddress(mongoHostList[i], mongoPort)); } // Auth String mongoUser = KrakenConfig.getProperty("kraken.mongodb.user", true); String mongoPwd = KrakenConfig.getProperty("kraken.mongodb.password", true); logger.info("kraken.mongodb.dbname : " + mongoName); logger.info("kraken.mongodb.user : " + mongoUser); List<MongoCredential> credentials = new ArrayList<MongoCredential>(); if (mongoUser != null) { credentials.add(MongoCredential.createMongoCRCredential(mongoUser, mongoName, mongoPwd.toCharArray())); } mongoInstance = new MongoClient(addrs, credentials, opt); return mongoInstance; }
From source file:com.stratio.connector.mongodb.core.configuration.MongoClientConfiguration.java
License:Apache License
/** * Gets the seeds.//from ww w . j a v a 2 s. com * * @return the seeds * @throws ConnectionException * @throws ConnectionException * if the list of server address cannot be retrieved */ public List<ServerAddress> getSeeds() throws ConnectionException { ArrayList<ServerAddress> seeds = new ArrayList<ServerAddress>(); Map<String, String> config = configuration.getClusterOptions(); String[] hosts; String[] ports; if (config != null) { String strHosts = config.get(HOST.getOptionName()); if (strHosts != null) { hosts = (String[]) ConnectorParser.hosts(strHosts); } else { hosts = HOST.getDefaultValue(); } String strPorts = config.get(PORT.getOptionName()); if (strPorts != null) { ports = (String[]) ConnectorParser.ports(strPorts); } else { ports = PORT.getDefaultValue(); } } else { hosts = HOST.getDefaultValue(); ports = PORT.getDefaultValue(); } // TODO if (hosts.length < 1 || (hosts.length != ports.length)) { throw new ConnectionException("invalid address"); } else { for (int i = 0; i < hosts.length; i++) { try { seeds.add(new ServerAddress(hosts[i], Integer.parseInt(ports[i]))); } catch (NumberFormatException e) { throw new ConnectionException("wrong port format " + ports[i], e); } catch (UnknownHostException e) { throw new ConnectionException("connection failed with" + hosts[i], e); } } } return seeds; }
From source file:com.streamreduce.datasource.MongoFactoryBean.java
License:Apache License
@Override protected Mongo createInstance() throws Exception { if (StringUtils.hasText(this.hostname)) { return new Mongo(new ServerAddress(hostname, port), getMongoOptions()); } else {//from w w w .j a v a 2 s. co m return new Mongo(); } }
From source file:com.streamreduce.storm.MongoClient.java
License:Apache License
/** * Initializes the {@link Mongo} object. * * @param host the host MongoDB is expected to be running on * @param port the port MongoDB is expected to be listening on * @param username the username to authenticate as * @param password the password to the username above *//*from w w w . j a va 2 s.c o m*/ private void init(String host, int port, String username, String password) { this.host = host; this.port = port; this.username = username; this.password = password; logger.info("Mongo Client Connecting to " + host + ":" + port); try { MongoOptions options = new MongoOptions(); options.autoConnectRetry = true; //options.connectionsPerHost = 50; options.connectTimeout = 1500; options.socketTimeout = 60000; options.threadsAllowedToBlockForConnectionMultiplier = 1000; //options.connectionsPerHost = 50; // * 5, so up to 250 can wait before it dies this.mongo = new Mongo(new ServerAddress(host, port), options); } catch (UnknownHostException e) { MongoException me = new MongoException("Unable to connect to Mongo at " + host + ":" + port, e); logger.error(me.getMessage(), me); throw me; } }