重新连接到同一Zookeeper服务器,会话ID失败

时间:2016-10-26 09:43:14

标签: apache-zookeeper

我的客户端应用程序连接到Zookeeper服务器并创建一个Ephemeral节点:

    ZooKeeper zk = new ZooKeeper(hostUrl, 40000, null);
    if (zk != null) {
        try {
            zk.create(zpath, "Test Node".getBytes(), 
                    ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

如果我的客户端应用程序在会话超时期间崩溃并重新启动,我想使用从上一个会话收到的会话ID重新连接到同一个Zookeeper服务器:

    ZooKeeper zk = new ZooKeeper(hostUrl, 40000, null, previoudSessionId, null);

然而,当我尝试这样做时,我总是得到一个会话过期的异常:

12:20:26.737 [main-SendThread(172.20.34.24:2181)] INFO  org.apache.zookeeper.ClientCnxn - Opening socket connection to server 172.20.34.24/172.20.34.24:2181. Will not attempt to authenticate using SASL (unknown error)
12:20:26.737 [main-SendThread(172.20.34.24:2181)] INFO  org.apache.zookeeper.ClientCnxn - Socket connection established, initiating session, client: /172.20.34.57:50462, server: 172.20.34.24/172.20.34.24:2181
12:20:26.737 [main-SendThread(172.20.34.24:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Session establishment request sent on 172.20.34.24/172.20.34.24:2181
12:20:26.737 [main-SendThread(172.20.34.24:2181)] INFO  org.apache.zookeeper.ClientCnxn - Unable to reconnect to ZooKeeper service, session 0x157ff4a59300008 has expired, closing socket connection
12:20:26.747 [main-EventThread] ERROR org.apache.zookeeper.ClientCnxn - Error while calling watcher 
java.lang.NullPointerException: null
    at org.apache.zookeeper.ClientCnxn$EventThread.processEvent(ClientCnxn.java:553) [zookeeper-3.5.1-alpha.jar:3.5.1-alpha-1693007]
    at org.apache.zookeeper.ClientCnxn$EventThread.run(ClientCnxn.java:528) [zookeeper-3.5.1-alpha.jar:3.5.1-alpha-1693007]
12:20:26.747 [main-EventThread] INFO  org.apache.zookeeper.ClientCnxn - EventThread shut down for session: 0x157ff4a59300008
org.apache.zookeeper.KeeperException$SessionExpiredException: KeeperErrorCode = Session expired for /Ephemeral
    at org.apache.zookeeper.KeeperException.create(KeeperException.java:131)
    at org.apache.zookeeper.KeeperException.create(KeeperException.java:51)
    at org.apache.zookeeper.ZooKeeper.create(ZooKeeper.java:1209)
    at curatorExp.ZookeeperExp.reconnectWithSessionId(ZookeeperExp.java:53)
    at curatorExp.ZookeeperExp.main(ZookeeperExp.java:24)

即使在获得此异常后,如果我使用shell检查Zookeeper服务器,我可以看到在崩溃之前创建的临时节点。 (我确保在会话超时40秒之前重新连接)

这预计会起作用吗?

0 个答案:

没有答案