我检查了 ZMQ 指南中给出的几个示例,但由于某种原因,订阅者没有收到任何消息。这是我要测试的代码,但徒劳无功。请为此提出解决方案 -
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.
请帮我解决这个问题。谢谢!!!