package com.firebird.server;
import java.util.ArrayList;
import org.zeromq.ZContext;
import org.zeromq.ZMQ;
import org.zeromq.ZMQ.Socket;
import org.zeromq.ZMsg;
public class PubSubPattern {
public static class PublisherRunnable implements Runnable{
@Override
public void run(){
ZContext cxt = new ZContext(1);
Socket hwServer = cxt.createSocket(ZMQ.PUB);
hwServer.bind("tcp://*:5550");
while(!Thread.currentThread().isInterrupted()){
ZMsg sendMsg = new ZMsg();
sendMsg.addString("world");
sendMsg.send(hwServer);
sendMsg.destroy();
}
hwServer.close();
cxt.destroy();
}
}
public static class SubcriberRunnable implements Runnable{
@Override
public void run(){
ZContext cxt = new ZContext(1);
Socket hwSocket = cxt.createSocket(ZMQ.SUB);
hwSocket.connect("tcp://*:5550");
//长度为0的byte数组,说明接受所有到达的消息,这两行代码是一定要加的,不然会接受不到数据,
byte[] filter = new byte[0];
hwSocket.subscribe(filter);
while(!Thread.currentThread().isInterrupted()){
ZMsg msg = ZMsg.recvMsg(hwSocket, 0);
if(msg == null){
continue;
}
System.out.println(msg.toString());
msg.destroy();
}
hwSocket.close();
cxt.destroy();
}
}
public static void main(String[] args) {
Thread bossThread = new Thread(new PublisherRunnable());
bossThread.start();
ArrayList<Thread> thArray = new ArrayList<Thread>(5);
for(int i = 0 ;i< 5;i++){
Thread th = new Thread(new SubcriberRunnable());
thArray.add(th);
th.start();
}
/*
try{
Thread.sleep(1000);
for(int i = 0;i<thArray.size();i++){
thArray.get(i).interrupt();
}
bossThread.interrupt();
}catch(Exception e){
e.printStackTrace();
}*/
}
}
import java.util.ArrayList;
import org.zeromq.ZContext;
import org.zeromq.ZMQ;
import org.zeromq.ZMQ.Socket;
import org.zeromq.ZMsg;
public class PubSubPattern {
public static class PublisherRunnable implements Runnable{
@Override
public void run(){
ZContext cxt = new ZContext(1);
Socket hwServer = cxt.createSocket(ZMQ.PUB);
hwServer.bind("tcp://*:5550");
while(!Thread.currentThread().isInterrupted()){
ZMsg sendMsg = new ZMsg();
sendMsg.addString("world");
sendMsg.send(hwServer);
sendMsg.destroy();
}
hwServer.close();
cxt.destroy();
}
}
public static class SubcriberRunnable implements Runnable{
@Override
public void run(){
ZContext cxt = new ZContext(1);
Socket hwSocket = cxt.createSocket(ZMQ.SUB);
hwSocket.connect("tcp://*:5550");
//长度为0的byte数组,说明接受所有到达的消息,这两行代码是一定要加的,不然会接受不到数据,
byte[] filter = new byte[0];
hwSocket.subscribe(filter);
while(!Thread.currentThread().isInterrupted()){
ZMsg msg = ZMsg.recvMsg(hwSocket, 0);
if(msg == null){
continue;
}
System.out.println(msg.toString());
msg.destroy();
}
hwSocket.close();
cxt.destroy();
}
}
public static void main(String[] args) {
Thread bossThread = new Thread(new PublisherRunnable());
bossThread.start();
ArrayList<Thread> thArray = new ArrayList<Thread>(5);
for(int i = 0 ;i< 5;i++){
Thread th = new Thread(new SubcriberRunnable());
thArray.add(th);
th.start();
}
/*
try{
Thread.sleep(1000);
for(int i = 0;i<thArray.size();i++){
thArray.get(i).interrupt();
}
bossThread.interrupt();
}catch(Exception e){
e.printStackTrace();
}*/
}
}