Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #180 from acogoluegnes/java-mthwserver

Added Java-based multithreaded service sample
  • Loading branch information...
commit eae145f48716b71782cadc02747b8d97c365861a 2 parents 3ab9896 + 119c4e7
@hintjens hintjens authored
Showing with 72 additions and 0 deletions.
  1. +72 −0 examples/Java/mthwserver.java
View
72 examples/Java/mthwserver.java
@@ -0,0 +1,72 @@
+//
+// Multithreaded Hello World server in Java
+//
+// Arnaud Cogoluègnes <acogoluegnes@gmail.com>
+//
+import org.zeromq.ZMQ;
+import org.zeromq.ZMQ.Context;
+import org.zeromq.ZMQQueue;
+
+public class mthwserver {
+
+ private static final class WorkerThread extends Thread {
+
+ private final ZMQ.Context context;
+
+ public WorkerThread(Context context) {
+ super();
+ this.context = context;
+ }
+
+ @Override
+ public void run() {
+ ZMQ.Socket receiver = context.socket(ZMQ.REP);
+ receiver.connect("inproc://workers");
+ while(!Thread.currentThread().isInterrupted()) {
+ byte[] request = receiver.recv (0);
+ // In order to display the 0-terminated string as a String,
+ // we omit the last byte from request
+ System.out.println ("Received request: [" +
+ new String(request,0,request.length-1) // Creates a String from request, minus the last byte
+ + "]");
+
+ // Do some 'work'
+ try {
+ Thread.sleep (1000);
+ }
+ catch(InterruptedException e){
+ e.printStackTrace();
+ }
+
+ // Send reply back to client
+ // We will send a 0-terminated string (C string) back to the client,
+ // so that this server also works with The Guide's C and C++ "Hello World" clients
+ String replyString = "World" + " ";
+ byte[] reply = replyString.getBytes();
+ reply[reply.length-1]=0; //Sets the last byte of the reply to 0
+ receiver.send(reply, 0);
+ }
+ }
+
+ }
+
+ public static void main(String[] args) {
+ // Prepare our context and socket
+ ZMQ.Context context = ZMQ.context(1);
+ // Socket to talk to clients
+ ZMQ.Socket clients = context.socket(ZMQ.ROUTER);
+ clients.bind("tcp://*:5555");
+
+ // Socket to talk to workers
+ ZMQ.Socket workers = context.socket(ZMQ.DEALER);
+ workers.bind("inproc://workers");
+
+ // Launch worker threads
+ for(int threadNb = 0;threadNb < 5; threadNb++) {
+ new WorkerThread(context).start();
+ }
+ // Connect work threads to client threads via a queue
+ ZMQQueue queue = new ZMQQueue(context, clients, workers);
+ new Thread(queue).start();
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.