1

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?

Hnig
  • 65
  • 8

1 Answers1

0

You are getting this exception because executor was shutdown or maybe there in some place of your program you have System.exit() call. In generalYou are getting this exception when thread in which nio chanel was blocked, was interrupted. This is signal to stop processing of current task - make task responsible to this request - you can log, clear resources and exit the task. As i understand you are not expecting you worker thread to be interrupted - check you program code - and stop the interruption.

Oleksandr Papchenko
  • 2,071
  • 21
  • 30