Search in sources :

Example 16 with ZFrame

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

the class ppworker method worker_socket.

//  Helper function that returns a new configured socket
//  connected to the Paranoid Pirate queue
private static Socket worker_socket(ZContext ctx) {
    Socket worker = ctx.createSocket(ZMQ.DEALER);
    worker.connect("tcp://localhost:5556");
    //  Tell queue we're ready for work
    System.out.println("I: worker ready\n");
    ZFrame frame = new ZFrame(PPP_READY);
    frame.send(worker, 0);
    return worker;
}
Also used : ZFrame(org.zeromq.ZFrame) Socket(org.zeromq.ZMQ.Socket)

Example 17 with ZFrame

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

the class spqueue method main.

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");
    //  Queue of available workers
    ArrayList<ZFrame> workers = new ArrayList<ZFrame>();
    Poller poller = ctx.createPoller(2);
    poller.register(backend, Poller.POLLIN);
    poller.register(frontend, Poller.POLLIN);
    //  The body of this example is exactly the same as lruqueue2.
    while (true) {
        boolean workersAvailable = workers.size() > 0;
        int rc = poller.poll(-1);
        //  Poll frontend only if we have available workers
        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;
            ZFrame address = msg.unwrap();
            workers.add(address);
            //  Forward message to client if it's not a READY
            ZFrame frame = msg.getFirst();
            if (new String(frame.getData(), ZMQ.CHARSET).equals(WORKER_READY))
                msg.destroy();
            else
                msg.send(frontend);
        }
        if (workersAvailable && poller.pollin(1)) {
            //  Get client request, route to first available worker
            ZMsg msg = ZMsg.recvMsg(frontend);
            if (msg != null) {
                msg.wrap(workers.remove(0));
                msg.send(backend);
            }
        }
    }
    //  When we're done, clean up properly
    while (workers.size() > 0) {
        ZFrame frame = workers.remove(0);
        frame.destroy();
    }
    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)

Example 18 with ZFrame

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

the class spworker method main.

public static void main(String[] args) throws Exception {
    ZContext ctx = new ZContext();
    Socket worker = ctx.createSocket(ZMQ.REQ);
    //  Set random identity to make tracing easier
    Random rand = new Random(System.nanoTime());
    String identity = String.format("%04X-%04X", rand.nextInt(0x10000), rand.nextInt(0x10000));
    worker.setIdentity(identity.getBytes(ZMQ.CHARSET));
    worker.connect("tcp://localhost:5556");
    //  Tell broker we're ready for work
    System.out.printf("I: (%s) worker ready\n", identity);
    ZFrame frame = new ZFrame(WORKER_READY);
    frame.send(worker, 0);
    int cycles = 0;
    while (true) {
        ZMsg msg = ZMsg.recvMsg(worker);
        if (msg == null)
            //  Interrupted
            break;
        //  Simulate various problems, after a few cycles
        cycles++;
        if (cycles > 3 && rand.nextInt(5) == 0) {
            System.out.printf("I: (%s) simulating a crash\n", identity);
            msg.destroy();
            break;
        } else if (cycles > 3 && rand.nextInt(5) == 0) {
            System.out.printf("I: (%s) simulating CPU overload\n", identity);
            Thread.sleep(3000);
        }
        System.out.printf("I: (%s) normal reply\n", identity);
        //  Do some heavy work
        Thread.sleep(1000);
        msg.send(worker);
    }
    ctx.destroy();
}
Also used : ZFrame(org.zeromq.ZFrame) Random(java.util.Random) ZContext(org.zeromq.ZContext) ZMsg(org.zeromq.ZMsg) Socket(org.zeromq.ZMQ.Socket)

Example 19 with ZFrame

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

the class flserver2 method main.

public static void main(String[] args) {
    if (args.length < 1) {
        System.out.printf("I: syntax: flserver2 <endpoint>\n");
        System.exit(0);
    }
    ZContext ctx = new ZContext();
    Socket server = ctx.createSocket(ZMQ.REP);
    server.bind(args[0]);
    System.out.printf("I: echo service is ready at %s\n", args[0]);
    while (true) {
        ZMsg request = ZMsg.recvMsg(server);
        if (request == null)
            //  Interrupted
            break;
        //  Fail nastily if run against wrong client
        assert (request.size() == 2);
        ZFrame identity = request.pop();
        request.destroy();
        ZMsg reply = new ZMsg();
        reply.add(identity);
        reply.add("OK");
        reply.send(server);
    }
    if (Thread.currentThread().isInterrupted())
        System.out.printf("W: interrupted\n");
    ctx.destroy();
}
Also used : ZFrame(org.zeromq.ZFrame) ZContext(org.zeromq.ZContext) ZMsg(org.zeromq.ZMsg) Socket(org.zeromq.ZMQ.Socket)

Example 20 with ZFrame

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

the class flserver3 method main.

public static void main(String[] args) {
    boolean verbose = (args.length > 0 && args[0].equals("-v"));
    ZContext ctx = new ZContext();
    //  Prepare server socket with predictable identity
    String bindEndpoint = "tcp://*:5555";
    String connectEndpoint = "tcp://localhost:5555";
    Socket server = ctx.createSocket(ZMQ.ROUTER);
    server.setIdentity(connectEndpoint.getBytes(ZMQ.CHARSET));
    server.bind(bindEndpoint);
    System.out.printf("I: service is ready at %s\n", bindEndpoint);
    while (!Thread.currentThread().isInterrupted()) {
        ZMsg request = ZMsg.recvMsg(server);
        if (verbose && request != null)
            request.dump(System.out);
        if (request == null)
            //  Interrupted
            break;
        //  Frame 0: identity of client
        //  Frame 1: PING, or client control frame
        //  Frame 2: request body
        ZFrame identity = request.pop();
        ZFrame control = request.pop();
        ZMsg reply = new ZMsg();
        if (control.equals(new ZFrame("PING")))
            reply.add("PONG");
        else {
            reply.add(control);
            reply.add("OK");
        }
        request.destroy();
        reply.push(identity);
        if (verbose && reply != null)
            reply.dump(System.out);
        reply.send(server);
    }
    if (Thread.currentThread().isInterrupted())
        System.out.printf("W: interrupted\n");
    ctx.destroy();
}
Also used : ZFrame(org.zeromq.ZFrame) ZContext(org.zeromq.ZContext) ZMsg(org.zeromq.ZMsg) Socket(org.zeromq.ZMQ.Socket)

Aggregations

ZFrame (org.zeromq.ZFrame)27 ZMsg (org.zeromq.ZMsg)20 ZContext (org.zeromq.ZContext)13 Socket (org.zeromq.ZMQ.Socket)13 Poller (org.zeromq.ZMQ.Poller)7 ArrayList (java.util.ArrayList)4 Random (java.util.Random)4 ZMQ (org.zeromq.ZMQ)4 PollItem (org.zeromq.ZMQ.PollItem)3 IOException (java.io.IOException)2 ZLoop (org.zeromq.ZLoop)2 DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 RandomAccessFile (java.io.RandomAccessFile)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1