-
Notifications
You must be signed in to change notification settings - Fork 489
/
Copy pathsyncpub.java
54 lines (46 loc) · 1.61 KB
/
syncpub.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package guide;
import org.zeromq.SocketType;
import org.zeromq.ZMQ.Socket;
import org.zeromq.ZContext;
/**
* Synchronized publisher.
*/
public class syncpub
{
/**
* We wait for 10 subscribers
*/
protected static final int SUBSCRIBERS_EXPECTED = 10;
public static void main(String[] args)
{
try (ZContext context = new ZContext()) {
// Socket to talk to clients
Socket publisher = context.createSocket(SocketType.PUB);
publisher.setLinger(5000);
// In 0MQ 3.x pub socket could drop messages if sub can follow the
// generation of pub messages
publisher.setSndHWM(0);
publisher.bind("tcp://*:5561");
// Socket to receive signals
Socket syncservice = context.createSocket(SocketType.REP);
syncservice.bind("tcp://*:5562");
System.out.println("Waiting for subscribers");
// Get synchronization from subscribers
int subscribers = 0;
while (subscribers < SUBSCRIBERS_EXPECTED) {
// - wait for synchronization request
syncservice.recv(0);
// - send synchronization reply
syncservice.send("", 0);
subscribers++;
}
// Now broadcast exactly 1M updates followed by END
System.out.println("Broadcasting messages");
int update_nbr;
for (update_nbr = 0; update_nbr < 1000000; update_nbr++) {
publisher.send("Rhubarb", 0);
}
publisher.send("END", 0);
}
}
}