Spymemcached 操作完成后挂起
我正在使用 Spymemcached 访问服务器上的 Memcached。我使用以下命令启动 memcached
memcached -m 4096 -p 11211 -u memcache -l 127.0.0.1 -d -vvv
,并使用以下驱动程序将一些键/值对放入 Memcache 中。由于某种原因,在最后一次 get
操作之后,main
函数就挂起了;它永远不会终止。
import java.net.InetSocketAddress;
import net.spy.memcached.MemcachedClient;
public class memcache {
public static void main(String args[]) {
//Connect to Memcache.
MemcachedClient c=null;
try{
c = new MemcachedClient(
new InetSocketAddress("127.0.0.1", 11211));
} catch (Exception e) {
System.err.println("Could not connect to Memcached.");
System.exit(-1);
}
System.out.println("Connected to Memcached.");
Object myObject = null;
for (int i = 0; i < 10; i++)
{
c.set(new String("HELLO"), 3600, i+1);
myObject=c.get("HELLO");
String value = myObject.toString();
System.out.println("***" + value);
}
System.out.println("Done.");
return;
}
}
我得到以下输出,但应用程序永远不会终止。
2011-12-04 14:18:31.839 INFO net.spy.memcached.MemcachedConnection: Added {QA sa=/127.0.0.1:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
Connected to Memcached.
2011-12-04 14:18:31.844 INFO net.spy.memcached.MemcachedConnection: Connection state changed for sun.nio.ch.SelectionKeyImpl@578088c0
***1
Done.
服务器报告以下状态:
<32 server listening (udp)
<33 new auto-negotiating client connection
33: going from conn_new_cmd to conn_waiting
33: going from conn_waiting to conn_read
33: going from conn_read to conn_parse_cmd
33: Client using the ascii protocol
<33 set HELLO 512 3600 1
33: going from conn_parse_cmd to conn_nread
> NOT FOUND HELLO
>33 STORED
33: going from conn_nread to conn_write
33: going from conn_write to conn_new_cmd
33: going from conn_new_cmd to conn_waiting
33: going from conn_waiting to conn_read
33: going from conn_read to conn_parse_cmd
<33 get HELLO
> FOUND KEY HELLO
>33 sending key HELLO
>33 END
33: going from conn_parse_cmd to conn_mwrite
33: going from conn_mwrite to conn_new_cmd
33: going from conn_new_cmd to conn_waiting
33: going from conn_waiting to conn_read
什么问题可能导致 main
函数永远不会终止?有人有什么建议吗?
I am using Spymemcached to access Memcached on a server. I started memcached using the following command
memcached -m 4096 -p 11211 -u memcache -l 127.0.0.1 -d -vvv
and I am using the following driver to put some key/value pairs into Memcache. For some reason, after the last get
operation, the main
function just hangs; it never terminates.
import java.net.InetSocketAddress;
import net.spy.memcached.MemcachedClient;
public class memcache {
public static void main(String args[]) {
//Connect to Memcache.
MemcachedClient c=null;
try{
c = new MemcachedClient(
new InetSocketAddress("127.0.0.1", 11211));
} catch (Exception e) {
System.err.println("Could not connect to Memcached.");
System.exit(-1);
}
System.out.println("Connected to Memcached.");
Object myObject = null;
for (int i = 0; i < 10; i++)
{
c.set(new String("HELLO"), 3600, i+1);
myObject=c.get("HELLO");
String value = myObject.toString();
System.out.println("***" + value);
}
System.out.println("Done.");
return;
}
}
I get the following output, but the application never terminates.
2011-12-04 14:18:31.839 INFO net.spy.memcached.MemcachedConnection: Added {QA sa=/127.0.0.1:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
Connected to Memcached.
2011-12-04 14:18:31.844 INFO net.spy.memcached.MemcachedConnection: Connection state changed for sun.nio.ch.SelectionKeyImpl@578088c0
***1
Done.
The server is reporting the following status:
<32 server listening (udp)
<33 new auto-negotiating client connection
33: going from conn_new_cmd to conn_waiting
33: going from conn_waiting to conn_read
33: going from conn_read to conn_parse_cmd
33: Client using the ascii protocol
<33 set HELLO 512 3600 1
33: going from conn_parse_cmd to conn_nread
> NOT FOUND HELLO
>33 STORED
33: going from conn_nread to conn_write
33: going from conn_write to conn_new_cmd
33: going from conn_new_cmd to conn_waiting
33: going from conn_waiting to conn_read
33: going from conn_read to conn_parse_cmd
<33 get HELLO
> FOUND KEY HELLO
>33 sending key HELLO
>33 END
33: going from conn_parse_cmd to conn_mwrite
33: going from conn_mwrite to conn_new_cmd
33: going from conn_new_cmd to conn_waiting
33: going from conn_waiting to conn_read
What problem might be causing the main
function to never terminate? Does anyone have any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案很简单。完成后客户端必须断开连接:
c.shutdown(...)
所有示例均不包含此关键步骤。
The solution was easy. The client must disconnect when it is done:
c.shutdown(...)
None of the examples include this critical step.