2
我預計這個程序中的線程工作在鎖定步驟。但是,在等待接收方趕上之前,發送方會發送〜60000條消息。我對HWM有什麼誤解?jzmq似乎忽略了XREP上的HWM
如果我沒有啓動接收者線程,那麼發送者在嘗試發送第二條消息時會阻塞。
public static void main(String[] args) throws Exception {
new Thread() {
@Override
public void run() {
ZMQ.Context context = ZMQ.context(1);
ZMQ.Socket socket = context.socket(ZMQ.XREP);
socket.setHWM(1);
socket.bind("tcp://127.0.0.1:8080");
while (true) {
try {
socket.recv(0);
byte[] msg = socket.recv(0);
System.out.println("Received: " + new String(msg, "UTF-8"));
Thread.sleep(1000);
} catch (Exception ex) {
Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}.start();
new Thread() {
@Override
public void run() {
ZMQ.Context context = ZMQ.context(1);
ZMQ.Socket socket = context.socket(ZMQ.XREQ);
socket.setHWM(1);
socket.connect("tcp://127.0.0.1:8080");
Integer i = 1;
while (true) {
System.out.println("Sending: " + i);
socket.send(i.toString().getBytes(), 0);
i++;
}
}
}.start();
}