当前位置: 首页>>代码示例>>Java>>正文


Java Socket.subscribe方法代码示例

本文整理汇总了Java中org.zeromq.ZMQ.Socket.subscribe方法的典型用法代码示例。如果您正苦于以下问题:Java Socket.subscribe方法的具体用法?Java Socket.subscribe怎么用?Java Socket.subscribe使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.zeromq.ZMQ.Socket的用法示例。


在下文中一共展示了Socket.subscribe方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: main

import org.zeromq.ZMQ.Socket; //导入方法依赖的package包/类
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(ZMQ.SUBSCRIPTION_ALL);

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

    frontend.close();
    backend.close();
    context.term();
}
 
开发者ID:zeromq,项目名称:jeromq3-x,代码行数:23,代码来源:wuproxy.java

示例2: main

import org.zeromq.ZMQ.Socket; //导入方法依赖的package包/类
public static void main(String[] args)
{
    ZContext context = new ZContext();
    Socket subscriber = context.createSocket(ZMQ.SUB);
    if (args.length == 1)
        subscriber.connect(args[0]);
    else
        subscriber.connect("tcp://localhost:5556");

    Random rand = new Random(System.currentTimeMillis());
    String subscription = String.format("%03d", rand.nextInt(1000));
    subscriber.subscribe(subscription.getBytes(ZMQ.CHARSET));

    while (true) {
        String topic = subscriber.recvStr();
        if (topic == null)
            break;
        String data = subscriber.recvStr();
        assert(topic.equals(subscription));
        System.out.println(data);
    }
    context.destroy();
}
 
开发者ID:zeromq,项目名称:jeromq3-x,代码行数:24,代码来源:pathosub.java

示例3: run

import org.zeromq.ZMQ.Socket; //导入方法依赖的package包/类
@Override
public void run(Object[] args, ZContext ctx, Socket pipe)
{
    //  Subscribe to "A" and "B"
    Socket subscriber = ctx.createSocket(ZMQ.SUB);
    subscriber.connect("tcp://localhost:6001");
    subscriber.subscribe("A".getBytes(ZMQ.CHARSET));
    subscriber.subscribe("B".getBytes(ZMQ.CHARSET));

    int count = 0;
    while (count < 5) {
        String string = subscriber.recvStr();
        if (string == null)
            break;              //  Interrupted
        count++;
    }
    ctx.destroySocket(subscriber);
}
 
开发者ID:zeromq,项目名称:jeromq3-x,代码行数:19,代码来源:espresso.java

示例4: run

import org.zeromq.ZMQ.Socket; //导入方法依赖的package包/类
public void run() {
	ZContext ctx = new ZContext();
	Socket subscriber = ctx.createSocket(ZMQ.SUB);
	subscriber.connect("tcp://localhost:5556");
	subscriber.subscribe(ZMQ.SUBSCRIPTION_ALL);

	while (true) {
		kvsimple kvMsg = kvsimple.recv(subscriber);
           if (kvMsg == null)
               break;

           clonecli1.kvMap.put(kvMsg.getKey(), kvMsg);
           System.out.println("receiving " + kvMsg);
           sequence.incrementAndGet();
	}
       ctx.destroy();
}
 
开发者ID:zeromq,项目名称:jeromq3-x,代码行数:18,代码来源:clonecli1.java

示例5: main

import org.zeromq.ZMQ.Socket; //导入方法依赖的package包/类
public static void main (String[] args) {

        // Prepare our context and subscriber
        Context context = ZMQ.context(1);
        Socket subscriber = context.socket(ZMQ.SUB);

        subscriber.connect("tcp://localhost:5563");
        subscriber.subscribe("B".getBytes(ZMQ.CHARSET));
        while (!Thread.currentThread ().isInterrupted ()) {
            // Read envelope with address
            String address = subscriber.recvStr ();
            // Read message contents
            String contents = subscriber.recvStr ();
            System.out.println(address + " : " + contents);
        }
        subscriber.close ();
        context.term ();
    }
 
开发者ID:zeromq,项目名称:jeromq3-x,代码行数:19,代码来源:psenvsub.java

示例6: connectSubSocket

import org.zeromq.ZMQ.Socket; //导入方法依赖的package包/类
Socket connectSubSocket() {
  Socket sub = ctx.socket(ZMQ.SUB);

  if (handlers.isEmpty()) {
    sub.subscribe("".getBytes());
  } else {
    for (String event : handlers.keySet()) {
      sub.subscribe(event.getBytes());
    }
  }
  sub.connect(pub);
  return sub;
}
 
开发者ID:Horsed,项目名称:jeromq-toolkit,代码行数:14,代码来源:AbstractSub.java

示例7: run

import org.zeromq.ZMQ.Socket; //导入方法依赖的package包/类
@Override
public void run(Object[] args, ZContext ctx, Socket pipe)
{
    //  Subscribe to everything
    Socket subscriber = ctx.createSocket(ZMQ.SUB);
    subscriber.subscribe(ZMQ.SUBSCRIPTION_ALL);
    subscriber.connect("tcp://localhost:5556");

    //  Get and process messages
    while (true) {
        String string = subscriber.recvStr();
        System.out.printf("%s\n", string);
        long clock = Long.parseLong(string);

        //  Suicide snail logic
        if (System.currentTimeMillis() - clock > MAX_ALLOWED_DELAY) {
            System.err.println("E: subscriber cannot keep up, aborting");
            break;
        }
        //  Work for 1 msec plus some random additional time
        try {
            Thread.sleep(1000 + rand.nextInt(2000));
        } catch (InterruptedException e) {
            break;
        }
    }
    pipe.send("gone and died");
}
 
开发者ID:zeromq,项目名称:jeromq3-x,代码行数:29,代码来源:suisnail.java

示例8: main

import org.zeromq.ZMQ.Socket; //导入方法依赖的package包/类
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(ZMQ.SUBSCRIPTION_ALL);

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

    //  - send a synchronization request
    syncclient.send(ZMQ.MESSAGE_SEPARATOR, 0);

    //  - wait for synchronization reply
    syncclient.recv(0);

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

    subscriber.close();
    syncclient.close();
    context.term();
}
 
开发者ID:zeromq,项目名称:jeromq3-x,代码行数:34,代码来源:syncsub.java

示例9: subscribe

import org.zeromq.ZMQ.Socket; //导入方法依赖的package包/类
public static Socket subscribe(Socket socket, byte[] topic) {
	socket.subscribe(topic);
	return socket;
}
 
开发者ID:zhangjunfang,项目名称:jstorm-0.9.6.3-,代码行数:5,代码来源:ZeroMq.java

示例10: main

import org.zeromq.ZMQ.Socket; //导入方法依赖的package包/类
public static void main (String[] args)
{
    ZContext ctx = new ZContext ();

    //  Use the Zyre UDP class to make sure we listen on the same
    //  network interface as our peers
    ZreUdp udp = new ZreUdp (ZreInterface.PING_PORT_NUMBER);
    String host = udp.host ();
    Socket collector = ctx.createSocket (ZMQ.SUB);

    //  Bind to an ephemeral port
    int port = collector.bindToRandomPort (String.format ("tcp://%s", host));

    //  Announce this to all peers we connect to
    ZreInterface inf = new ZreInterface ();
    inf.setHeader ("X-ZRELOG", "tcp://%s:%d", host, port);

    //  Get all log messages (don't filter)
    collector.subscribe ("".getBytes ());

    Poller items = ctx.getContext ().poller ();
    
    items.register (collector, Poller.POLLIN);
    items.register (inf.handle (), Poller.POLLIN);
    
    while (!Thread.currentThread ().isInterrupted ()) {
        if (items.poll (1000) == -1)
            break;              //  Interrupted
        //  Handle input on collector
        if (items.pollin (0))
            printLogMsg (collector);

        //  Handle event from interface (ignore it)
        if (items.pollin (1)) {
            ZMsg msg = inf.recv ();
            if (msg == null)
                break;              //  Interrupted
            msg.destroy ();
        }
    }
    inf.destroy ();
    udp.destroy ();
    ctx.destroy ();

}
 
开发者ID:zeromq,项目名称:jyre,代码行数:46,代码来源:ZreLogger.java

示例11: main

import org.zeromq.ZMQ.Socket; //导入方法依赖的package包/类
public static void main(String[] argv) {
    //  Arguments can be either of:
    //      -p  primary server, at tcp://localhost:5001
    //      -b  backup server, at tcp://localhost:5002
    ZContext ctx = new ZContext();
    Socket statepub = ctx.createSocket(ZMQ.PUB);
    Socket statesub = ctx.createSocket(ZMQ.SUB);
    statesub.subscribe(ZMQ.SUBSCRIPTION_ALL);
    Socket frontend = ctx.createSocket(ZMQ.ROUTER);
    bstarsrv fsm = new bstarsrv();

    if (argv.length == 1 && argv[0].equals("-p")) {
        System.out.printf("I: Primary active, waiting for backup (passive)\n");
        frontend.bind("tcp://*:5001");
        statepub.bind("tcp://*:5003");
        statesub.connect("tcp://localhost:5004");
        fsm.state = State.STATE_PRIMARY;
    }
    else
    if (argv.length == 1 && argv[0].equals("-b")) {
        System.out.printf("I: Backup passive, waiting for primary (active)\n");
        frontend.bind("tcp://*:5002");
        statepub.bind("tcp://*:5004");
        statesub.connect("tcp://localhost:5003");
        fsm.state = State.STATE_BACKUP;
    }
    else {
        System.out.printf("Usage: bstarsrv { -p | -b }\n");
        ctx.destroy();
        System.exit(0);
    }
    //  .split handling socket input
    //  We now process events on our two input sockets, and process these
    //  events one at a time via our finite-state machine. Our "work" for
    //  a client request is simply to echo it back:

    //  Set timer for next outgoing state message
    long sendStateAt = System.currentTimeMillis() + HEARTBEAT;
    while (!Thread.currentThread().isInterrupted()) {
        PollItem[] items = {
                new PollItem(frontend, ZMQ.Poller.POLLIN),
                new PollItem(statesub, ZMQ.Poller.POLLIN),
        };
        int timeLeft = (int) ((sendStateAt - System.currentTimeMillis()));
        if (timeLeft < 0)
            timeLeft = 0;
        int rc = ZMQ.poll(items, 2, timeLeft);
        if (rc == -1)
            break;              //  Context has been shut down

        if (items[0].isReadable()) {
            //  Have a client request
            ZMsg msg = ZMsg.recvMsg(frontend);
            fsm.event = Event.CLIENT_REQUEST;
            if (fsm.stateMachine() == false)
                //  Answer client by echoing request back
                msg.send(frontend);
            else
                msg.destroy();
        }
        if (items[1].isReadable()) {
            //  Have state from our peer, execute as event
            String message = statesub.recvStr();
            fsm.event = Event.values()[Integer.parseInt(message)];
            if (fsm.stateMachine())
                break;          //  Error, so exit
            fsm.peerExpiry = System.currentTimeMillis() + 2 * HEARTBEAT;
        }
        //  If we timed out, send state to peer
        if (System.currentTimeMillis() >= sendStateAt) {
            statepub.send(String.valueOf(fsm.state.ordinal()));
            sendStateAt = System.currentTimeMillis() + HEARTBEAT;
        }
    }
    if (Thread.currentThread().isInterrupted())
        System.out.printf ("W: interrupted\n");

    //  Shutdown sockets and context
    ctx.destroy();
}
 
开发者ID:zeromq,项目名称:jeromq3-x,代码行数:81,代码来源:bstarsrv.java

示例12: main

import org.zeromq.ZMQ.Socket; //导入方法依赖的package包/类
public static void main(String[] argv)
{
    //  First argument is this broker's name
    //  Other arguments are our peers' names
    //
    if (argv.length < 1) {
        System.out.println("syntax: peering1 me {you}\n");
        System.exit(-1);
    }
    String self = argv[0];
    System.out.println(String.format("I: preparing broker at %s\n", self));
    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(ZMQ.SUBSCRIPTION_ALL);
    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), ZMQ.CHARSET);
            String available = new String(statefe.recv(0), ZMQ.CHARSET);
            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);
        }
    }
    ctx.destroy();
}
 
开发者ID:zeromq,项目名称:jeromq3-x,代码行数:53,代码来源:peering1.java


注:本文中的org.zeromq.ZMQ.Socket.subscribe方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。