You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2016/01/14 15:54:00 UTC

[2/3] camel git commit: Improve shutdown of listener for consumer

Improve shutdown of listener for consumer


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/5b0fbd34
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/5b0fbd34
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/5b0fbd34

Branch: refs/heads/master
Commit: 5b0fbd344a1f4fff3c848691a38d4faf6029e322
Parents: 15e72ee
Author: Quinn Stevenson <qu...@pronoia-solutions.com>
Authored: Mon Jan 11 14:15:10 2016 -0700
Committer: Claus Ibsen <da...@apache.org>
Committed: Thu Jan 14 14:22:39 2016 +0000

----------------------------------------------------------------------
 .../component/mllp/MllpTcpServerConsumer.java   | 48 +++++++++++++++++++-
 1 file changed, 46 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/5b0fbd34/components/camel-mllp/src/main/java/org/apache/camel/component/mllp/MllpTcpServerConsumer.java
----------------------------------------------------------------------
diff --git a/components/camel-mllp/src/main/java/org/apache/camel/component/mllp/MllpTcpServerConsumer.java b/components/camel-mllp/src/main/java/org/apache/camel/component/mllp/MllpTcpServerConsumer.java
index 186a462..33568da 100644
--- a/components/camel-mllp/src/main/java/org/apache/camel/component/mllp/MllpTcpServerConsumer.java
+++ b/components/camel-mllp/src/main/java/org/apache/camel/component/mllp/MllpTcpServerConsumer.java
@@ -21,6 +21,7 @@ import java.io.InputStream;
 import java.net.InetSocketAddress;
 import java.net.ServerSocket;
 import java.net.Socket;
+import java.net.SocketException;
 import java.net.SocketTimeoutException;
 import java.nio.charset.Charset;
 import java.util.ArrayList;
@@ -194,12 +195,33 @@ public class MllpTcpServerConsumer extends DefaultConsumer {
             log.debug("Starting acceptor thread");
 
             while (null != serverSocket && serverSocket.isBound() && !serverSocket.isClosed()) {
-                try {
                     /* ? set this here ? */
                     // serverSocket.setSoTimeout( 10000 );
                     // TODO: Need to check maxConnections and figure out what to do when exceeded
-                    Socket socket = serverSocket.accept();
+                Socket socket = null;
+                try {
+                    socket = serverSocket.accept();
+                } catch (SocketException socketEx) {
+                    // This should happen if the component is closed while the accept call is blocking
+                    if (serverSocket.isBound()) {
+                        try {
+                            serverSocket.close();
+                        } catch (Exception ex) {
+                            log.info("Exception encountered closing ServerSocket after SocketException on accept()", ex);
+                        }
+                    }
+                } catch (IOException ioEx) {
+                    log.error("Exception encountered accepting connection - closing ServerSocket", ioEx);
+                    if (serverSocket.isBound()) {
+                        try {
+                            serverSocket.close();
+                        } catch (Exception ex) {
+                            log.info("Exception encountered closing ServerSocket after exception on accept()", ex);
+                        }
+                    }
+                }
 
+                try {
                     /* Wait a bit and then check and see if the socket is really there - it could be a load balancer
                      pinging the port
                       */
@@ -266,8 +288,30 @@ public class MllpTcpServerConsumer extends DefaultConsumer {
                     log.error("Exception accepting new connection", ex);
                 }
             }
+
+            log.info("ServerSocket.accept loop finished - closing listener");
+            if (null != serverSocket  &&  serverSocket.isBound()  &&  !serverSocket.isClosed()) {
+                try {
+                    serverSocket.close();
+                } catch (Exception ex) {
+                    log.warn("Exception encountered closing ServerSocket after accept loop had exited", ex);
+                }
+            }
         }
 
+        @Override
+        public void interrupt() {
+            super.interrupt();
+            if (null != serverSocket) {
+                if (serverSocket.isBound()) {
+                    try {
+                        serverSocket.close();
+                    } catch (IOException ioEx ) {
+                        log.warn("Exception encountered closing ServerSocket in interrupt() method", ioEx);
+                    }
+                }
+            }
+        }
     }
 
     class ClientSocketThread extends Thread {