Package org.zeromq.ZMQ

Examples of org.zeromq.ZMQ.Context


*/
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());

    boolean more = false;
    byte[] message;

    //  Shunt messages out to our own subscribers
    while (!Thread.currentThread().isInterrupted()) {
      while (true) {
        // receive message
        message = frontend.recv(0);
        more = frontend.hasReceiveMore();

        // proxy it
        backend.send(message, more ? ZMQ.SNDMORE : 0);
        if(!more){
          break;
        }
      }
    }
    frontend.close();
    backend.close();
    context.term();
  }
View Full Code Here


* Christophe Huntzinger <chuntzin_at_wanadoo.fr>
*
*/
public class syncsub{
  public static void main (String[] args) {
    Context context = ZMQ.context(1);

    //  First, connect our subscriber socket
    Socket subscriber = context.socket(ZMQ.SUB);
    subscriber.connect("tcp://localhost:5561");
    subscriber.subscribe("".getBytes());

    //  Second, synchronize with publisher
    Socket syncclient = context.socket(ZMQ.REQ);
    syncclient.connect("tcp://localhost:5562");

    //  - send a synchronization request
    syncclient.send("".getBytes(), 0);

    //  - wait for synchronization reply
    byte[] value = syncclient.recv(0);

    //  Third, get our updates and report how many we got
    int update_nbr = 0;
    while (true) {
      byte[] stringValue = subscriber.recv(0);
      String string  = new String(stringValue);
      if (string.equals("END")) {
        break;
      }
      update_nbr++;
    }
    System.out.println("Received "+update_nbr+" updates.");

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

import org.zeromq.ZMQ.Context;
import org.zeromq.ZMQ.Socket;

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

    Socket socket = context.socket(ZMQ.REQ);
    socket.connect("tcp://localhost:5000");

    for (int i = 0; i < 10; i++) {
      socket.send("Hello", 0);
      String reply = socket.recvStr(0);
      System.out.println("Received reply " + i + " [" + reply + "]");
    }

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

import org.zeromq.ZMQ.Context;
import org.zeromq.ZMQ.Socket;

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

    Socket socket = context.socket(ZMQ.SUB);
    socket.connect("tcp://localhost:5000");

    socket.subscribe("tech".getBytes());
    socket.subscribe("music".getBytes());

    while (!Thread.currentThread().isInterrupted()) {
      String reply = socket.recvStr(0);
      System.out.println("Received " + reply);
    }

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

import org.zeromq.ZMQ.Context;
import org.zeromq.ZMQ.Socket;

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

    Socket socket = context.socket(ZMQ.PUB);
    socket.bind("tcp://localhost:5000");

    String[] topics = { "tech", "music", "design" };
    while (!Thread.currentThread().isInterrupted()) {
      for (int i = 0; i < topics.length; i++) {
        if (socket.send(topics[i], 0))
          System.out.println("Publish: " + topics[i]);
      }
    }

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

import org.zeromq.ZMQ.Context;
import org.zeromq.ZMQ.Socket;

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

    Socket pub = context.socket(ZMQ.PUB);
    pub.bind("tcp://localhost:5000");

    Socket receive = context.socket(ZMQ.PULL);
    receive.bind("tcp://localhost:5001");

    while (!Thread.currentThread().isInterrupted()) {
      String message = receive.recvStr(0);
      System.out.println("Received: " + message);
      pub.send(message, 0);
    }

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

      }
    }
  }

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

    Socket send = context.socket(ZMQ.PUSH);
    send.connect("tcp://localhost:5001");

    Socket receive = context.socket(ZMQ.SUB);
    receive.connect("tcp://localhost:5000");
    receive.subscribe("".getBytes());

    Poller poller = new Poller(0);
    poller.register(receive, Poller.POLLIN);

    System.out.print("What is your name? ");
    String name = in.nextLine();

    try {
      Sender sender = new Sender(name, send);
      sender.start();

      Receiver receiver = new Receiver(poller, receive);
      receiver.start();

      sender.join();
      receiver.join();
    }
    catch (InterruptedException e) {}
    finally {
      receive.close();
      send.close();
      context.term();
    }
  }
View Full Code Here

import org.zeromq.ZMQ.Context;
import org.zeromq.ZMQ.Socket;

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

    Socket socket = context.socket(ZMQ.REP);
    socket.bind("tcp://localhost:5000");

    while (!Thread.currentThread().isInterrupted()) {
      String request = socket.recvStr(0);
      System.out.println("Received request: [" + request + "].");
      socket.send("World", 0);
    }

    socket.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.