use of com.mongodb.ServerAddress in project spring-data-mongodb by spring-projects.
the class CursorReadingTaskUnitTests method stopTaskWhileEmittingMessages.
// DATAMONGO-1803
@Test
public void stopTaskWhileEmittingMessages() throws Throwable {
when(cursor.getServerCursor()).thenReturn(new ServerCursor(10, new ServerAddress("mock")));
when(cursor.tryNext()).thenReturn("hooyah");
runOnce(new MultithreadedStopRunningWhileEmittingMessages(task, cursor));
verify(listener, times(task.getValues().size())).onMessage(any());
}
use of com.mongodb.ServerAddress in project spring-data-mongodb by spring-projects.
the class CursorReadingTaskUnitTests method errorOnNextNotifiesErrorHandlerOnlyOnce.
// DATAMONGO-2366
@Test
public void errorOnNextNotifiesErrorHandlerOnlyOnce() {
ArgumentCaptor<Throwable> errorCaptor = ArgumentCaptor.forClass(Throwable.class);
when(cursor.getServerCursor()).thenReturn(new ServerCursor(10, new ServerAddress("mock")));
when(cursor.tryNext()).thenThrow(new IllegalStateException());
task.run();
verify(errorHandler).handleError(errorCaptor.capture());
assertThat(errorCaptor.getValue()).isInstanceOf(IllegalStateException.class);
}
use of com.mongodb.ServerAddress in project hutool by looly.
the class MongoDS method initCloud.
/**
* 初始化集群<br>
* 集群的其它客户端设定参数使用全局设定<br>
* 集群中每一个实例成员用一个group表示,例如:
*
* <pre>
* user = test1
* pass = 123456
* database = test
* [db0]
* host = 192.168.1.1:27117
* [db1]
* host = 192.168.1.1:27118
* [db2]
* host = 192.168.1.1:27119
* </pre>
*/
public synchronized void initCloud() {
if (groups == null || groups.length == 0) {
throw new DbRuntimeException("Please give replication set groups!");
}
if (setting == null) {
// 若未指定配置文件,则使用默认配置文件
setting = new Setting(MONGO_CONFIG_PATH, true);
}
final List<ServerAddress> addrList = new ArrayList<>();
for (String group : groups) {
addrList.add(createServerAddress(group));
}
final MongoCredential credentail = createCredentail(StrUtil.EMPTY);
try {
if (null == credentail) {
mongo = new MongoClient(addrList, buildMongoClientOptions(StrUtil.EMPTY));
} else {
mongo = new MongoClient(addrList, credentail, buildMongoClientOptions(StrUtil.EMPTY));
}
} catch (Exception e) {
log.error(e, "Init MongoDB connection error!");
return;
}
log.info("Init MongoDB cloud Set pool with connection to {}", addrList);
}
use of com.mongodb.ServerAddress in project hutool by looly.
the class MongoDS method createServerAddress.
// --------------------------------------------------------------------------- Private method start
/**
* 创建ServerAddress对象,会读取配置文件中的相关信息
*
* @param group 分组,如果为{@code null}或者""默认为无分组
* @return ServerAddress
*/
private ServerAddress createServerAddress(String group) {
final Setting setting = checkSetting();
if (group == null) {
group = StrUtil.EMPTY;
}
final String tmpHost = setting.getByGroup("host", group);
if (StrUtil.isBlank(tmpHost)) {
throw new NotInitedException("Host name is empy of group: {}", group);
}
final int defaultPort = setting.getInt("port", group, 27017);
return new ServerAddress(NetUtil.buildInetSocketAddress(tmpHost, defaultPort));
}
use of com.mongodb.ServerAddress in project carina by qaprosoft.
the class MongoConnector method createClient.
/**
* Creates client for DB specified in properties.
*
* @return MongoDB client
* @throws NumberFormatException java.lang.NumberFormatException
* @throws UnknownHostException java.net.UnknownHostException
*/
public static MongoClient createClient() throws NumberFormatException, UnknownHostException {
if (!clients.containsKey(database)) {
validateConfig(database);
MongoCredential credential = MongoCredential.createMongoCRCredential(user, database, password.toCharArray());
clients.put(database, new MongoClient(new ServerAddress(host, Integer.valueOf(port)), Arrays.asList(credential)));
}
return clients.get(database);
}
Aggregations