I am using spymemcached-2.8.4.jar and 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,
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 :
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:
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.