提交 9d884da5 编写于 作者: S shenhongxi

zeromq

上级 3066988e
...@@ -58,6 +58,13 @@ ...@@ -58,6 +58,13 @@
<version>4.2.0</version> <version>4.2.0</version>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/org.zeromq/jeromq -->
<dependency>
<groupId>org.zeromq</groupId>
<artifactId>jeromq</artifactId>
<version>0.4.2</version>
</dependency>
</dependencies> </dependencies>
......
package com.itlong.whatsmars.mq.zeromq;
import org.zeromq.ZMQ;
public class Publisher {
public static void main(String args[]) {
ZMQ.Context context = ZMQ.context(1); // 创建包含一个I/O线程的context
ZMQ.Socket publisher = context.socket(ZMQ.PUB);
publisher.bind("tcp://*:5555");
while (!Thread.currentThread ().isInterrupted()) {
String message = "toutiao hello";
publisher.send(message.getBytes());
System.out.println("sent : " + message);
}
publisher.close();
context.term();
}
}
\ No newline at end of file
package com.itlong.whatsmars.mq.zeromq;
import org.zeromq.ZMQ;
public class Subscriber {
public static void main(String args[]) {
for (int j = 0; j < 100; j++) {
new Thread(new Runnable(){
public void run() {
ZMQ.Context context = ZMQ.context(1);
ZMQ.Socket subscriber = context.socket(ZMQ.SUB);
subscriber.connect("tcp://127.0.0.1:5555");
subscriber.subscribe("toutiao".getBytes());
try {
while (true) {
byte[] message = subscriber.recv();
System.out.println("receive : " + new String(message));
}
} finally {
subscriber.close();
context.term();
}
}
}).start();
}
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册