Running docker containers with zeromq
Hi, I am new to docker and have been trying to setup a simulation of a client server arrangement with docker and jzmq (the java port of zeromq). My code is here:
The client
import java.io.PrintWriter;
import java.io.StringWriter;
import org.zeromq.ZMQ;
import java.util.*;
public class EchoClient{
public static void main(String[] args){
ZMQ.Context context = ZMQ.Context(1);
Scanner scanner = new Scanner(System.in);
boolean close = false;
ZMQ.Socket requester = context.socket(ZMQ.REQ);
String serverAddr = System.getenv("SERVER_CONNECT_ADDR");
requester.connect(serverAddr);
try{
while(true){
System.out.println("Please input a line");
String line = scanner.nextLine();
if (line.isEmpty() || "NO".equalsIgnoreCase(line)){
close = true;
}
requeste.send(line.getBytes, 0);
byte [] reply = requester.recv(0);
System.out.println("Received(server response): "
+ new String(reply));
if (close == true){
return;
}
System.out.println("Please input a line: ");
line = scanner.nextLine();
System.out.println("User input was: %s%n", line);
}
} catch (IlegalStateException | NoSuchElementException e){
System.out.println("System.in was closed; exiting");
}
requester.close();
context.term();
}
}
The server
import java.io.PrintWriter;
import java.io.StringWriter;
import org.zeromq.ZMQ;
public class EchoServer{
public static void main(String[] args){
ZMQ.Context context = ZMQ.Context(1);
ZMQ.Socket responder = context.socket(ZMQ.REP);
String ipaddress = System.getenv("SERVER_LISTEN_ADDR");
responder.bind(ipaddress);
try{
while(!Thread.currentThread().isInterrupted()){
byte[] request = responder.recv(0);
if (request.length != 0 &&
"NO".equalsIgnoreCase(new String(request)){
System.out.println("Received (client request): "
+ new String(request));
Thread.sleep(1000);
String reply="Previous command: " + new String(request)
+ "\n Next command: \n";
} else {
String reply = "Server is closing";
responder.send(reply.getBytes(), 0);
Thread.currentThread().interrupt();
}
}
} catch( Exception e){
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter();
e.printStackTrace();
System.out.println(sw.toString());
}
responder.close();
context.term();
}
}
entrypoint.sh(for Server)
#!/bin/bash
COMPLIE_ARGS=".:/usr/local/share/java/zmq.jar"
RUN_ARGS="-Djava.library.path=/usr/local/lib"
APP="EchoServer"
javac -cp ${COMPLIE_ARGS} ${APP}.java
java -cp ${COMPLIE_ARGS} ${RUN_ARGS} ${APP}
entrypoint.sh(for Client)
#!/bin/bash
COMPLIE_ARGS=".:/usr/local/share/java/zmq.jar"
RUN_ARGS="-Djava.library.path=/usr/local/lib"
APP="EchoClient"
javac -cp ${COMPLIE_ARGS} ${APP}.java
java -cp ${COMPLIE_ARGS} ${RUN_ARGS} ${APP}
docker-compose.yml
version: "2"
services:
serverd:
build: serverd
environment:
SERVER_LISTEN_ADDR: tcp://0.0.0.0:5555
clientd:
build: clientd
environment:
SERVER_CONNECT_ADDR: tcp://serverd:5555
My OS build is Ubuntu 16.04 LTS.