Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1472988
  • 博文数量: 148
  • 博客积分: 2234
  • 博客等级: 大尉
  • 技术积分: 3225
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-17 21:34
个人简介

未来很长。

文章存档

2017年(7)

2016年(4)

2015年(1)

2014年(6)

2013年(31)

2012年(99)

分类: Java

2013-08-21 10:44:44

最近一直在学习JS,以前一直都在搞C/C++,接触了JS之后,感觉到了JS的强大之处,下面先看看学到的一个小的知识点,就是传输信息的工具zmq:

PUB/SUB模式: 发布/订阅模式。该模式是单向的,发布者send msg, 订阅者receive msg.  一旦有消息发出,pub会发送给所有的sub。.

模式如下图:

可以看出发布者绑定绑定一个端口,订阅者通过连接发布者接受订阅的消息。

官网描述这种模式要注意以下几点:

1. pub/sub模式下,sub事实上可以连接多个pub,每次只连接一个connect,所以接收到的消息可以是叫错的,以至于不会单个pub掩盖了其他pub

2. 如果存在某个pub没有被任何sub连接,则该pub会丢弃所有的消息

3. 如果你采用tcp的连接方式,sub很慢,消息将会堆积在pub,后期会对该问题有个较好的解决

4. 目前的而版本,过滤发生在sub端,而不是pub端,意思就是说一个pub会发送所有的消息到所有的sub, 由sub决定是要drop这个msg.

 

以下代码显示一个pub 两个sub, Publisher发送两种主题的消息A和B, Sub1接受B主题的消息, Sub2接受A主题的消息。


Java 代码

点击(此处)折叠或打开

  1. package pubsub;
  2.       
  3.     import org.zeromq.ZMQ;
  4.     import org.zeromq.ZMQ.Context;
  5.     import org.zeromq.ZMQ.Socket;
  6.       
  7.     public class Publisher {
  8.       
  9.         /**
  10.          * @param args
  11.          * @throws InterruptedException
  12.          */
  13.         public static void main(String[] args) throws InterruptedException {
  14.         // TODO Auto-generated method stub
  15.         Context context = ZMQ.context(1);
  16.         Socket publisher = context.socket(ZMQ.PUB);
  17.         publisher.bind("tcp://*:5557");
  18.         int i = 0;
  19.         while (true) {
  20.             Thread.currentThread().sleep(1000);
  21.             publisher.send("A".getBytes(), ZMQ.SNDMORE);
  22.             publisher.send("This is A".getBytes(), 0);
  23.             publisher.send("B".getBytes(), ZMQ.SNDMORE);
  24.             publisher.send("This is B".getBytes(), 0);
  25.         }
  26.         }
  27.       
  28.     }

Java代码

点击(此处)折叠或打开

  1. package pubsub;
  2.       
  3.     import org.zeromq.ZMQ;
  4.     import org.zeromq.ZMQ.Context;
  5.     import org.zeromq.ZMQ.Socket;
  6.       
  7.     public class Sub1 {
  8.       
  9.         /**
  10.          * @param args
  11.          */
  12.         public static void main(String[] args) {
  13.         // TODO Auto-generated method stub
  14.         Context context = ZMQ.context(1);
  15.         Socket subscribe = context.socket(ZMQ.SUB);
  16.         subscribe.connect("tcp://127.0.0.1:5557");
  17.         subscribe.subscribe("B".getBytes());
  18.         while (true) {
  19.             System.out.println(new String(subscribe.recv(0)));
  20.             System.out.println(new String(subscribe.recv(0)));
  21.         }
  22.         }
  23.       
  24.     }

Java代码

点击(此处)折叠或打开

  1. package pubsub;
  2.       
  3.     import org.zeromq.ZMQ;
  4.     import org.zeromq.ZMQ.Context;
  5.     import org.zeromq.ZMQ.Socket;
  6.       
  7.     public class Sub2 {
  8.         public static void main(String[] args) {
  9.         // TODO Auto-generated method stub
  10.         Context context = ZMQ.context(1);
  11.         Socket subscribe = context.socket(ZMQ.SUB);
  12.         subscribe.connect("tcp://127.0.0.1:5557");
  13.         //subscribe.subscribe("topic".getBytes());
  14.         subscribe.subscribe("A".getBytes());
  15.         while (true) {
  16.             System.out.println(new String(subscribe.recv(0)));
  17.             System.out.println(new String(subscribe.recv(0)));
  18.         }
  19.         }
  20.     }

结果显示
Java代码

点击(此处)折叠或打开

  1. Sub1:
  2.     B
  3.     This is B
  4.     B
  5.     This is B
  6.     B
  7.     This is B
  8.     B
  9.     This is B
  10.     ...
Java代码






阅读(11222) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~