本文整理汇总了Java中com.mongodb.MongoClientURI.getHosts方法的典型用法代码示例。如果您正苦于以下问题:Java MongoClientURI.getHosts方法的具体用法?Java MongoClientURI.getHosts怎么用?Java MongoClientURI.getHosts使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.mongodb.MongoClientURI
的用法示例。
在下文中一共展示了MongoClientURI.getHosts方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createUser
import com.mongodb.MongoClientURI; //导入方法依赖的package包/类
public static void createUser() throws IOException, InterruptedException {
String mongoHost = MultiValueConfigLoader.getConfig("MONGODB-integrationTest");
log.info("Host is " + mongoHost);
if (mongoHost != null) {
MongoClientURI uri = new MongoClientURI(mongoHost);
List<String> hosts = uri.getHosts();
for (String host : hosts) {
String[] cmdarray = createSetupCommand(host, uri.getDatabase(), uri.getUsername(), new String(uri.getPassword()));
Process process = Runtime.getRuntime().exec(cmdarray);
int retVal = process.waitFor();
log.info(String.format("retVal=%s", retVal));
log.info("output is " + IOUtils.toString(process.getInputStream()));
if (retVal != 0) {
log.info("error is " + IOUtils.toString(process.getErrorStream()));
}
}
} else {
log.error("mongo host is not defined!");
}
}
示例2: extractServerAddresses
import com.mongodb.MongoClientURI; //导入方法依赖的package包/类
private ServerAddress[] extractServerAddresses(MongoClientURI mongoClientURI) {
final List<String> hosts = mongoClientURI.getHosts();
final List<ServerAddress> serverAddresses = new ArrayList<>(hosts.size());
for (String host : hosts) {
final HostAndPort hostAndPort = HostAndPort.fromString(host).withDefaultPort(ServerAddress.defaultPort());
final ServerAddress serverAddress = new ServerAddress(hostAndPort.getHostText(), hostAndPort.getPort());
serverAddresses.add(serverAddress);
}
return serverAddresses.toArray(new ServerAddress[serverAddresses.size()]);
}
示例3: validateMongoClientURI
import com.mongodb.MongoClientURI; //导入方法依赖的package包/类
/**
* Validates MongoClientURI
* @param mongoClientURI {MongoClientURI} must not be null and contain valid host and (optional) port.
*/
private void validateMongoClientURI(MongoClientURI mongoClientURI)
{
// 1. Argument not optional
if (null== mongoClientURI)
{
throw new IllegalArgumentException("'mongoClientURI' argument may not be null.");
}
// 2. Validate host
for(String host : mongoClientURI.getHosts())
{
// ensure not null or empty or just whitespace chars
if ( null != host && host.trim().length() > 0)
{
try
{
// create a URI from the host name - may throw URISyntaxException
URI uri = new URI("my://" + host);
// get host without port from URI
host = uri.getHost();
if (null == host || host.trim().length() == 0 )
{
throw new IllegalArgumentException("'mongoClientURI' argument must contain a valid host: " + mongoClientURI);
}
}
catch (URISyntaxException ex)
{
// validation failed due to malformed host
throw new IllegalArgumentException("'mongoClientURI' argument must contain a valid host: " + mongoClientURI);
}
}
else
{
throw new IllegalArgumentException("'mongoClientURI' argument: host is mandatory: " + mongoClientURI);
}
}
}