环境:
Couchbase服务器3.1 客户端SDK 2.1.4
我有一个包含来自couchbase的get值操作的代码。当它运行时,随机抛出TimeOutException错误。
从CB的论坛和文档中挖掘后没有运气。
欣赏是否有任何线索。
错误截图如下:
我的代码如下:
package com.fairyland.jdp.framework.service;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.concurrent.TimeUnit;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.commons.lang3.StringUtils;
import com.couchbase.client.java.Bucket;
import com.couchbase.client.java.Cluster;
import com.couchbase.client.java.CouchbaseCluster;
import com.couchbase.client.java.document.JsonDocument;
import com.couchbase.client.java.document.json.JsonObject;
import com.couchbase.client.java.env.DefaultCouchbaseEnvironment;
import com.fairyland.jdp.framework.util.PropsUtil;
public class CouchbaseService implements ServletContextListener{
private static Cluster cluster;
private static Bucket ticketBucket;
// private CouchbaseService(){
//
// }
public static void init(){
if(ticketBucket == null){
String[] hostnames = PropsUtil.get("couchbaseAddr").split(",");
cluster = CouchbaseCluster.create(DefaultCouchbaseEnvironment
.builder()
.build(),hostnames);
String pwd = PropsUtil.get("couchbasePwd");
if(StringUtils.isEmpty(pwd)){
ticketBucket = cluster.openBucket(PropsUtil.get("bucketName"),60,TimeUnit.SECONDS);
}else{
ticketBucket = cluster.openBucket(PropsUtil.get("bucketName"),pwd,60,TimeUnit.SECONDS);
}
}
}
private static synchronized Bucket getBucket(){
init();
return ticketBucket;
}
public static void close(){
if(cluster!=null){
cluster.disconnect();
}
}
public static boolean setValue(String key,int timeout,Serializable value){
String valueJson = null;
try {
// valueJson = JacksonUtil.getInstance().bean2Json(value);
valueJson = serialize(value);
} catch (Exception e) {
e.printStackTrace();
}
// String expireTime = sdf.format(new Date(System.currentTimeMillis()+getTimeout(value)));
// String ticketType = getTicketType(value);
// Create a user and insert it
JsonObject json = JsonObject.empty()
.put("value", valueJson);
// .put("expireTime",expireTime)
// .put("ticketType",ticketType);
JsonDocument doc = null;
if(timeout<=0)
doc = JsonDocument.create(key, json);
else
doc = JsonDocument.create(key,timeout, json);
getBucket().upsert(doc, 10, TimeUnit.SECONDS);
JsonDocument result = getBucket().upsert(doc);
if(result!=null && result.content().getString("value")!=null){
return true;
}else{
return false;
}
}
public static Object getValue(String key){
JsonDocument doc = null;
doc = getBucket().get(key);
if(doc==null)
return null;
String valueJson = doc.content().getString("value");
Object value = null;
value = deSerialize(valueJson);
return value;
}
public static JsonDocument removeValue(String key){
return getBucket().remove(key);
}
public static String serialize(Serializable obj){
try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(obj);
String serStr = byteArrayOutputStream.toString("ISO-8859-1");
serStr = java.net.URLEncoder.encode(serStr, "UTF-8");
objectOutputStream.close();
byteArrayOutputStream.close();
return serStr;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static Object deSerialize(String value){
try {
String redStr = java.net.URLDecoder.decode(value, "UTF-8");
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(redStr.getBytes("ISO-8859-1"));
ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
Object obj = objectInputStream.readObject();
objectInputStream.close();
byteArrayInputStream.close();
return obj;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
public void contextDestroyed(ServletContextEvent arg0) {
close();
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
init();
}
}