|
33 | 33 | import java.nio.channels.ServerSocketChannel; |
34 | 34 | import java.nio.channels.SocketChannel; |
35 | 35 | import java.util.ArrayList; |
| 36 | +import java.util.HashSet; |
36 | 37 | import java.util.Iterator; |
37 | 38 | import java.util.List; |
38 | 39 | import java.util.Set; |
@@ -75,6 +76,7 @@ public abstract class NioConnection implements Callable<Boolean> { |
75 | 76 | protected ExecutorService _executor; |
76 | 77 | protected ExecutorService _sslHandshakeExecutor; |
77 | 78 | protected CAService caService; |
| 79 | + protected Set<SocketChannel> socketChannels = new HashSet<>(); |
78 | 80 |
|
79 | 81 | public NioConnection(final String name, final int port, final int workers, final HandlerFactory factory) { |
80 | 82 | _name = name; |
@@ -230,6 +232,7 @@ public void run() { |
230 | 232 | link.setSSLEngine(sslEngine); |
231 | 233 | link.setKey(socketChannel.register(key.selector(), SelectionKey.OP_READ, link)); |
232 | 234 | final Task task = _factory.create(Task.Type.CONNECT, link, null); |
| 235 | + socketChannels.add(socketChannel); |
233 | 236 | registerLink(saddr, link); |
234 | 237 | _executor.submit(task); |
235 | 238 | } catch (IOException e) { |
@@ -496,6 +499,16 @@ public void close(final SelectionKey key) { |
496 | 499 |
|
497 | 500 | /* Release the resource used by the instance */ |
498 | 501 | public void cleanUp() throws IOException { |
| 502 | + for (SocketChannel channel : socketChannels) { |
| 503 | + if (channel != null && channel.isOpen()) { |
| 504 | + try { |
| 505 | + logger.info(String.format("Closing connection: %s", channel.getRemoteAddress())); |
| 506 | + channel.close(); |
| 507 | + } catch (IOException e) { |
| 508 | + logger.warn(String.format("Unable to close connection due to %s", e.getMessage())); |
| 509 | + } |
| 510 | + } |
| 511 | + } |
499 | 512 | if (_selector != null) { |
500 | 513 | _selector.close(); |
501 | 514 | } |
|
0 commit comments