我查看了ZMQ指南中給出的幾個示例,但由於某種原因,子訂閱者不會收到任何消息。這是我試圖測試的代碼,但徒勞無功。請爲此提出修正 -ZeroMQ/JZMQ - 用戶沒有收到任何消息
public class SyncPubTest {
protected static int SUBSCRIBERS_EXPECTED = 2;
public static void main (String[] args) {
Context context = ZMQ.context(1);
Socket publisher = context.socket(ZMQ.PUB);
publisher.bind("tcp://*:5561");
try {
//zmq??
Thread.sleep (1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
int update_nbr;
for (update_nbr = 0; update_nbr < 10; update_nbr++){
publisher.send("Rhubarb".getBytes(), ZMQ.NOBLOCK);
}
publisher.send("END".getBytes(), 0);
// publisher.close();
//context.term();
}
}
public class SyncSubTest {
public static void main(String[] args) {
Context context = ZMQ.context(1);
Socket subscriber = context.socket(ZMQ.SUB);
subscriber.connect("tcp://localhost:5561");
subscriber.subscribe("".getBytes());
int update_nbr = 0;
while (true) {
byte[] stringValue = subscriber.recv(0);
String string = new String(stringValue);
if (string.equals("END")) {
break;
}
update_nbr++;
System.out.println("Received " + update_nbr + " updates. :" + string);
}
//subscriber.close();
//context.term();
}
}
For some reason, context.term() hangs even all the sockets created in the context are closed.
請幫我解決這個問題。謝謝!!!
由於下面的鏈接!首先運行用戶後,它會打印第一個字符「R」,但不打印整個字符串。代碼中出現錯誤? – user2508012
其實我可以解決這個問題。似乎這是舊版本中的一個錯誤。我將JAR文件更新爲jzmq-2.1.3.jar,現在它顯示了預期的結果。非常感謝你的幫助!! – user2508012