forked from booksbyus/zguide
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request booksbyus#180 from acogoluegnes/java-mthwserver
Added Java-based multithreaded service sample
- Loading branch information
Showing
1 changed file
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |