jeromq
jeromq copied to clipboard
Sending messages at high frequency will lose a lot of messages
Hi, I read some similar issues, but there is no same problem as me.
Issues: The Pub server sends 10000 messages and the Sub client only receives 6873.
For Example:
client receive log:
this is 1024 byte message 0
this is 1024 byte message 1
......
this is 1024 byte message 6872
this is 1024 byte message 6873
All messages between 6873 and 10000 have been lost
Env: OS: Windows 10; Message length: 1024 byte ZMQ version: 0.5.2
Step: 1、open sub client first 2、open pub server then 3、sleep for 3 seconds before sending messages cyclically
Server Code:
public class ZeroMqPubClass {
public static void main(String[] args) {
ZMQ.Context context = ZMQ.context(1);
ZMQ.Socket publisher = context.socket(SocketType.PUB);
publisher.bind("tcp://*:5511");
int a = 0;
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
String message = "this is 1024 byte message";
// publisher.setSndHWM(0);
// publisher.setXpubNoDrop(true);
while (a <= 10000) {
String push = message + " " + (a++);
publisher.send(push,0);
}
publisher.close();
context.term();
}
}
Client Code:
public static void main(String[] args) {
new Thread(new Runnable(){
public void run() {
ZMQ.Context context = ZMQ.context(1);
ZMQ.Socket subscriber = context.socket(SocketType.SUB);
subscriber.connect("tcp://localhost:5511");
subscriber.subscribe("".getBytes());
// subscriber.setRcvHWM(0);
while(true){
byte[] message = subscriber.recv(0);
System.out.println(new String(message,StandardCharsets.UTF_8));
}
}
}).start();
}
}
Result: the Sub client only receives 6873.
Can you try to subscribe before connecting?
Can you try to subscribe before connecting?
Thank you for your reply. I open the Sub Client first, then the Pub Server, and the Pub Server sleeps for 3 seconds, so that the Sub Client and the Pub Server have sufficient time to connect, and then the Pub Server pushes messages. Is there any problem with my step?