Permalink
Browse files

chapter 4 java examples

  • Loading branch information...
1 parent a94d57a commit 1195b8872d5dd7a7409a262bc59924496e005a7d @miniway miniway committed Mar 24, 2013
View
@@ -1,6 +1,4 @@
/**
- * (c) 2011 Arkadiusz Orzechowski
- *
* This file is part of ZGuide
*
* ZGuide is free software; you can redistribute it and/or modify it under
@@ -22,8 +20,6 @@
/**
* Majordomo Protocol definitions, Java version
- *
- * @author Arkadiusz Orzechowski <aorzecho@gmail.com>
*/
public enum MDP {
View
@@ -1,11 +1,16 @@
+import org.zeromq.ZMQ;
+import org.zeromq.ZMQ.Context;
import org.zeromq.ZMQ.Socket;
+import java.math.BigInteger;
+import java.util.Arrays;
+import java.util.List;
import java.util.Random;
public class ZHelper
{
private static Random rand = new Random(System.currentTimeMillis ());
-
+
/**
* Receives all message parts from socket, prints neatly
*/
@@ -36,21 +41,20 @@ public static void setId (Socket sock)
sock.setIdentity (identity.getBytes ());
}
-
-
- public static List<Socket> buildZPipe(Context ctx) {
- Socket socket1 = ctx.socket(ZMQ.PAIR);
- socket1.setLinger(0);
- socket1.setHWM(1);
-
- Socket socket2 = ctx.socket(ZMQ.PAIR);
- socket2.setLinger(0);
- socket2.setHWM(1);
-
- String iface = "inproc://" + new BigInteger(130, rand).toString(32);
- socket1.bind(iface);
- socket2.connect(iface);
-
- return Arrays.asList(socket1, socket2);
- }
+
+ public static List<Socket> buildZPipe(Context ctx) {
+ Socket socket1 = ctx.socket(ZMQ.PAIR);
+ socket1.setLinger(0);
+ socket1.setHWM(1);
+
+ Socket socket2 = ctx.socket(ZMQ.PAIR);
+ socket2.setLinger(0);
+ socket2.setHWM(1);
+
+ String iface = "inproc://" + new BigInteger(130, rand).toString(32);
+ socket1.bind(iface);
+ socket2.connect(iface);
+
+ return Arrays.asList(socket1, socket2);
+ }
}
@@ -1,12 +1,12 @@
-import java.util.Random;
-
import org.zeromq.ZContext;
import org.zeromq.ZFrame;
import org.zeromq.ZMQ;
-import org.zeromq.ZMQ.Poller;
-import org.zeromq.ZMsg;
import org.zeromq.ZMQ.PollItem;
import org.zeromq.ZMQ.Socket;
+import org.zeromq.ZMsg;
+import org.zeromq.ZMQ.Poller;
+
+import java.util.Random;
//
//Asynchronous client-to-server (DEALER to ROUTER)
@@ -26,11 +26,9 @@
private static Random rand = new Random(System.nanoTime());
- private static class client_task implements Runnable
- {
+ private static class client_task implements Runnable {
- public void run()
- {
+ public void run() {
ZContext ctx = new ZContext();
Socket client = ctx.createSocket(ZMQ.DEALER);
@@ -64,10 +62,8 @@ public void run()
//one request at a time but one client can talk to multiple workers at
//once.
- private static class server_task implements Runnable
- {
- public void run()
- {
+ private static class server_task implements Runnable {
+ public void run() {
ZContext ctx = new ZContext();
// Frontend socket talks to clients over TCP
@@ -92,17 +88,14 @@ public void run()
//Each worker task works on one request at a time and sends a random number
//of replies back, with random delays between replies:
- private static class server_worker implements Runnable
- {
+ private static class server_worker implements Runnable {
private ZContext ctx;
- public server_worker(ZContext ctx)
- {
+ public server_worker(ZContext ctx) {
this.ctx = ctx;
}
- public void run()
- {
+ public void run() {
Socket worker = ctx.createSocket(ZMQ.DEALER);
worker.connect("inproc://backend");
@@ -135,8 +128,7 @@ public void run()
//The main thread simply starts several clients, and a server, and then
//waits for the server to finish.
- public static void main(String[] args) throws Exception
- {
+ public static void main(String[] args) throws Exception {
ZContext ctx = new ZContext();
new Thread(new client_task()).start();
new Thread(new client_task()).start();
Oops, something went wrong.

0 comments on commit 1195b88

Please sign in to comment.