0

我正在使用CouchbaseConnectionFactoryBuilderHTTPnio 连接到拥有 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
4

1 回答 1

4

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

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

于 2013-10-31T06:47:10.837 回答