gpt4 book ai didi

Java SO_timeout 和 Connection_Timeout 问题 - 如何将 java 客户端与 couchbase 连接的时间设置得更长?

转载 作者:行者123 更新时间:2023-12-01 18:41:49 25 4
gpt4 key购买 nike

我正在使用 CouchbaseConnectionFactoryBuilder 和 HTTPnio 连接到拥有 3 台机器的集群。

当我运行javacode时,它会重复连接到机器,然后执行一些操作,然后再次断开连接并重新连接,进行身份验证等,这样做浪费了大量时间。

如何设置更长的连接时间,以便在连接时执行更多操作,并且不会浪费时间在每 5 秒后重新连接和验证?

    public boolean init() throws DBException {      


System.out.println("in INIT **************.");
if (initialized) {
return true;
}
HttpParams params = new SyncBasicHttpParams();
params=params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 50000);
params=params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 50000);

/*
Properties systemProperties = System.getProperties();
systemProperties.put("net.spy.log.LoggerImpl", "net.spy.memcached.compat.log.SunLogger");
System.setProperties(systemProperties);

Logger logger = Logger.getLogger("com.couchbase.client");
logger.setLevel(Level.FINEST);
for(Handler h : logger.getParent().getHandlers()) {
if(h instanceof ConsoleHandler){
h.setLevel(Level.FINEST);
}
}
*/

props = getProperties(); //used to set default configuration
String url = props.getProperty(CONNECTION_URL, DEFAULT_PROP);
String bucket = props.getProperty(CONNECTION_BUCKET, DEFAULT_PROP);
String user = props.getProperty(CONNECTION_USER, DEFAULT_PROP);
String passwd = props.getProperty(CONNECTION_PASSWD, DEFAULT_PROP);

gson = new Gson();

try {
String urls[]= url.split(",");
for(int i=0;i<urls.length;i++)
uris.add(URI.create(urls[i]));

CouchbaseConnectionFactoryBuilder cfb = new CouchbaseConnectionFactoryBuilder(); //Object to make connections to couchbase
cfb.setOpTimeout(2000000); // wait up to 10 seconds for an operation to succeed
//cfb.setOpQueueMaxBlockTime(5000); // wait up to 5 seconds when trying to enqueue an operation
user_client = new CouchbaseClient(cfb.buildCouchbaseConnection(uris, "users", "")); //connection object for user bucket
res_client = new CouchbaseClient(cfb.buildCouchbaseConnection(uris, "resources", ""));
manip_client = new CouchbaseClient(cfb.buildCouchbaseConnection(uris, "manipulation", ""));
photo_client = new CouchbaseClient(cfb.buildCouchbaseConnection(uris, "photos", ""));

} catch (IOException e) {
e.printStackTrace();
return false;
}

initialized = true;
return true;

}

public void cleanup(boolean warmup) { //ends connection of thread with the DS
System.out.println("shutdown coucebase client connection");
user_client.shutdown(1, TimeUnit.SECONDS);
res_client.shutdown(1, TimeUnit.SECONDS);
manip_client.shutdown(1, TimeUnit.SECONDS);
photo_client.shutdown(1, TimeUnit.SECONDS);
initialized = false;
}

2013-10-29 23:13:55.491 INFO com.couchbase.client.ViewConnection: Added SANKET-PC to connect queue
2013-10-29 23:13:55.495 INFO com.couchbase.client.ViewConnection: Added awesome to connect queue
2013-10-29 23:13:55.497 INFO com.couchbase.client.ViewConnection: Added gaurang-PC1 to connect queue
2013-10-29 23:13:55.497 INFO com.couchbase.client.CouchbaseClient: viewmode property isn't defined. Setting viewmode to production mode
2013-10-29 23:13:55.850 INFO com.couchbase.client.CouchbaseConnection: Added {QA sa=/192.168.0.11:11210, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2013-10-29 23:13:55.851 INFO com.couchbase.client.CouchbaseConnection: Added {QA sa=/192.168.0.12:11210, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2013-10-29 23:13:55.879 INFO com.couchbase.client.CouchbaseConnection: Added {QA sa=/192.168.0.20:11210, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2013-10-29 23:14:00.386 INFO com.couchbase.client.CouchbaseConnection: Connection state changed for sun.nio.ch.SelectionKeyImpl@2430c1a0
2013-10-29 23:14:00.387 INFO com.couchbase.client.CouchbaseConnection: Connection state changed for sun.nio.ch.SelectionKeyImpl@66b55df1
2013-10-29 23:14:00.387 INFO com.couchbase.client.CouchbaseConnection: Connection state changed for sun.nio.ch.SelectionKeyImpl@1308c2db
2013-10-29 23:14:00.390 INFO net.spy.memcached.auth.AuthThread: Authenticated to SANKET-PC/192.168.0.20:11210
2013-10-29 23:14:00.391 INFO net.spy.memcached.auth.AuthThread: Authenticated to awesome/192.168.0.11:11210
2013-10-29 23:14:00.391 INFO net.spy.memcached.auth.AuthThread: Authenticated to gaurang-PC1/192.168.0.12:11210
2013-10-29 23:14:04.892 INFO com.couchbase.client.ViewConnection: Added SANKET-PC to connect queue
2013-10-29 23:14:04.898 INFO com.couchbase.client.ViewConnection: Added awesome to connect queue
2013-10-29 23:14:04.901 INFO com.couchbase.client.ViewConnection: Added gaurang-PC1 to connect queue
2013-10-29 23:14:04.903 INFO com.couchbase.client.CouchbaseClient: viewmode property isn't defined. Setting viewmode to production mode
2013-10-29 23:14:05.206 INFO com.couchbase.client.CouchbaseConnection: Added {QA sa=/192.168.0.11:11210, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2013-10-29 23:14:05.207 INFO com.couchbase.client.CouchbaseConnection: Added {QA sa=/192.168.0.12:11210, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2013-10-29 23:14:05.208 INFO com.couchbase.client.CouchbaseConnection: Added {QA sa=/192.168.0.20:11210, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2013-10-29 23:14:10.012 INFO com.couchbase.client.CouchbaseConnection: Connection state changed for sun.nio.ch.SelectionKeyImpl@3fe40c9f
2013-10-29 23:14:10.013 INFO com.couchbase.client.CouchbaseConnection: Connection state changed for sun.nio.ch.SelectionKeyImpl@14f8bb7e
2013-10-29 23:14:10.014 INFO com.couchbase.client.CouchbaseConnection: Connection state changed for sun.nio.ch.SelectionKeyImpl@201cc181
2013-10-29 23:14:10.016 INFO com.couchbase.client.ViewConnection: Added awesome to connect queue
2013-10-29 23:14:10.017 INFO net.spy.memcached.auth.AuthThread: Authenticated to gaurang-PC1/192.168.0.12:11210
2013-10-29 23:14:10.027 INFO net.spy.memcached.auth.AuthThread: Authenticated to awesome/192.168.0.11:11210
2013-10-29 23:14:10.027 INFO com.couchbase.client.ViewConnection: Added gaurang-PC1 to connect queue
2013-10-29 23:14:10.027 INFO net.spy.memcached.auth.AuthThread: Authenticated to SANKET-PC/192.168.0.20:11210
2013-10-29 23:14:14.535 INFO com.couchbase.client.ViewConnection: Added SANKET-PC to connect queue
2013-10-29 23:14:14.536 INFO com.couchbase.client.CouchbaseClient: viewmode property isn't defined.

--
是否进行一些操作并再次重新连接身份验证会浪费大量时间..如何增加这个时间,以便我可以在重新连接之前执行更多操作

2013-10-29 22:58:48.132 INFO com.couchbase.client.ViewConnection:  Added SANKET-PC to connect queue
2013-10-29 22:58:48.133 INFO com.couchbase.client.ViewConnection: Added gaurang-PC1 to connect queue
2013-10-29 22:58:48.134 INFO com.couchbase.client.CouchbaseClient: viewmode property isn't defined. Setting viewmode to production mode
2013-10-29 22:58:48.941 INFO com.couchbase.client.CouchbaseConnection: Added {QA sa=/192.168.0.11:11210, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2013-10-29 22:58:48.941 INFO com.couchbase.client.CouchbaseConnection: Added {QA sa=/192.168.0.12:11210, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2013-10-29 22:58:48.942 INFO com.couchbase.client.CouchbaseConnection: Added {QA sa=/192.168.0.20:11210, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2013-10-29 22:58:53.451 INFO com.couchbase.client.CouchbaseConnection: Connection state changed for sun.nio.ch.SelectionKeyImpl@34c945ea
2013-10-29 22:58:53.451 INFO com.couchbase.client.ViewConnection: Added gaurang-PC1 to connect queue
2013-10-29 22:58:53.451 INFO com.couchbase.client.CouchbaseConnection: Connection state changed for sun.nio.ch.SelectionKeyImpl@a0d20b9
2013-10-29 22:58:53.454 INFO com.couchbase.client.CouchbaseConnection: Connection state changed for sun.nio.ch.SelectionKeyImpl@107dd383
2013-10-29 22:58:53.456 INFO net.spy.memcached.auth.AuthThread: Authenticated to gaurang-PC1/192.168.0.12:11210
2013-10-29 22:58:53.456 INFO com.couchbase.client.ViewConnection: Added awesome to connect queue
2013-10-29 22:58:53.459 INFO net.spy.memcached.auth.AuthThread: Authenticated to awesome/192.168.0.11:11210
2013-10-29 22:58:53.459 INFO net.spy.memcached.auth.AuthThread: Authenticated to SANKET-PC/192.168.0.20:11210
2013-10-29 22:58:57.961 INFO com.couchbase.client.ViewConnection: Added SANKET-PC to connect queue
2013-10-29 22:58:57.962 INFO com.couchbase.client.CouchbaseClient: viewmode property isn't defined. Setting viewmode to production mode
2013-10-29 22:58:58.251 INFO com.couchbase.client.CouchbaseConnection: Added {QA sa=/192.168.0.11:11210, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2013-10-29 22:58:58.252 INFO com.couchbase.client.CouchbaseConnection: Added {QA sa=/192.168.0.12:11210, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2013-10-29 22:58:58.252 INFO com.couchbase.client.CouchbaseConnection: Added {QA sa=/192.168.0.20:11210, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2013-10-29 22:59:02.754 INFO com.couchbase.client.CouchbaseConnection: Connection state changed for sun.nio.ch.SelectionKeyImpl@f9da0cd
2013-10-29 22:59:02.755 INFO com.couchbase.client.CouchbaseConnection: Connection state changed for sun.nio.ch.SelectionKeyImpl@35a7fabd
2013-10-29 22:59:02.755 INFO com.couchbase.client.CouchbaseConnection: Connection state changed for sun.nio.ch.SelectionKeyImpl@3027f656
2013-10-29 22:59:02.757 INFO net.spy.memcached.auth.AuthThread: Authenticated to gaurang-PC1/192.168.0.12:11210
2013-10-29 22:59:02.757 INFO net.spy.memcached.auth.AuthThread: Authenticated to SANKET-PC/192.168.0.20:11210
2013-10-29 22:59:02.758 INFO net.spy.memcached.auth.AuthThread: Authenticated to awesome/192.168.0.11:11210

最佳答案

这个问题很容易回答。创建 CouchbaseClient 对象一次(在单例中),然后在请求之间重用它。这是记录在案的最佳实践并且很有意义,因为在后台完成的所有操作的对象创建成本很高。

此外,请确保不要将存储桶视为关系数据库表。一个应用程序的所有数据都应该放入一个存储桶中,那么根据设计,您也只需要一个 CouchbaseClient 对象。

关于Java SO_timeout 和 Connection_Timeout 问题 - 如何将 java 客户端与 couchbase 连接的时间设置得更长?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19674908/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com