Search in sources :

Example 6 with ClusterEnvironment

use of com.couchbase.client.java.env.ClusterEnvironment in project spring-data-couchbase by spring-projects.

the class CollectionAwareIntegrationTests method beforeAll.

@BeforeAll
public static void beforeAll() {
    callSuperBeforeAll(new Object() {
    });
    ClusterEnvironment environment = environment().build();
    Cluster cluster = Cluster.connect(seedNodes(), ClusterOptions.clusterOptions(authenticator()).environment(environment));
    Bucket bucket = cluster.bucket(config().bucketname());
    bucket.waitUntilReady(Duration.ofSeconds(5));
    waitForService(bucket, ServiceType.QUERY);
    waitForQueryIndexerToHaveBucket(cluster, config().bucketname());
    CollectionManager collectionManager = bucket.collections();
    setupScopeCollection(cluster, scopeName, collectionName, collectionManager);
    setupScopeCollection(cluster, scopeName, collectionName2, collectionManager);
    if (otherScope != null || otherCollection != null) {
        // afterAll should be undoing the creation of scope etc
        setupScopeCollection(cluster, otherScope, otherCollection, collectionManager);
    }
    try {
        // needs an index for this N1ql Join
        // create index ix2 on my_bucket(parent_id) where `_class` = 'org.springframework.data.couchbase.domain.Address';
        List<String> fieldList = new ArrayList<>();
        fieldList.add("parentId");
        cluster.query("CREATE INDEX `parent_idx` ON default:" + bucketName() + "." + scopeName + "." + collectionName2 + "(parentId)");
    } catch (IndexExistsException ife) {
        LOGGER.warn("IndexFailureException occurred - ignoring: ", ife.toString());
    }
    Config.setScopeName(scopeName);
    ApplicationContext ac = new AnnotationConfigApplicationContext(Config.class);
    // the Config class has been modified, these need to be loaded again
    couchbaseTemplate = (CouchbaseTemplate) ac.getBean(COUCHBASE_TEMPLATE);
    reactiveCouchbaseTemplate = (ReactiveCouchbaseTemplate) ac.getBean(REACTIVE_COUCHBASE_TEMPLATE);
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) ClusterEnvironment(com.couchbase.client.java.env.ClusterEnvironment) IndexExistsException(com.couchbase.client.core.error.IndexExistsException) Bucket(com.couchbase.client.java.Bucket) CollectionManager(com.couchbase.client.java.manager.collection.CollectionManager) ArrayList(java.util.ArrayList) Cluster(com.couchbase.client.java.Cluster) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 7 with ClusterEnvironment

use of com.couchbase.client.java.env.ClusterEnvironment 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)

Example 8 with ClusterEnvironment

use of com.couchbase.client.java.env.ClusterEnvironment in project connectors-se by Talend.

the class CouchbaseService method closeConnection.

public void closeConnection(CouchbaseDataStore ds) {
    ClusterHolder holder = clustersPool.get(ds);
    if (holder == null) {
        return;
    }
    int stillUsed = holder.release();
    if (stillUsed > 0) {
        return;
    }
    clustersPool.remove(ds);
    Cluster cluster = holder.getCluster();
    ClusterEnvironment environment = holder.getEnv();
    if (cluster != null) {
        try {
            cluster.disconnect();
            log.debug(i18n.clusterWasClosed());
        } catch (ComponentException e) {
            log.debug(i18n.cannotCloseCluster());
        }
    }
    if (environment != null) {
        try {
            environment.shutdown();
            log.debug(i18n.couchbaseEnvWasClosed());
        } catch (ComponentException e) {
            log.debug(i18n.cannotCloseCouchbaseEnv());
        }
    }
}
Also used : ClusterEnvironment(com.couchbase.client.java.env.ClusterEnvironment) ComponentException(org.talend.sdk.component.api.exception.ComponentException) Cluster(com.couchbase.client.java.Cluster)

Example 9 with ClusterEnvironment

use of com.couchbase.client.java.env.ClusterEnvironment in project spring-boot by spring-projects.

the class CouchbaseAutoConfigurationTests method customizeJsonSerializer.

@Test
void customizeJsonSerializer() {
    JsonSerializer customJsonSerializer = mock(JsonSerializer.class);
    this.contextRunner.withUserConfiguration(CouchbaseTestConfiguration.class).withConfiguration(AutoConfigurations.of(JacksonAutoConfiguration.class)).withBean(ClusterEnvironmentBuilderCustomizer.class, () -> (builder) -> builder.jsonSerializer(customJsonSerializer)).withPropertyValues("spring.couchbase.connection-string=localhost").run((context) -> {
        ClusterEnvironment env = context.getBean(ClusterEnvironment.class);
        JsonSerializer serializer = env.jsonSerializer();
        assertThat(serializer).extracting("wrapped").isSameAs(customJsonSerializer);
    });
}
Also used : ClusterEnvironment(com.couchbase.client.java.env.ClusterEnvironment) JsonSerializer(com.couchbase.client.java.codec.JsonSerializer) JacksonJsonSerializer(com.couchbase.client.java.codec.JacksonJsonSerializer) Test(org.junit.jupiter.api.Test)

Example 10 with ClusterEnvironment

use of com.couchbase.client.java.env.ClusterEnvironment in project spring-boot by spring-projects.

the class CouchbaseAutoConfigurationTests method whenObjectMapperBeanIsDefinedThenClusterEnvironmentObjectMapperIsDerivedFromIt.

@Test
void whenObjectMapperBeanIsDefinedThenClusterEnvironmentObjectMapperIsDerivedFromIt() {
    this.contextRunner.withUserConfiguration(CouchbaseTestConfiguration.class).withConfiguration(AutoConfigurations.of(JacksonAutoConfiguration.class)).withPropertyValues("spring.couchbase.connection-string=localhost").run((context) -> {
        ClusterEnvironment env = context.getBean(ClusterEnvironment.class);
        Set<Object> expectedModuleIds = new HashSet<>(context.getBean(ObjectMapper.class).getRegisteredModuleIds());
        expectedModuleIds.add(new JsonValueModule().getTypeId());
        JsonSerializer serializer = env.jsonSerializer();
        assertThat(serializer).extracting("wrapped").isInstanceOf(JacksonJsonSerializer.class).extracting("mapper", as(InstanceOfAssertFactories.type(ObjectMapper.class))).extracting(ObjectMapper::getRegisteredModuleIds).isEqualTo(expectedModuleIds);
    });
}
Also used : JacksonJsonSerializer(com.couchbase.client.java.codec.JacksonJsonSerializer) ClusterEnvironment(com.couchbase.client.java.env.ClusterEnvironment) JsonSerializer(com.couchbase.client.java.codec.JsonSerializer) JacksonJsonSerializer(com.couchbase.client.java.codec.JacksonJsonSerializer) JsonValueModule(com.couchbase.client.java.json.JsonValueModule) JacksonAutoConfiguration(org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Aggregations

ClusterEnvironment (com.couchbase.client.java.env.ClusterEnvironment)13 Cluster (com.couchbase.client.java.Cluster)7 Bucket (com.couchbase.client.java.Bucket)5 Collection (com.couchbase.client.java.Collection)4 Test (org.junit.jupiter.api.Test)4 JsonSerializer (com.couchbase.client.java.codec.JsonSerializer)3 SeedNode (com.couchbase.client.core.env.SeedNode)2 JacksonJsonSerializer (com.couchbase.client.java.codec.JacksonJsonSerializer)2 CheckpointDao (com.couchbase.connector.dcp.CheckpointDao)2 CouchbaseCheckpointDao (com.couchbase.connector.dcp.CouchbaseCheckpointDao)2 Duration (java.time.Duration)2 ComponentException (org.talend.sdk.component.api.exception.ComponentException)2 Slf4jReporter (com.codahale.metrics.Slf4jReporter)1 DefaultFullHttpRequest (com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultFullHttpRequest)1 FullHttpRequest (com.couchbase.client.core.deps.io.netty.handler.codec.http.FullHttpRequest)1 PingResult (com.couchbase.client.core.diagnostics.PingResult)1 ConnectionStringPropertyLoader (com.couchbase.client.core.env.ConnectionStringPropertyLoader)1 OwnedSupplier (com.couchbase.client.core.env.OwnedSupplier)1 TimeoutConfig (com.couchbase.client.core.env.TimeoutConfig)1 Builder (com.couchbase.client.core.env.TimeoutConfig.Builder)1