title: Cleanly interrupt zeromq polling thread
body: <p>I have a multithreaded application written in Java using jeromq 0.3.2.  I'm working on putting it into the Tanuki service wrapper so that it runs as a windows service, and I'm having some trouble with cleanly stopping the threads.  The <code>run()</code> method of the proxy is a variation on the <a href="http://zguide.zeromq.org/page%3aall#Basic-Reliable-Queuing-Simple-Pirate-Pattern" rel="nofollow">Simple Pirate</a> pattern from <a href="http://zguide.zeromq.org/page%3aall" rel="nofollow">the guide</a>, and looks like this:</p>
  
  <pre><code>public void run()
  {
      ZContext context = new ZContext();
      Socket frontend = context.createSocket(ZMQ.ROUTER);
      Socket backend = context.createSocket(ZMQ.ROUTER);
      frontend.bind("tcp://*:5555");
      backend.bind("inproc://workers");
  
      while (!Thread.currentThread().isInterrupted())
      {
          ZMQ.Poller poller = new ZMQ.Poller(2);
          poller.register(frontend, ZMQ.Poller.POLLIN);
          poller.register(backend, ZMQ.Poller.POLLIN);
          poller.poll();
          if (poller.pollin(0))
          {
              processFrontend(frontend, backend, context);
          }
          if (poller.pollin(1))
          {
              processBackend(frontend, backend);
          }
      }
  
      // additonal code to close worker threads
  }
  </code></pre>
  
  <p>How can I cleanly exit from this loop when the controlling wrapper wants to stop the application?</p>
  
  <p>If there are no clients currently connected, then the loop is blocked at the call to <code>poller.poll()</code>, so if the wrapper calls <code>interrupt()</code> on the thread it is ignored.  If there are clients currently sending in messages, then a call to <code>interrupt()</code> causes the <code>poller.poll()</code> method to throw a <code>zmq.ZError$IOException: java.nio.channels.ClosedByInterruptException</code></p>
  
  <p>I've also tried to use:</p>
  
  <pre><code>PollItem[] items = {
      new PollItem(frontend, Poller.POLLIN),
      new PollItem(backend, Poller.POLLIN)
  };
  int rc = ZMQ.poll(items, 2, -1);
  
  if (rc == -1)
      break;
  
  if (items[0].isReadable())
  {
      processFrontend(frontend, backend, context);
  }
  
  if (items[1].isReadable())
  {
      processBackend(frontend, backend);
  }
  </code></pre>
  
  <p>but the call to <code>ZMQ.poll</code> exhibits the same behaviour.  Two possible alternatives are:</p>
  
  <ul>
  <li>set a timeout on <code>ZMQ.poll</code> and wrap the content of the <code>run()</code> method in a try/catch for the IOException.  </li>
  <li>add a method to my Runnable that will connect to the frontend and send a special message that will be read in <code>processFrontend</code> and cause the code to break out of the loop.</li>
  </ul>
  
  <p>The first seems a bit dirty, and the second feels a bit fragile.  Are there any other approaches I should consider?  Is there some other polling method I can use that reacts more cleanly to a thread interrupt?</p>