ZMQ - Python and Java Communication
Python ZMQ Server
import time
import zmq
context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://*:5555")
i = 0
print("Waiting for incoming messages")
while True:
# Wait for next request from client
message = socket.recv()
print(message)
socket.send_string(f"pong {i}")
i = i + 1
Java Client
import org.zeromq.ZMQ;
public class SocketClient {
public static void main(String[] args) {
ZMQ.Context context = ZMQ.context(1);
ZMQ.Socket socket = context.socket(ZMQ.REQ);
socket.connect ("tcp://localhost:5555");
for(int i = 0; i <10; i++) {
String request = String.format("Ping %d", i) ;
socket.send(request.getBytes (), 0);
byte[] reply = socket.recv(0);
System.out.println("Received " + new String (reply));
}
socket.close();
context.term();
}
}
Maven dependency for ZMQ in Java.
<dependency>
<groupId>org.zeromq</groupId>
<artifactId>jeromq</artifactId>
<version>0.5.1</version>
</dependency>