如何在Spring Data Couchbase中设置Couchbase操作超时?
我有一个简单的spring项目,该项目尝试使用spring-data-couchbase从ouchbase检索文档.我已经通过扩展AbstractCouchbaseConfiguration配置了配置.一切都很好.
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?
要为CouchbaseClient定义超时,必须使用ConnectionFactory提供超时.遗憾的是,当前版本的spring-data-couchbase没有提供一种简单的方法来实现这一目标.
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.
由于要扩展AbstractCouchbaseConfiguration,因此您可能需要覆盖couchbaseClient()
:
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);
}
}
此外,您可以直接调用CouchbaseFactoryBean,但如果不使用XML Bean定义配置应用程序,则不是一个好习惯.
Also, you can call directly CouchbaseFactoryBean but it's not a good practice if you are not configuring your application using XML bean definitions.
这里是XML配置,以防万一:
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"/>