Package org.zeromq.ZMQ

Examples of org.zeromq.ZMQ.Context


        }
    }
   
    public static void main (String[] args) {

        Context context = ZMQ.context(1);

        Socket clients = context.socket(ZMQ.ROUTER);
        clients.bind ("tcp://*:5555");

        Socket workers = context.socket(ZMQ.DEALER);
        workers.bind ("inproc://workers");

        for(int thread_nbr = 0; thread_nbr < 5; thread_nbr++) {
            Thread worker = new Worker (context);
            worker.start();
        }
        //  Connect work threads to client threads via a queue
        ZMQ.proxy (clients, workers, null);

        //  We never get here but clean up anyhow
        clients.close();
        workers.close();
        context.term();
    }
View Full Code Here


    public static void main(String[] argv) throws Exception
    {
        Random rand = new Random(System.nanoTime());

        Context context = ZMQ.context(1);
        Socket server = context.socket(ZMQ.REP);
        server.bind("tcp://*:5555");

        int cycles = 0;
        while (true) {
            String request = server.recvStr();
            cycles++;

            //  Simulate various problems, after a few cycles
            if (cycles > 3 && rand.nextInt(3) == 0) {
                System.out.println("I: simulating a crash");
                break;
            } else if (cycles > 3 && rand.nextInt(3) == 0) {
                System.out.println("I: simulating CPU overload");
                Thread.sleep(2000);
            }
            System.out.printf("I: normal request (%s)\n", request);
            Thread.sleep(1000);              //  Do some heavy work
            server.send(request);
        }
        server.close();
        context.term();
    }
View Full Code Here

* Sends "Hello" to server, expects "World" back
*/
public class rrclient{

    public static void main(String[] args) {
        Context context = ZMQ.context(1);

        //  Socket to talk to server
        Socket requester = context.socket(ZMQ.REQ);
        requester.connect("tcp://localhost:5559");
       
        System.out.println("launch and connect client.");

        for (int request_nbr = 0; request_nbr < 10; request_nbr++) {
            requester.send("Hello", 0);
            String reply = requester.recvStr(0);
            System.out.println("Received reply " + request_nbr + " [" + reply + "]");
        }
       
        //  We never get here but clean up anyhow
        requester.close();
        context.term();
    }
View Full Code Here

*/
public class clonecli2 {
  private static Map<String, kvsimple> kvMap = new HashMap<String, kvsimple>();

  public void run() {
    Context ctx = ZMQ.context(1);
    Socket snapshot = ctx.socket(ZMQ.DEALER);
    snapshot.connect("tcp://localhost:5556");

    Socket subscriber = ctx.socket(ZMQ.SUB);
    subscriber.connect("tcp://localhost:5557");
    subscriber.subscribe("".getBytes());

    // get state snapshot
    snapshot.send("ICANHAZ?".getBytes(), 0);
View Full Code Here

*/
public class msgqueue{

    public static void main (String[] args) {
        //  Prepare our context and sockets
        Context context = ZMQ.context(1);

        //  Socket facing clients
        Socket frontend = context.socket(ZMQ.ROUTER);
        frontend.bind("tcp://*:5559");

        //  Socket facing services
        Socket backend = context.socket(ZMQ.DEALER);
        backend.bind("tcp://*:5560");

        //  Start the proxy
        ZMQ.proxy (frontend, backend, null);

        //  We never get here but clean up anyhow
        frontend.close();
        backend.close();
        context.term();
    }
View Full Code Here

     * We wait for 10 subscribers
     */
    protected static int SUBSCRIBERS_EXPECTED = 10;

    public static void main (String[] args) {
        Context context = ZMQ.context(1);

        //  Socket to talk to clients
        Socket publisher = context.socket(ZMQ.PUB);
        publisher.setLinger(5000);
        // In 0MQ 3.x pub socket could drop messages if sub can follow the generation of pub messages
        publisher.setSndHWM(0);
        publisher.bind("tcp://*:5561");

        //  Socket to receive signals
        Socket syncservice = context.socket(ZMQ.REP);
        syncservice.bind("tcp://*:5562");

        System.out.println("Waiting subscribers");
        //  Get synchronization from subscribers
        int subscribers = 0;
        while (subscribers < SUBSCRIBERS_EXPECTED) {
            //  - wait for synchronization request
            syncservice.recv(0);

            //  - send synchronization reply
            syncservice.send("", 0);
            subscribers++;
        }
        //  Now broadcast exactly 1M updates followed by END
        System.out.println ("Broadcasting messages");

        int update_nbr;
        for (update_nbr = 0; update_nbr < 1000000; update_nbr++){
            publisher.send("Rhubarb", 0);
        }

        publisher.send("END", 0);

        // clean up
        publisher.close();
        syncservice.close();
        context.term();
    }
View Full Code Here

*/
public class clonesrv1 {
  private static AtomicLong sequence = new AtomicLong();

  public void run() {
    Context ctx = ZMQ.context(1);
    Socket publisher = ctx.socket(ZMQ.PUB);
    publisher.bind("tcp://*:5556");

    try {
      Thread.sleep(200);
    } catch (InterruptedException e) {
View Full Code Here

*/
public class wuproxy{

    public static void main (String[] args) {
        //  Prepare our context and sockets
        Context context = ZMQ.context(1);

        //  This is where the weather server sits
        Socket frontend =  context.socket(ZMQ.SUB);
        frontend.connect("tcp://192.168.55.210:5556");

        //  This is our public endpoint for subscribers
        Socket backend  = context.socket(ZMQ.PUB);
        backend.bind("tcp://10.1.1.0:8100");

        //  Subscribe on everything
        frontend.subscribe("".getBytes());

        //  Run the proxy until the user interrupts us
        ZMQ.proxy (frontend, backend, null);

        frontend.close();
        backend.close();
        context.term();
    }
View Full Code Here

     * they start; after that we treat them as ready when they reply with
     * a response back to a client. The load-balancing data structure is
     * just a queue of next available workers.
     */
    public static void main (String[] args) {
        Context context = ZMQ.context(1);
        //  Prepare our context and sockets
        Socket frontend  = context.socket(ZMQ.ROUTER);
        Socket backend  = context.socket(ZMQ.ROUTER);
        frontend.bind("ipc://frontend.ipc");
        backend.bind("ipc://backend.ipc");

        int clientNbr;
        for (clientNbr = 0; clientNbr < NBR_CLIENTS; clientNbr++)
            new ClientTask().start();

        for (int workerNbr = 0; workerNbr < NBR_WORKERS; workerNbr++)
            new WorkerTask().start();

        //  Here is the main loop for the least-recently-used queue. It has two
        //  sockets; a frontend for clients and a backend for workers. It polls
        //  the backend in all cases, and polls the frontend only when there are
        //  one or more workers ready. This is a neat way to use 0MQ's own queues
        //  to hold messages we're not ready to process yet. When we get a client
        //  reply, we pop the next available worker, and send the request to it,
        //  including the originating client identity. When a worker replies, we
        //  re-queue that worker, and we forward the reply to the original client,
        //  using the reply envelope.

        //  Queue of available workers
        Queue<String> workerQueue = new LinkedList<String>();

        while (!Thread.currentThread().isInterrupted()) {

            //  Initialize poll set
            Poller items = new Poller (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)
                break;

            //  Handle worker activity on backend
            if (items.pollin(0)) {

                //  Queue worker address for LRU routing
                workerQueue.add (backend.recvStr ());

                //  Second frame is empty
                String empty = backend.recvStr ();
                assert (empty.length() == 0);

                //  Third frame is READY or else a client reply address
                String clientAddr = backend.recvStr ();

                //  If client reply, send rest back to frontend
                if (!clientAddr.equals("READY")) {

                    empty = backend.recvStr ();
                    assert (empty.length() == 0);

                    String reply = backend.recvStr ();
                    frontend.sendMore(clientAddr);
                    frontend.sendMore("");
                    frontend.send(reply);

                    if (--clientNbr == 0)
                        break;
                }

            }

            if (items.pollin(1)) {
                //  Now get next client request, route to LRU worker
                //  Client request is [address][empty][request]
                String clientAddr = frontend.recvStr ();

                String empty = frontend.recvStr ();
                assert (empty.length() == 0);

                String request = frontend.recvStr ();

                String workerAddr = workerQueue.poll();

                backend.sendMore (workerAddr);
                backend.sendMore ("");
                backend.sendMore (clientAddr );
                backend.sendMore ("");
                backend.send (request);

            }
        }

        frontend.close();
        backend.close();
        context.term();

    }
View Full Code Here

     */
    private static class ClientTask extends Thread
    {
        public void run()
        {
            Context context = ZMQ.context(1);

            //  Prepare our context and sockets
            Socket client  = context.socket(ZMQ.REQ);
            ZHelper.setId (client);     //  Set a printable identity

            client.connect("ipc://frontend.ipc");

            //  Send request, get reply
            client.send("HELLO");
            String reply = client.recvStr ();
            System.out.println("Client: " + reply);

            client.close();
            context.term();
        }
View Full Code Here

TOP

Related Classes of org.zeromq.ZMQ.Context

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.