Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>No worries. I've ended with this:</p> <pre><code>// maximum number of restarts for socket server private final AtomicLong reconnectCounter = new AtomicLong(MAX_RECONNECT); public void run() { boolean reconnect = false; ServerSocketChannel ssc = null; ServerSocket serverSocket = null; Thread t = Thread.currentThread(); try { // handle pretty rare situation when exit flag was already set if (t.isInterrupted()) { Thread.interrupted(); logger.error("Monitor thread was started with interrupted status set"); return; } ssc = ServerSocketChannel.open(); final InetSocketAddress isa = new InetSocketAddress(InetAddress.getByName(BIND_IP), conf.monitoringPort); serverSocket = ssc.socket(); serverSocket.bind(isa); ssc.configureBlocking(false); // active polling mode while (!t.isInterrupted()) { boolean succ = false; SocketChannel chan = null; Socket s = null; try { chan = null; s = null; chan = ssc.accept(); if (chan != null) { s = chan.socket(); logger.error("Accepting message from @" + (s != null ? s.getRemoteSocketAddress() : "null")); final long ts = createTimestamp(); MonitorTask task = new MonitorTask(s, ts, MonitorPoolService.this, monitorEmailService); ExecutorService exec = this.taskExecutor; if (exec != null) { exec.execute(task); succ = true; } else { t.interrupt(); break; // make sure loop exited - flag could change again } } // Prevents 100% CPU try { Thread.sleep(1); } catch (InterruptedException e) { Thread.interrupted(); logger.warn("Monitor thread was interrupted on wait"); // Printed on exit from JVM } } catch (Exception e) { if (e instanceof IOException &amp;&amp; (! (e instanceof AsynchronousCloseException || e instanceof ClosedByInterruptException)) ) reconnect = true; // reconnect only if some thread did not intendedly closed a connection logger.error("Error in processing message from @" + (s != null ? s.getRemoteSocketAddress() : ""), e); } finally { try { if (s != null &amp;&amp; !succ) s.close(); } catch (IOException e) { logger.error("Error when closing socket to @" + (s != null ? s.getRemoteSocketAddress() : ""), e); } finally { try { if (chan != null &amp;&amp; !succ) chan.close(); } catch (IOException e) { logger.error("Error when closing socket channel to @" + (s != null ? s.getRemoteSocketAddress() : ""), e); } } } } } catch (Exception e) { if (e instanceof IOException) reconnect = true; logger.error("Error in monitoring thread", e); } finally { try { if (serverSocket != null) serverSocket.close(); } catch (Exception e) { if (e instanceof IOException) reconnect = true; logger.error("Error when closing a server socket", e); } finally { try { if (ssc != null) ssc.close(); } catch (Exception e) { if (e instanceof IOException) reconnect = true; logger.error("Error when closing a server socket channel", e); } } } if (t.isInterrupted()) return; // interrupted - exit if (!reconnect) return; // some IOExceptions can be handled by a reconnect final long maxRestart = this.reconnectCounter.getAndDecrement(); // counter if (maxRestart &lt;= 0) return; // maxRestart reached - exit restart(maxRestart - 1); // restart this runnable } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload