[Req-broker-rep]

Source: Internet
Author: User
[Req-broker-rep] mode: for requests from multiple clients, the broker distributes multiple tasks to the workers one by one, so that requests from multiple clients can be sent to the broker concurrently. Worker concurrent execution operations. The broker in the middle returns the worker result to the corresponding client at any time.

Broker program:

Package guide; import Java. NIO. charset. charset; import Java. text. simpledateformat; import Java. util. calendar; import Org. zeromq. zmq; import Org. zeromq. zmq. context; import Org. zeromq. zmq. poller; import Org. zeromq. zmq. socket;/*** simple request-reply broker **/public class rrbroker {public static void main (string [] ARGs) {// prepare our context and sockets context = zmq. context (1); socket frontend = Context. socket (zmq. router); socket backend = context. socket (zmq. dealer); frontend. BIND ("TCP: // *: 5559"); backend. BIND ("TCP: // *: 5560"); system. out. println ("launch and connect broker. "); // initialize poll set poller items = new poller (2); items. register (frontend, poller. pollin); items. register (backend, poller. pollin); Boolean more = false; byte [] Message; // switch messages between sockets while (! Thread. currentthread (). isinterrupted () {// poll and memorize multipart detection items. poll (); If (items. pollin (0) {While (true) {// receive message = frontend. recv (0); More = frontend. hasreceivemore (); // broker it backend. send (message, more? Zmq. sndmore: 0); string str_msg = ""; if (message! = NULL) {str_msg = new string (message, charset. forname ("UTF-8");} system. out. println (getcurrtime () + ": routed a req. MSG ["+ str_msg +"] More ["+ more +"] "); If (! More) {break ;}}if (items. pollin (1) {While (true) {// receive message = backend. recv (0); More = backend. hasreceivemore (); // broker it frontend. send (message, more? Zmq. sndmore: 0); system. Out. println (getcurrtime () + ": Got a rep."); If (! More) {break ;}}}// we never get here but clean up anyhow frontend. close (); backend. close (); context. term ();} public static string getcurrtime () {calendar Cal = calendar. getinstance (); Cal. gettime (); simpledateformat SDF = new simpledateformat ("mm: Ss. SS "); Return SDF. format (Cal. gettime () ;}}// The following is the client program: Package guide; import Org. zeromq. zmq; import Org. zeromq. zmq. context; import Org. zeromq. zmq. socket;/*** Hello World client * connects req socket to TCP: // localhost: 5559 * Sends "hello" to server, expects "world" back */public class rrclient {public static void main (string [] ARGs) {context = zmq. context (1); // socket to talk to server socket requester = context. socket (zmq. REQ); requester. connect ("TCP: // localhost: 5559"); system. out. println ("launch and connect client. "); For (INT request_nbr = 0; request_nbr <20; request_nbr ++) {requester. send ("hello", 0); string reply = requester. recvstr (0); system. out. println ("replicated ed reply" + request_nbr + "[" + reply + "]");} // we never get here but clean up anyhow requester. close (); context. term ();}}
// Worker Program: Package guide; import Org. zeromq. zmq; import Org. zeromq. zmq. context; import Org. zeromq. zmq. socket; // Hello world worker // connects rep socket to TCP: // *: 5560 // expects "hello" from client, replies with "world" public class rrworker {public static void main (string [] ARGs) throws exception {context = zmq. context (1); // socket to talk to server socket responder = context. socket (zmq. Rep); responder. Connect ("TCP: // localhost: 5560"); While (! Thread. currentthread (). isinterrupted () {// wait for next request from client string = responder. recvstr (0); system. out. printf ("received request: [% s] \ n", string); // do some 'work' thread. sleep (1000); // send reply back to client responder. send ("world");} // we never get here but clean up anyhow responder. close (); context. term ();}}

 

[Req-broker-rep]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.