Search in sources :

Example 56 with Socket

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

the class rtpapa method main.

//  We will do this all in one thread to emphasize the getSequence
//  of events
public static void main(String[] args) {
    Context context = ZMQ.context(1);
    Socket client = context.socket(ZMQ.ROUTER);
    client.bind("ipc://routing.ipc");
    Socket worker = context.socket(ZMQ.REP);
    worker.setIdentity("A".getBytes(ZMQ.CHARSET));
    worker.connect("ipc://routing.ipc");
    //  with routing envelope, it will actually match the worker
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    //  Send papa address, address stack, empty part, and request
    client.send("A", ZMQ.SNDMORE);
    client.send("address 3", ZMQ.SNDMORE);
    client.send("address 2", ZMQ.SNDMORE);
    client.send("address 1", ZMQ.SNDMORE);
    client.send("", ZMQ.SNDMORE);
    client.send("This is the workload", 0);
    //  Worker should get just the workload
    ZHelper.dump(worker);
    //  We don't play with envelopes in the worker
    worker.send("This is the reply", 0);
    //  Now dump what we got off the ROUTER socket
    ZHelper.dump(client);
    client.close();
    worker.close();
    context.term();
}
Also used : Context(org.zeromq.ZMQ.Context) Socket(org.zeromq.ZMQ.Socket)

Example 57 with Socket

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

the class rtreq method main.

/**
     * While this example runs in a single process, that is just to make
     * it easier to start and stop the example. Each thread has its own
     * context and conceptually acts as a separate process.
     */
public static void main(String[] args) throws Exception {
    Context context = ZMQ.context(1);
    Socket broker = context.socket(ZMQ.ROUTER);
    broker.bind("tcp://*:5671");
    for (int workerNbr = 0; workerNbr < NBR_WORKERS; workerNbr++) {
        Thread worker = new Worker();
        worker.start();
    }
    //  Run for five seconds and then tell workers to end
    long endTime = System.currentTimeMillis() + 5000;
    int workersFired = 0;
    while (true) {
        //  Next message gives us least recently used worker
        String identity = broker.recvStr();
        broker.sendMore(identity);
        //  Envelope delimiter
        broker.recvStr();
        //  Response from worker
        broker.recvStr();
        broker.sendMore("");
        //  Encourage workers until it's time to fire them
        if (System.currentTimeMillis() < endTime)
            broker.send("Work harder");
        else {
            broker.send("Fired!");
            if (++workersFired == NBR_WORKERS)
                break;
        }
    }
    broker.close();
    context.term();
}
Also used : Context(org.zeromq.ZMQ.Context) Socket(org.zeromq.ZMQ.Socket)

Example 58 with Socket

use of org.zeromq.ZMQ.Socket 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 59 with Socket

use of org.zeromq.ZMQ.Socket 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 60 with Socket

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

the class suisnail method main.

//  .split main task
//  The main task simply starts a client and a server, and then
//  waits for the client to signal that it has died:
public static void main(String[] args) throws Exception {
    ZContext ctx = new ZContext();
    Socket pubpipe = ZThread.fork(ctx, new Publisher());
    Socket subpipe = ZThread.fork(ctx, new Subscriber());
    subpipe.recvStr();
    pubpipe.send("break");
    Thread.sleep(100);
    ctx.destroy();
}
Also used : ZContext(org.zeromq.ZContext) Socket(org.zeromq.ZMQ.Socket)

Aggregations

Socket (org.zeromq.ZMQ.Socket)84 Context (org.zeromq.ZMQ.Context)32 ZContext (org.zeromq.ZContext)30 Test (org.junit.Test)26 Poller (org.zeromq.ZMQ.Poller)20 ZMsg (org.zeromq.ZMsg)14 Random (java.util.Random)13 ZFrame (org.zeromq.ZFrame)13 PollItem (org.zeromq.ZMQ.PollItem)6 ByteBuffer (java.nio.ByteBuffer)4 ArrayList (java.util.ArrayList)4 SelectableChannel (java.nio.channels.SelectableChannel)3 HashMap (java.util.HashMap)3 IOException (java.io.IOException)2 LinkedList (java.util.LinkedList)2 Entry (java.util.Map.Entry)2 Ignore (org.junit.Ignore)2 Actor (org.zeromq.ZActor.Actor)2 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1