Hello list,
Environment:
Couchbase server 3.1 Client SDK 2.1.4
I have a code that wrapped the get value operation from couchbase. And when
it is running, randomly throw TimeOutException error.
No luck after dig from CB's forum and docs.
Appreciate if any clue.
The error message is as below:
java.lang.RuntimeException: java.util.concurrent.TimeoutException
at com.couchbase.client.java.util.Blocking.blockForSingle(Blocking.java:93)
~[couchbase-java-client-2.1.4.jar:2.1.4]
at com.couchbase.client.java.CouchbaseBucket.get(CouchbaseBucket.java:100)
~[couchbase-java-client-2.1.4.jar:2.14]
at com.couchbase.client.java.CouchbaseBucket.get(CouchbaseBucket.java:95)
~[couchbase-java-client-2.1.4.jar:2.14]
at
com.fairyland.jdp.framework.service.CouchbaseService.getValue(CouchbaseService.java:83)
~[classes/:na]
And my code is as below:
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();
}
}
--
jOe
--
You received this message because you are subscribed to the Google Groups
"Couchbase" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.