ZeroMQ(http://www.zeromq.org) 性能非常卓越,设计理念也非常牛,正在看文档中,这里记录一下遇到的一个问题:

服务端和客户端之间使用REQ , REP , 启动之后,不停杀掉Server进程,并重启(模拟故障恢复),多试几次之后,Client就会卡住,此时无论怎样都恢复不了。看tcp连接,发现是ESTABLISHED,而且由于zeromq提供的接口都无法感知网络状况,所以此时client压根不知道已经挂死了…

继续看文档,看看是不是自己写得不对…

public class Server {
	public static void main(String[] args) throws Exception {
		// Prepare our context and sockets
		final ZMQ.Context context = ZMQ.context(1);
		ZMQ.Socket socket = context.socket(ZMQ.REP);
		socket.bind("tcp://*:5555");
 
		while (true) {
			byte[] request = socket.recv(0);
			System.out.println(new String(request));
			while (socket.hasReceiveMore()) {
				request = socket.recv(0);
				System.out.println(new String(request));
			}
			String s = new String(request);
			long r = Long.parseLong(s) + 100;
                       // reply = request+100
			socket.send(String.valueOf(r).getBytes(), 0);
		}
        }
}
public class Client {
	public static void main(String[] args) {
		// Prepare our context and socket
		final ZMQ.Context context = ZMQ.context(1);
		ZMQ.Socket socket = context.socket(ZMQ.REQ);
 
		System.out.println("Connecting to hello world server...");
		socket.connect("tcp://localhost:5555");
 
		while (true) {
			long l = System.nanoTime();
			String s = String.valueOf(l);
			socket.send(s.getBytes(), 0);
			byte[] r = socket.recv(0);
			System.out.println(l + " + 100 = " + new String(r));
               }
       }
}