Package org.zeromq.ZMQ

Examples of org.zeromq.ZMQ.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);
View Full Code Here


    //  the heartbeating, which lets the worker detect if the queue has
    //  died, and vice-versa:
   
    public static void main(String[] args) {
        ZContext ctx = new ZContext ();
        Socket worker = worker_socket (ctx);

        //  If liveness hits zero, queue is considered disconnected
        int liveness = HEARTBEAT_LIVENESS;
        int interval = INTERVAL_INIT;
View Full Code Here

    public static void main (String[] args) {

        Context context = ZMQ.context(1);
   
        //  Bind to inproc: endpoint, then start upstream thread
        Socket receiver = context.socket(ZMQ.PAIR);
        receiver.bind("inproc://step3");
       
        //  Step 2 relays the signal to step 3
        Thread step2 = new Step2 (context);
        step2.start();
       
        //  Wait for signal
        receiver.recv(0);
        receiver.close ();
   
        System.out.println ("Test successful!");
        context.term ();
    }
View Full Code Here

        }

        @Override
        public void run(){
            //  Signal downstream to step 2
            Socket xmitter = context.socket(ZMQ.PAIR);
            xmitter.connect("inproc://step2");
            System.out.println ("Step 1 ready, signaling step 2");
            xmitter.send("READY", 0);
            xmitter.close ();
        }
View Full Code Here

        }

        @Override
        public void run(){
            //  Bind to inproc: endpoint, then start upstream thread
            Socket receiver = context.socket(ZMQ.PAIR);
            receiver.bind("inproc://step2");
            Thread step1 = new Step1 (context);
            step1.start();

            //  Wait for signal
            receiver.recv(0);
            receiver.close ();

            //  Connect to step3 and tell it we're ready
            Socket xmitter = context.socket(ZMQ.PAIR);
            xmitter.connect("inproc://step3");
            xmitter.send("READY", 0);

            xmitter.close ();
        }
View Full Code Here

public class psenvpub {

    public static void main (String[] args) throws Exception {
        // Prepare our context and publisher
        Context context = ZMQ.context(1);
        Socket publisher = context.socket(ZMQ.PUB);

        publisher.bind("tcp://*:5563");
        while (!Thread.currentThread ().isInterrupted ()) {
            // Write two messages, each with an envelope and content
            publisher.sendMore ("A");
            publisher.send ("We don't want to see this");
            publisher.sendMore ("B");
            publisher.send("We would like to see this");
        }
        publisher.close ();
        context.term ();
    }
View Full Code Here

     * 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);
            broker.recv(0);     //  Envelope delimiter
            broker.recv(0);     //  Response from worker
            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();
    }
View Full Code Here

        @Override
        public void run() {

            Context context = ZMQ.context(1);
            Socket worker = context.socket(ZMQ.DEALER);
            ZHelper.setId(worker)//  Set a printable identity

            worker.connect("tcp://localhost:5671");

            int total = 0;
            while (true) {
                //  Tell the broker we're ready for work
                worker.sendMore("");
                worker.send("Hi Boss");

                //  Get workload from broker, until finished
                worker.recvStr();   //  Envelope delimiter
                String workload = worker.recvStr();
                boolean finished = workload.equals("Fired!");
                if (finished) {
                    System.out.printf("Completed: %d tasks\n", total);
                    break;
                }
                total++;

                //  Do some random work
                try {
                    Thread.sleep(rand.nextInt(500) + 1);
                } catch (InterruptedException e) {
                }
            }
            worker.close();
            context.term();
        }
View Full Code Here

        Random rand = new Random(System.nanoTime());

        ZContext ctx = new ZContext();

        //  Bind state backend to endpoint
        Socket statebe = ctx.createSocket(ZMQ.PUB);
        statebe.bind(String.format("ipc://%s-state.ipc", self));

        //  Connect statefe to all peers
        Socket statefe = ctx.createSocket(ZMQ.SUB);
        statefe.subscribe("".getBytes());
        int argn;
        for (argn = 1; argn < argv.length; argn++) {
            String peer = argv[argn];
            System.out.printf("I: connecting to state backend at '%s'\n", peer);
            statefe.connect(String.format("ipc://%s-state.ipc", peer));
        }
        //  The main loop sends out status messages to peers, and collects
        //  status messages back from peers. The zmq_poll timeout defines
        //  our own heartbeat:

        while (true) {
            //  Poll for activity, or 1 second timeout
            PollItem items[] = {new PollItem(statefe, Poller.POLLIN)};
            int rc = ZMQ.poll(items, 1000);
            if (rc == -1)
                break;              //  Interrupted

            //  Handle incoming status messages
            if (items[0].isReadable()) {
                String peer_name = new String(statefe.recv(0));
                String available = new String(statefe.recv(0));
                System.out.printf("%s - %s workers free\n", peer_name, available);
            } else {
                //  Send random values for worker availability
                statebe.send(self, ZMQ.SNDMORE);
                statebe.send(String.format("%d", rand.nextInt(10)), 0);
View Full Code Here

     * to hold the list of workers, and to read and send messages:
     */
    public static void main (String[] args) {
        ZContext context = new ZContext();
        //  Prepare our context and sockets
        Socket frontend  = context.createSocket (ZMQ.ROUTER);
        Socket backend  = context.createSocket (ZMQ.ROUTER);
        frontend.bind("ipc://frontend.ipc");
        backend.bind("ipc://backend.ipc");

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

View Full Code Here

TOP

Related Classes of org.zeromq.ZMQ.Socket

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.