Search in sources :

Example 1 with Builder

use of com.couchbase.client.core.env.TimeoutConfig.Builder in project connectors-se by Talend.

the class CouchbaseService method openConnection.

public Cluster openConnection(CouchbaseDataStore dataStore) {
    String bootStrapNodes = dataStore.getBootstrapNodes();
    String username = dataStore.getUsername();
    String password = dataStore.getPassword();
    String urls = Arrays.stream(resolveAddresses(bootStrapNodes)).collect(Collectors.joining(","));
    ClusterHolder holder = clustersPool.computeIfAbsent(dataStore, ds -> {
        ClusterEnvironment.Builder envBuilder = ClusterEnvironment.builder();
        if (dataStore.isUseConnectionParameters()) {
            Builder timeoutBuilder = TimeoutConfig.builder();
            dataStore.getConnectionParametersList().forEach(conf -> setTimeout(timeoutBuilder, envBuilder, conf.getParameterName(), parseValue(conf.getParameterValue())));
            envBuilder.timeoutConfig(timeoutBuilder);
        }
        ClusterEnvironment environment = envBuilder.build();
        Cluster cluster = Cluster.connect(urls, ClusterOptions.clusterOptions(username, password).environment(environment));
        try {
            cluster.waitUntilReady(Duration.ofSeconds(3), WaitUntilReadyOptions.waitUntilReadyOptions().desiredState(ClusterState.ONLINE));
        } catch (UnambiguousTimeoutException e) {
            LOG.error(i18n.connectionKO());
            throw new ComponentException(e);
        }
        return new ClusterHolder(environment, cluster);
    });
    holder.use();
    Cluster cluster = holder.getCluster();
    // connection is lazily initialized; need to send actual request to test it
    cluster.buckets().getAllBuckets();
    PingResult pingResult = cluster.ping();
    LOG.debug(i18n.connectedToCluster(pingResult.id()));
    return cluster;
}
Also used : ClusterEnvironment(com.couchbase.client.java.env.ClusterEnvironment) PingResult(com.couchbase.client.core.diagnostics.PingResult) Builder(com.couchbase.client.core.env.TimeoutConfig.Builder) ComponentException(org.talend.sdk.component.api.exception.ComponentException) Cluster(com.couchbase.client.java.Cluster) UnambiguousTimeoutException(com.couchbase.client.core.error.UnambiguousTimeoutException)

Aggregations

PingResult (com.couchbase.client.core.diagnostics.PingResult)1 Builder (com.couchbase.client.core.env.TimeoutConfig.Builder)1 UnambiguousTimeoutException (com.couchbase.client.core.error.UnambiguousTimeoutException)1 Cluster (com.couchbase.client.java.Cluster)1 ClusterEnvironment (com.couchbase.client.java.env.ClusterEnvironment)1 ComponentException (org.talend.sdk.component.api.exception.ComponentException)1