我查看了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