Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set couchbase operation timeout in spring data couchbase?

I have a simple spring project which try to retrieve a document from couchbase using spring-data-couchbase. I have configured the config by extending AbstractCouchbaseConfiguration. Everything works perfectly fine.

Since I use couchbase as a cache, now I need to set the operation timeout to a lower value. Anybody can shed some light on how to do it?

like image 980
Aris Feryanto Avatar asked Jul 09 '15 19:07

Aris Feryanto


4 Answers

According to the docs, the correct answer is wrong. That's not the way it should be done...

When you extend from AbstractCouchbaseConfiguration

Default settings can be customized through the DefaultCouchbaseEnvironment.Builder or through the setting of system properties. Latter ones take always precedence and can be used to override builder settings at runtime too. http://docs.couchbase.com/sdk-api/couchbase-java-client-2.0.0/com/couchbase/client/java/env/DefaultCouchbaseEnvironment.html

For instance, to customize the timeout connection:

@Override
protected CouchbaseEnvironment getEnvironment() {
        DefaultCouchbaseEnvironment.builder().connectTimeout(15000);
        return super.getEnvironment();
}

There are other options that can be assigned this way.

like image 113
Carlos Andres Avatar answered Nov 20 '22 07:11

Carlos Andres


According the docs (https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html ),

Here is the application.properties :

spring.couchbase.env.timeouts.connect=5000ms # Bucket connections timeouts.
spring.couchbase.env.timeouts.key-value=2500ms # Blocking operations performed on a specific key timeout.
spring.couchbase.env.timeouts.query=7500ms # N1QL query operations timeout.
spring.couchbase.env.timeouts.socket-connect=1000ms # Socket connect connections timeout.
spring.couchbase.env.timeouts.view=7500ms # Regular and geospatial view operations timeout.
like image 39
Serhat Yıldırım Avatar answered Nov 20 '22 06:11

Serhat Yıldırım


To define a timeout for the CouchbaseClient you have to provide it using the ConnectionFactory. Sadly, the current version of spring-data-couchbase doesn't provide a simple way to do that.

The class responsible to create connection factories is ConnectionFactoryBean, and it has a setter for the operations timeout, but I couldn't find anything for @Configuration classes.

Since you are extending AbstractCouchbaseConfiguration, you might want to override couchbaseClient():

public class MyCouchbaseConfiguration extends AbstractCouchbaseConfiguration {

     ...

     private final CouchbaseConnectionFactoryBuilder builder = new CouchbaseConnectionFactoryBuilder();
     private CouchbaseConnectionFactory connectionFactory;

     ...

     @Override
     @Bean(destroyMethod = "shutdown")
     public CouchbaseClient couchbaseClient() throws Exception {
           setLoggerProperty(couchbaseLogger());

           if(connectionFactory == null){
               builder.setOpTimeout(myTimeout);
               // Set another parameters.
               ...

               connectionFactory = builder.buildCouchbaseConnection(
                   bootstrapUris(bootstrapHosts()),
                   getBucketName(),
                   getBucketPassword()
               );
           }

           return new CouchbaseClient(connectionFactory);
     }
}

Also, you can call directly CouchbaseFactoryBean but it's not a good practice if you are not configuring your application using XML bean definitions.

Here is the XML configuration just in case:

<bean id="couchbase" class="org.springframework.data.couchbase.core.CouchbaseFactoryBean">
    <property name="opTimeout" value="1000"/> <!-- 1 sec -->
    <property name="bucket" value="myBucket"/>
    <property name="password" value="myPassword"/>
    <property name="host" value="myHost"/>
</bean>
<couchbase:template id="couchbaseTemplate"/>
like image 28
CarlosMecha Avatar answered Nov 20 '22 07:11

CarlosMecha


For Spring Data Couchbase 2, adding the following property in application.properties did it

 spring.couchbase.env.timeouts.connect=20000
like image 1
Gonen I Avatar answered Nov 20 '22 07:11

Gonen I