import org.zeromq.ZMQ; public class Publisher { public static void main(String[] args) { ZMQ.Context context = ZMQ.context(1); ZMQ.Socket publisher = context.socket(ZMQ.PUB); publisher.bind("tcp://localhost:5555"); while (!Thread.currentThread().isInterrupted()) { String message = "Hello, World!"; publisher.send(message.getBytes(), 0); System.out.println("Published: " + message); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } publisher.close(); context.term(); } } import org.zeromq.ZMQ; public class Subscriber { public static void main(String[] args) { ZMQ.Context context = ZMQ.context(1); ZMQ.Socket subscriber = context.socket(ZMQ.SUB); subscriber.connect("tcp://localhost:5555"); subscriber.subscribe("".getBytes()); while (!Thread.currentThread().isInterrupted()) { byte[] message = subscriber.recv(0); System.out.println("Received: " + new String(message)); } subscriber.close(); context.term(); } }


上一篇:
下一篇:
切换中文