2011-12-04 45 views
4

我正在使用Spymemcached訪問服務器上的Memcached。我開始使用以下命令Spymemcached掛起操作完成後

memcached -m 4096 -p 11211 -u memcache -l 127.0.0.1 -d -vvv 

Memcached和我使用下面的驅動程序把一些鍵/值對的內存緩存。由於某種原因,在最後的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 [email protected] 
***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功能永遠不會終止?有沒有人有什麼建議?

回答

8

解決方案很簡單。當它完成客戶端必須斷開:

c.shutdown(...)

的例子包括沒有這一關鍵的一步。