-
Notifications
You must be signed in to change notification settings - Fork 489
/
Copy pathrtmama.java
73 lines (61 loc) · 2.21 KB
/
rtmama.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package guide;
import org.zeromq.SocketType;
import org.zeromq.ZMQ;
import org.zeromq.ZContext;
//
//Custom routing Router to Mama (ROUTER to REQ)
//
public class rtmama
{
private static final int NBR_WORKERS = 10;
public static class Worker implements Runnable
{
private final byte[] END = "END".getBytes(ZMQ.CHARSET);
public void run()
{
try (ZContext context = new ZContext()) {
ZMQ.Socket worker = context.createSocket(SocketType.REQ);
// worker.setIdentity(); will set a random id automatically
worker.connect("ipc://routing.ipc");
int total = 0;
while (true) {
worker.send("ready", 0);
byte[] workerload = worker.recv(0);
if (new String(workerload, ZMQ.CHARSET).equals("END")) {
System.out.printf("Processs %d tasks.%n", total);
break;
}
total += 1;
}
}
}
}
public static void main(String[] args)
{
try (ZContext context = new ZContext()) {
ZMQ.Socket client = context.createSocket(SocketType.ROUTER);
client.bind("ipc://routing.ipc");
for (int i = 0; i != NBR_WORKERS; i++) {
new Thread(new Worker()).start();
}
for (int i = 0; i != NBR_WORKERS; i++) {
// LRU worker is next waiting in queue
byte[] address = client.recv(0);
byte[] empty = client.recv(0);
byte[] ready = client.recv(0);
client.send(address, ZMQ.SNDMORE);
client.send("", ZMQ.SNDMORE);
client.send("This is the workload", 0);
}
for (int i = 0; i != NBR_WORKERS; i++) {
// LRU worker is next waiting in queue
byte[] address = client.recv(0);
byte[] empty = client.recv(0);
byte[] ready = client.recv(0);
client.send(address, ZMQ.SNDMORE);
client.send("", ZMQ.SNDMORE);
client.send("END", 0);
}
}
}
}