本文整理匯總了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);
}
}
}