Search in sources :

Example 1 with ZMsg

use of org.zeromq.ZMsg in project jeromq by zeromq.

the class lbbroker2 method main.

/**
     * This is the main task. This has the identical functionality to
     * the previous lbbroker example but uses higher level classes to start child threads
     * to hold the list of workers, and to read and send messages:
     */
public static void main(String[] args) {
    ZContext context = new ZContext();
    //  Prepare our context and sockets
    Socket frontend = context.createSocket(ZMQ.ROUTER);
    Socket backend = context.createSocket(ZMQ.ROUTER);
    frontend.bind("ipc://frontend.ipc");
    backend.bind("ipc://backend.ipc");
    int clientNbr;
    for (clientNbr = 0; clientNbr < NBR_CLIENTS; clientNbr++) ZThread.start(new ClientTask());
    for (int workerNbr = 0; workerNbr < NBR_WORKERS; workerNbr++) ZThread.start(new WorkerTask());
    //  Queue of available workers
    Queue<ZFrame> workerQueue = new LinkedList<ZFrame>();
    while (!Thread.currentThread().isInterrupted()) {
        //  Initialize poll set
        Poller items = context.createPoller(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)) {
            ZMsg msg = ZMsg.recvMsg(backend);
            if (msg == null)
                //  Interrupted
                break;
            ZFrame identity = msg.unwrap();
            //  Queue worker address for LRU routing
            workerQueue.add(identity);
            //  Forward message to client if it's not a READY
            ZFrame frame = msg.getFirst();
            if (Arrays.equals(frame.getData(), WORKER_READY))
                msg.destroy();
            else
                msg.send(frontend);
        }
        if (items.pollin(1)) {
            //  Get client request, route to first available worker
            ZMsg msg = ZMsg.recvMsg(frontend);
            if (msg != null) {
                msg.wrap(workerQueue.poll());
                msg.send(backend);
            }
        }
    }
    context.destroy();
}
Also used : ZFrame(org.zeromq.ZFrame) ZContext(org.zeromq.ZContext) ZMsg(org.zeromq.ZMsg) Socket(org.zeromq.ZMQ.Socket) LinkedList(java.util.LinkedList) Poller(org.zeromq.ZMQ.Poller)

Example 2 with ZMsg

use of org.zeromq.ZMsg in project jeromq by zeromq.

the class mdcliapi method send.

/**
     * Send request to broker and get reply by hook or crook Takes ownership of
     * request message and destroys it when sent. Returns the reply message or
     * NULL if there was no reply.
     *
     * @param service
     * @param request
     * @return
     */
public ZMsg send(String service, ZMsg request) {
    request.push(new ZFrame(service));
    request.push(MDP.C_CLIENT.newFrame());
    if (verbose) {
        log.format("I: send request to '%s' service: \n", service);
        request.dump(log.out());
    }
    ZMsg reply = null;
    int retriesLeft = retries;
    while (retriesLeft > 0 && !Thread.currentThread().isInterrupted()) {
        request.duplicate().send(client);
        // Poll socket for a reply, with timeout
        ZMQ.Poller items = ctx.createPoller(1);
        items.register(client, ZMQ.Poller.POLLIN);
        if (items.poll(timeout) == -1)
            // Interrupted
            break;
        if (items.pollin(0)) {
            ZMsg msg = ZMsg.recvMsg(client);
            if (verbose) {
                log.format("I: received reply: \n");
                msg.dump(log.out());
            }
            // Don't try to handle errors, just assert noisily
            assert (msg.size() >= 3);
            ZFrame header = msg.pop();
            assert (MDP.C_CLIENT.equals(header.toString()));
            header.destroy();
            ZFrame replyService = msg.pop();
            assert (service.equals(replyService.toString()));
            replyService.destroy();
            reply = msg;
            break;
        } else {
            items.unregister(client);
            if (--retriesLeft == 0) {
                log.format("W: permanent error, abandoning\n");
                break;
            }
            log.format("W: no reply, reconnecting\n");
            reconnectToBroker();
        }
    }
    request.destroy();
    return reply;
}
Also used : ZFrame(org.zeromq.ZFrame) ZMsg(org.zeromq.ZMsg) ZMQ(org.zeromq.ZMQ)

Example 3 with ZMsg

use of org.zeromq.ZMsg in project jeromq by zeromq.

the class flcliapi method connect.

//  .split connect method
//  To implement the connect method, the frontend object sends a multipart
//  message to the backend agent. The first part is a string "CONNECT", and
//  the second part is the endpoint. It waits 100msec for the connection to
//  come up, which isn't pretty, but saves us from sending all requests to a
//  single server, at startup time:
public void connect(String endpoint) {
    ZMsg msg = new ZMsg();
    msg.add("CONNECT");
    msg.add(endpoint);
    msg.send(pipe);
    try {
        //  Allow connection to come up
        Thread.sleep(100);
    } catch (InterruptedException e) {
    }
}
Also used : ZMsg(org.zeromq.ZMsg)

Example 4 with ZMsg

use of org.zeromq.ZMsg in project jeromq by zeromq.

the class flclient2 method request.

private ZMsg request(ZMsg request) {
    //  Prefix request with getSequence number and empty envelope
    String sequenceText = String.format("%d", ++sequence);
    request.push(sequenceText);
    request.push("");
    //  Blast the request to all connected servers
    int server;
    for (server = 0; server < servers; server++) {
        ZMsg msg = request.duplicate();
        msg.send(socket);
    }
    //  Wait for a matching reply to arrive from anywhere
    //  Since we can poll several times, calculate each one
    ZMsg reply = null;
    long endtime = System.currentTimeMillis() + GLOBAL_TIMEOUT;
    Poller poller = ctx.createPoller(1);
    poller.register(socket, Poller.POLLIN);
    while (System.currentTimeMillis() < endtime) {
        poller.poll(endtime - System.currentTimeMillis());
        if (poller.pollin(0)) {
            //  Reply is [empty][getSequence][OK]
            reply = ZMsg.recvMsg(socket);
            assert (reply.size() == 3);
            reply.pop();
            String sequenceStr = reply.popString();
            int sequenceNbr = Integer.parseInt(sequenceStr);
            if (sequenceNbr == sequence)
                break;
            reply.destroy();
        }
    }
    request.destroy();
    return reply;
}
Also used : ZMsg(org.zeromq.ZMsg) Poller(org.zeromq.ZMQ.Poller)

Example 5 with ZMsg

use of org.zeromq.ZMsg in project jeromq by zeromq.

the class ppqueue method main.

//  The main task is an LRU queue with heartbeating on workers so we can
//  detect crashed or blocked worker tasks:
public static void main(String[] args) {
    ZContext ctx = new ZContext();
    Socket frontend = ctx.createSocket(ZMQ.ROUTER);
    Socket backend = ctx.createSocket(ZMQ.ROUTER);
    //  For clients
    frontend.bind("tcp://*:5555");
    //  For workers
    backend.bind("tcp://*:5556");
    //  List of available workers
    ArrayList<Worker> workers = new ArrayList<Worker>();
    //  Send out heartbeats at regular intervals
    long heartbeat_at = System.currentTimeMillis() + HEARTBEAT_INTERVAL;
    Poller poller = ctx.createPoller(2);
    poller.register(backend, Poller.POLLIN);
    poller.register(frontend, Poller.POLLIN);
    while (true) {
        boolean workersAvailable = workers.size() > 0;
        int rc = poller.poll(HEARTBEAT_INTERVAL);
        if (rc == -1)
            //  Interrupted
            break;
        //  Handle worker activity on backend
        if (poller.pollin(0)) {
            //  Use worker address for LRU routing
            ZMsg msg = ZMsg.recvMsg(backend);
            if (msg == null)
                //  Interrupted
                break;
            //  Any sign of life from worker means it's ready
            ZFrame address = msg.unwrap();
            Worker worker = new Worker(address);
            worker.ready(workers);
            //  Validate control message, or return reply to client
            if (msg.size() == 1) {
                ZFrame frame = msg.getFirst();
                String data = new String(frame.getData(), ZMQ.CHARSET);
                if (!data.equals(PPP_READY) && !data.equals(PPP_HEARTBEAT)) {
                    System.out.println("E: invalid message from worker");
                    msg.dump(System.out);
                }
                msg.destroy();
            } else
                msg.send(frontend);
        }
        if (workersAvailable && poller.pollin(1)) {
            //  Now get next client request, route to next worker
            ZMsg msg = ZMsg.recvMsg(frontend);
            if (msg == null)
                //  Interrupted
                break;
            msg.push(Worker.next(workers));
            msg.send(backend);
        }
        if (System.currentTimeMillis() >= heartbeat_at) {
            for (Worker worker : workers) {
                worker.address.send(backend, ZFrame.REUSE + ZFrame.MORE);
                ZFrame frame = new ZFrame(PPP_HEARTBEAT);
                frame.send(backend, 0);
            }
            heartbeat_at = System.currentTimeMillis() + HEARTBEAT_INTERVAL;
        }
        Worker.purge(workers);
    }
    //  When we're done, clean up properly
    while (workers.size() > 0) {
        Worker worker = workers.remove(0);
    }
    workers.clear();
    ctx.destroy();
}
Also used : ZFrame(org.zeromq.ZFrame) ArrayList(java.util.ArrayList) ZContext(org.zeromq.ZContext) ZMsg(org.zeromq.ZMsg) Socket(org.zeromq.ZMQ.Socket) Poller(org.zeromq.ZMQ.Poller)

Aggregations

ZMsg (org.zeromq.ZMsg)38 ZFrame (org.zeromq.ZFrame)20 ZContext (org.zeromq.ZContext)14 Socket (org.zeromq.ZMQ.Socket)14 Poller (org.zeromq.ZMQ.Poller)10 ArrayList (java.util.ArrayList)4 Random (java.util.Random)4 ZMQ (org.zeromq.ZMQ)4 IOException (java.io.IOException)3 File (java.io.File)2 RandomAccessFile (java.io.RandomAccessFile)2 BufferedWriter (java.io.BufferedWriter)1 DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 FileWriter (java.io.FileWriter)1 LinkedList (java.util.LinkedList)1 PollItem (org.zeromq.ZMQ.PollItem)1