INT-3473: Set Inactive TcpNioFactory Before Stop

JIRA: https://jira.spring.io/browse/INT-3473

A simple fix to prevent `Selector closed` error loging message on `TcpNioServerConnectionFactory#stop()`

Polishing
This commit is contained in:
Artem Bilan
2014-07-16 12:45:14 +03:00
committed by Gary Russell
parent f0eeb3b2b9
commit dae634346f

View File

@@ -20,9 +20,7 @@ import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;
import java.nio.channels.CancelledKeyException;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.ClosedSelectorException;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
@@ -116,14 +114,11 @@ public class TcpNioServerConnectionFactory extends AbstractServerConnectionFacto
* When a socket is ready for reading, unregisters the read interest and
* schedules a call to doRead which reads all available data. When the read
* is complete, the socket is again registered for read interest.
* @param server
* @param selector
* @param server the ServerSocketChannel to select
* @param selector the Selector multiplexor
* @throws IOException
* @throws ClosedChannelException
* @throws SocketException
*/
private void doSelect(ServerSocketChannel server, final Selector selector)
throws IOException, SocketException {
private void doSelect(ServerSocketChannel server, final Selector selector) throws IOException {
while (this.isActive()) {
int soTimeout = this.getSoTimeout();
int selectionCount = 0;
@@ -200,7 +195,8 @@ public class TcpNioServerConnectionFactory extends AbstractServerConnectionFacto
TcpConnectionSupport wrappedConnection = wrapConnection(connection);
this.initializeConnection(wrappedConnection, socketChannel.socket());
return connection;
} catch (Exception e) {
}
catch (Exception e) {
logger.error("Failed to establish new incoming connection", e);
return null;
}
@@ -208,6 +204,7 @@ public class TcpNioServerConnectionFactory extends AbstractServerConnectionFacto
@Override
public void stop() {
this.setActive(false);
if (this.selector != null) {
try {
this.selector.close();
@@ -216,14 +213,17 @@ public class TcpNioServerConnectionFactory extends AbstractServerConnectionFacto
logger.error("Error closing selector", e);
}
}
if (this.serverChannel == null) {
return;
if (this.serverChannel != null) {
try {
this.serverChannel.close();
}
catch (IOException e) {
}
finally {
this.serverChannel = null;
}
}
try {
this.serverChannel.close();
}
catch (IOException e) {}
this.serverChannel = null;
super.stop();
}