如何检测是否已连接Memcached服务器?

时间:2019-12-21 22:35:18

标签: memcached spy spymemcached

我正在使用net.spy.memcached.MemcachedClient,但是我发现很难检测目标memcached服务器是否可用:

    System.out.println("connecting  memcached server: "+serverName + ":" + port);
    System.setProperty("net.spy.verifyAliveOnConnect", "true");
    InetSocketAddress address =  new InetSocketAddress(serverName, port);
    MemcachedClient mcc = new MemcachedClient(address);

    for (MemcachedNode node : mcc.getConnection().getLocator().getAll()) {
        if (!node.isActive()) {
            System.out.println("Failed to connect to Memcached server");
            mcc.shutdown();            
            throw new IOException("can't connect the server");
        }
    }

    System.out.println("Connection to server successfully");

在调试模式下(逐步),代码可以正确运行。 但是,如果不处于调试模式,则始终抛出该IOException甚至memcached服务器可用。 设置节点活动状态似乎是异步工作。

我试图在第2行中设置net.spy.verifyAliveOnConnect属性没有帮助。

也尝试了另一种方式:

        ConnectionFactory factory = new ConnectionFactoryBuilder()
            .setDaemon(true)
            .build();
    List<InetSocketAddress> servers = AddrUtil.getAddresses(serverName + ":" + port);
    MemcachedClient mcc = new MemcachedClient(factory,servers);

没有帮助。

0 个答案:

没有答案