Search in sources :

Example 96 with ServerAddress

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());
}
Also used : ServerCursor(com.mongodb.ServerCursor) ServerAddress(com.mongodb.ServerAddress) Test(org.junit.jupiter.api.Test)

Example 97 with ServerAddress

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);
}
Also used : ServerCursor(com.mongodb.ServerCursor) ServerAddress(com.mongodb.ServerAddress) Test(org.junit.jupiter.api.Test)

Example 98 with ServerAddress

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);
}
Also used : MongoClient(com.mongodb.MongoClient) MongoCredential(com.mongodb.MongoCredential) Setting(cn.hutool.setting.Setting) ServerAddress(com.mongodb.ServerAddress) ArrayList(java.util.ArrayList) DbRuntimeException(cn.hutool.db.DbRuntimeException) DbRuntimeException(cn.hutool.db.DbRuntimeException) NotInitedException(cn.hutool.core.exceptions.NotInitedException)

Example 99 with ServerAddress

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));
}
Also used : NotInitedException(cn.hutool.core.exceptions.NotInitedException) Setting(cn.hutool.setting.Setting) ServerAddress(com.mongodb.ServerAddress)

Example 100 with ServerAddress

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);
}
Also used : MongoClient(com.mongodb.MongoClient) MongoCredential(com.mongodb.MongoCredential) ServerAddress(com.mongodb.ServerAddress)

Aggregations

ServerAddress (com.mongodb.ServerAddress)201 Test (org.junit.Test)49 MongoClient (com.mongodb.MongoClient)48 ArrayList (java.util.ArrayList)31 Test (org.junit.jupiter.api.Test)31 MongoCredential (com.mongodb.MongoCredential)30 ClusterSettings (com.mongodb.connection.ClusterSettings)20 ClusterId (com.mongodb.connection.ClusterId)19 MongoClientSettings (com.mongodb.MongoClientSettings)18 BsonDocument (org.bson.BsonDocument)18 Before (org.junit.Before)18 ClusterDescription (com.mongodb.connection.ClusterDescription)15 Document (org.bson.Document)14 ServerDescription (com.mongodb.connection.ServerDescription)13 List (java.util.List)12 RepeatedTest (org.junit.jupiter.api.RepeatedTest)12 ConnectionString (com.mongodb.ConnectionString)10 MongoClient (com.mongodb.client.MongoClient)9 MongoDatabase (com.mongodb.client.MongoDatabase)9 ServerId (com.mongodb.connection.ServerId)8