consider the following worker:
public class Worker implements Callable<String> ....
// initialize context
@Override
public String call() {
ZMQ.Socket pullSocket = context.socket(ZMQ.PULL);
pullSocket.connect(HOST_PULL_SOCKET);
while (!Thread.currentThread().isInterrupted() && !context.isClosed()) {
String result = pullSocket.recvStr();
return result;
}
return "test";
}
The pullSocket.recvStr()
throws an java.nio.channels.ClosedByInterruptException
as the following scenario
ExecutorService service = Executors.newFixedThreadPool(4);
List<Worker> workers = new ArrayList<>();
for(int i = 0: i < 4;i++){
workers.add(new Worker()); // Class where call() is executed
}
String result = service.invokeAny(workers);
is executed.
How can I avoid this kind of Exception or what is preferred way to handle the closing of sockets wheninvokeAny()
gets called?