Не беспокойся.Я закончил с этим:
// 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
&& (! (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 && !succ) s.close();
} catch (IOException e) {
logger.error("Error when closing socket to @" + (s != null ? s.getRemoteSocketAddress() : ""), e);
} finally {
try {
if (chan != null && !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 <= 0) return; // maxRestart reached - exit
restart(maxRestart - 1); // restart this runnable
}