Search in sources :

Example 81 with Socket

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

the class pathopub method main.

public static void main(String[] args) throws Exception {
    ZContext context = new ZContext();
    Socket publisher = context.createSocket(ZMQ.PUB);
    if (args.length == 1)
        publisher.connect(args[0]);
    else
        publisher.bind("tcp://*:5556");
    //  Ensure subscriber connection has time to complete
    Thread.sleep(1000);
    //  Send out all 1,000 topic messages
    int topicNbr;
    for (topicNbr = 0; topicNbr < 1000; topicNbr++) {
        publisher.send(String.format("%03d", topicNbr), ZMQ.SNDMORE);
        publisher.send("Save Roger");
    }
    //  Send one random update per second
    Random rand = new Random(System.currentTimeMillis());
    while (!Thread.currentThread().isInterrupted()) {
        Thread.sleep(1000);
        publisher.send(String.format("%03d", rand.nextInt(1000)), ZMQ.SNDMORE);
        publisher.send("Off with his head!");
    }
    context.destroy();
}
Also used : Random(java.util.Random) ZContext(org.zeromq.ZContext) Socket(org.zeromq.ZMQ.Socket)

Example 82 with Socket

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

Example 83 with Socket

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

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

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