Search in sources :

Example 31 with ZContext

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

the class flclient1 method main.

//  .split client task
//  The client uses a Lazy Pirate strategy if it only has one server to talk
//  to. If it has two or more servers to talk to, it will try each server just
//  once:
public static void main(String[] argv) {
    ZContext ctx = new ZContext();
    ZMsg request = new ZMsg();
    request.add("Hello world");
    ZMsg reply = null;
    int endpoints = argv.length;
    if (endpoints == 0)
        System.out.printf("I: syntax: flclient1 <endpoint> ...\n");
    else if (endpoints == 1) {
        //  For one endpoint, we retry N times
        int retries;
        for (retries = 0; retries < MAX_RETRIES; retries++) {
            String endpoint = argv[0];
            reply = tryRequest(ctx, endpoint, request);
            if (reply != null)
                //  Successful
                break;
            System.out.printf("W: no response from %s, retrying...\n", endpoint);
        }
    } else {
        //  For multiple endpoints, try each at most once
        int endpointNbr;
        for (endpointNbr = 0; endpointNbr < endpoints; endpointNbr++) {
            String endpoint = argv[endpointNbr];
            reply = tryRequest(ctx, endpoint, request);
            if (reply != null)
                //  Successful
                break;
            System.out.printf("W: no response from %s\n", endpoint);
        }
    }
    if (reply != null) {
        System.out.printf("Service is running OK\n");
        reply.destroy();
    }
    request.destroy();
    ;
    ctx.destroy();
}
Also used : ZContext(org.zeromq.ZContext) ZMsg(org.zeromq.ZMsg)

Example 32 with ZContext

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

the class flserver1 method main.

public static void main(String[] args) {
    if (args.length < 1) {
        System.out.printf("I: syntax: flserver1 <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 msg = ZMsg.recvMsg(server);
        if (msg == null)
            //  Interrupted
            break;
        msg.send(server);
    }
    if (Thread.currentThread().isInterrupted())
        System.out.printf("W: interrupted\n");
    ctx.destroy();
}
Also used : ZContext(org.zeromq.ZContext) ZMsg(org.zeromq.ZMsg) Socket(org.zeromq.ZMQ.Socket)

Example 33 with ZContext

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

the class lruqueue3 method run.

public void run() {
    ZContext context = new ZContext();
    //  Prepare our context and sockets
    Socket worker = context.createSocket(ZMQ.REQ);
    worker.connect("ipc://backend.ipc");
    ZFrame frame = new ZFrame(lruqueue3.LRU_READY);
    //  Tell backend we're ready for work
    frame.send(worker, 0);
    while (true) {
        ZMsg msg = ZMsg.recvMsg(worker);
        if (msg == null)
            break;
        msg.getLast().reset("OK".getBytes(ZMQ.CHARSET));
        msg.send(worker);
        System.out.println(Thread.currentThread().getName() + " Worker Sent OK");
    }
    context.destroy();
}
Also used : ZFrame(org.zeromq.ZFrame) ZContext(org.zeromq.ZContext) ZMsg(org.zeromq.ZMsg) Socket(org.zeromq.ZMQ.Socket)

Aggregations

ZContext (org.zeromq.ZContext)33 Socket (org.zeromq.ZMQ.Socket)30 Poller (org.zeromq.ZMQ.Poller)17 ZMsg (org.zeromq.ZMsg)14 ZFrame (org.zeromq.ZFrame)13 Random (java.util.Random)11 ArrayList (java.util.ArrayList)4 ByteBuffer (java.nio.ByteBuffer)3 HashMap (java.util.HashMap)3 IOException (java.io.IOException)2 Entry (java.util.Map.Entry)2 ZLoop (org.zeromq.ZLoop)2 PollItem (org.zeromq.ZMQ.PollItem)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 LinkedList (java.util.LinkedList)1