Search in sources :

Example 21 with Poller

use of org.zeromq.ZMQ.Poller in project jeromq by zeromq.

the class lbbroker method main.

/**
     * This is the main task. It starts the clients and workers, and then
     * routes requests between the two layers. Workers signal READY when
     * they start; after that we treat them as ready when they reply with
     * a response back to a client. The load-balancing data structure is
     * just a queue of next available workers.
     */
public static void main(String[] args) {
    Context context = ZMQ.context(1);
    //  Prepare our context and sockets
    Socket frontend = context.socket(ZMQ.ROUTER);
    Socket backend = context.socket(ZMQ.ROUTER);
    frontend.bind("ipc://frontend.ipc");
    backend.bind("ipc://backend.ipc");
    int clientNbr;
    for (clientNbr = 0; clientNbr < NBR_CLIENTS; clientNbr++) new ClientTask().start();
    for (int workerNbr = 0; workerNbr < NBR_WORKERS; workerNbr++) new WorkerTask().start();
    //  Here is the main loop for the least-recently-used queue. It has two
    //  sockets; a frontend for clients and a backend for workers. It polls
    //  the backend in all cases, and polls the frontend only when there are
    //  one or more workers ready. This is a neat way to use 0MQ's own queues
    //  to hold messages we're not ready to process yet. When we get a client
    //  reply, we pop the next available worker, and send the request to it,
    //  including the originating client identity. When a worker replies, we
    //  re-queue that worker, and we forward the reply to the original client,
    //  using the reply envelope.
    //  Queue of available workers
    Queue<String> workerQueue = new LinkedList<String>();
    while (!Thread.currentThread().isInterrupted()) {
        //  Initialize poll set
        Poller items = context.poller(2);
        //  Always poll for worker activity on backend
        items.register(backend, Poller.POLLIN);
        //  Poll front-end only if we have available workers
        if (workerQueue.size() > 0)
            items.register(frontend, Poller.POLLIN);
        if (items.poll() < 0)
            //  Interrupted
            break;
        //  Handle worker activity on backend
        if (items.pollin(0)) {
            //  Queue worker address for LRU routing
            workerQueue.add(backend.recvStr());
            //  Second frame is empty
            String empty = backend.recvStr();
            assert (empty.length() == 0);
            //  Third frame is READY or else a client reply address
            String clientAddr = backend.recvStr();
            //  If client reply, send rest back to frontend
            if (!clientAddr.equals("READY")) {
                empty = backend.recvStr();
                assert (empty.length() == 0);
                String reply = backend.recvStr();
                frontend.sendMore(clientAddr);
                frontend.sendMore("");
                frontend.send(reply);
                if (--clientNbr == 0)
                    break;
            }
        }
        if (items.pollin(1)) {
            //  Now get next client request, route to LRU worker
            //  Client request is [address][empty][request]
            String clientAddr = frontend.recvStr();
            String empty = frontend.recvStr();
            assert (empty.length() == 0);
            String request = frontend.recvStr();
            String workerAddr = workerQueue.poll();
            backend.sendMore(workerAddr);
            backend.sendMore("");
            backend.sendMore(clientAddr);
            backend.sendMore("");
            backend.send(request);
        }
    }
    frontend.close();
    backend.close();
    context.term();
}
Also used : Context(org.zeromq.ZMQ.Context) Socket(org.zeromq.ZMQ.Socket) LinkedList(java.util.LinkedList) Poller(org.zeromq.ZMQ.Poller)

Example 22 with Poller

use of org.zeromq.ZMQ.Poller in project jeromq by zeromq.

the class flclient1 method tryRequest.

private static ZMsg tryRequest(ZContext ctx, String endpoint, ZMsg request) {
    System.out.printf("I: trying echo service at %s...\n", endpoint);
    Socket client = ctx.createSocket(ZMQ.REQ);
    client.connect(endpoint);
    //  Send request, wait safely for reply
    ZMsg msg = request.duplicate();
    msg.send(client);
    Poller poller = ctx.createPoller(1);
    poller.register(client, Poller.POLLIN);
    poller.poll(REQUEST_TIMEOUT);
    ZMsg reply = null;
    if (poller.pollin(0))
        reply = ZMsg.recvMsg(client);
    //  Close socket in any case, we're done with it now
    ctx.destroySocket(client);
    return reply;
}
Also used : ZMsg(org.zeromq.ZMsg) Socket(org.zeromq.ZMQ.Socket) Poller(org.zeromq.ZMQ.Poller)

Aggregations

Poller (org.zeromq.ZMQ.Poller)22 Socket (org.zeromq.ZMQ.Socket)20 ZContext (org.zeromq.ZContext)17 ZMsg (org.zeromq.ZMsg)10 Random (java.util.Random)7 ZFrame (org.zeromq.ZFrame)7 ArrayList (java.util.ArrayList)4 IOException (java.io.IOException)2 ByteBuffer (java.nio.ByteBuffer)2 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 Entry (java.util.Map.Entry)2 Context (org.zeromq.ZMQ.Context)2 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 FileWriter (java.io.FileWriter)1 RandomAccessFile (java.io.RandomAccessFile)1