首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java SO_timeout和Connection_Timeout问题--如何将java客户端与couchbase连接的时间设置得更长?

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

Stack Overflow用户
提问于 2013-10-30 05:58:22
回答 1查看 388关注 0票数 0

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

当我运行javacode时,它会反复连接到机器,然后执行一些操作,然后再断开连接、重新连接、验证等等,这浪费了很多时间。

如何将连接的时间设置为更长的时间,以便在连接时执行更多操作,而不浪费每5秒重新连接和身份验证的时间?

代码语言:javascript
复制
    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. 

--

有些评论和重新连接认证浪费了很多时间..如何增加这一次,以便我可以在重新连接之前做更多的操作

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-31 06:47:10

这很容易回答。只创建一次CouchbaseClient对象(在单例中),然后在请求中重用它。这是记录在案的最佳实践,也是有意义的,因为在后台完成的所有东西的对象创建都是昂贵的。

另外,确保不要像对待关系数据库表一样对待存储桶。一个应用程序的所有数据都应该放在一个桶中,然后您--通过设计--只需要一个CouchbaseClient对象。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19674908

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档