[zeromq-dev] pub-sub losing messages
Michael Keselman
michael.keselman at gmail.com
Tue May 21 18:50:23 CEST 2013
I am playing ZMQ using Java. I created very basic pub/sub application (see
below) with subscriber emulating slow response via random delay.
Unfortunately something isn't working. Whether I run multiple subscribers
or only one, they have never received all messages. I thought that setting
high water mark would help, but it didn't. Am I doing something wrong or
missing some nuance?
-----Pub-----
import org.zeromq.ZMQ;
public class Pub {
public static void main(String[] args) {
ZMQ.Context context = ZMQ.context(1);
ZMQ.Socket publisher = context.socket(ZMQ.PUB);
publisher.setHWM(1000);
publisher.bind("tcp://*:5555");
long counter = 0;
String msg;
for (int i=0; i<100000; i++){
msg = Long.toString(counter)+"*******";
publisher.send(msg , 0);
System.out.println(msg);
counter++;
}
publisher.close ();
context.term ();
}
}
-----Sub-----
import org.zeromq.ZMQ;
import java.util.Random;
public class sub {
public static void main(String[] args) throws Exception{
Random rand = new Random();
ZMQ.Context context = ZMQ.context(1);
ZMQ.Socket subscriber = context.socket(ZMQ.SUB);
subscriber.connect("tcp://localhost:5555");
subscriber.subscribe("".getBytes());
String msg;
while (1>0){
msg = subscriber.recvStr(0);
System.out.println(msg);
Thread.sleep(rand.nextInt(3));
}
//subscriber.close();
//context.term();
}
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20130521/6dc7f7d8/attachment.htm>
More information about the zeromq-dev
mailing list