简体   繁体   English

在java中捕获memcache异常

[英]Catching memcache exception in java

I am using spymemcached-2.8.4.jar and jdk.1.7.我正在使用 spymemcached-2.8.4.jar 和 jdk.1.7。

Here is my code这是我的代码

try {

     MemcachedClient client = new MemcachedClient(...);

     client.set('name', 1000, 'some_name');

}catch(Exception ex){
     System.out.println("Exception occurred");
     System.out.println(ex.getMessage());
     ex.printStackTrace();

     logExceptionInDB(ex);
}

In my scansion, my memcached machine is not running, so it is printing the following exception in console,在我的扫描中,我的 memcached 机器没有运行,所以它在控制台中打印以下异常,

java.net.ConnectException: Connection refused
    at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
    at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:735)
    at net.spy.memcached.MemcachedConnection.handleIO(MemcachedConnection.java:369)
    at net.spy.memcached.MemcachedConnection.handleIO(MemcachedConnection.java:242)
    at net.spy.memcached.MemcachedConnection.run(MemcachedConnection.java:836)

But I want to catch this excepting to write it in database.但我想抓住这个,除了把它写在数据库中。 How to catch this exception?如何捕捉这个异常?

You can't catch the exception mentioned in the post, as it was already catch by spymemcached itself, you can only see the log, the related code in MemcachedConnection.java is :帖子中提到的异常是抓不到的,因为spymemcached本身已经抓到了,只能看到日志, MemcachedConnection.java的相关代码是:

catch (ConnectException var5) {
            this.getLogger().info("Reconnecting due to failure to connect to %s", new Object[]{node, var5});
            this.queueReconnect(node);
        }

It will try to reconnect afterwards, so make sure your configuration is right.之后它将尝试重新连接,因此请确保您的配置正确。
Actually, you can catch exception produced by set method:实际上,您可以捕获set方法产生的异常:

    OperationFuture<Boolean> future = client.set("name", 1000, "some_name");
    Boolean result = future.get(50, TimeUnit.MILLISECONDS);

Assume that your timeout time for one operation is 50 milliseconds, you will get CheckedOperationTimeoutException after the time elapsed.假设您的一项操作的超时时间为 50 毫秒,您将在时间过去后得到CheckedOperationTimeoutException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM