Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timothymugayi/cf51cbb67db4317fdd785daac2775582 to your computer and use it in GitHub Desktop.
Save timothymugayi/cf51cbb67db4317fdd785daac2775582 to your computer and use it in GitHub Desktop.
package com.zeromq;
import org.zeromq.SocketType;
import org.zeromq.ZContext;
import org.zeromq.ZMQ;
import org.zeromq.ZMQ.Socket;
public class Subscriber {
/**
* The subscriber thread requests messages starting with
* C and D, then reads and counts incoming messages.
* @param args
*/
public static void main(String[] args){
System.out.println("Starting Java Subscriber...");
try (ZContext context = new ZContext()) {
try (Socket subscriber = context.createSocket(SocketType.SUB)) {
subscriber.connect("tcp://localhost:6001");
System.out.println("Subscriber connected to ports 6001");
subscriber.subscribe("C".getBytes(ZMQ.CHARSET));
subscriber.subscribe("D".getBytes(ZMQ.CHARSET));
System.out.println("Listening to topic C, D");
while (!Thread.currentThread().isInterrupted()) {
// Block until a message is received
byte[] reply = subscriber.recv(0);
System.out.println("Received: [" + new String(reply, ZMQ.CHARSET) + "]");
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment