You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by or...@apache.org on 2021/09/02 11:56:48 UTC

[camel] branch main updated: (chores) camel-mllp: add note about not a leak (#6030)

This is an automated email from the ASF dual-hosted git repository.

orpiske pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 1b6a306  (chores) camel-mllp: add note about not a leak (#6030)
1b6a306 is described below

commit 1b6a306a935a2d380b650fe8ab1a12decf0c4df5
Author: Otavio Rodolfo Piske <or...@users.noreply.github.com>
AuthorDate: Thu Sep 2 13:56:14 2021 +0200

    (chores) camel-mllp: add note about not a leak (#6030)
    
    Add a comment in the code and simplify it a bit to avoid an impression
    that there is a leak.
---
 .../mllp/internal/TcpServerBindThread.java         | 107 ++++++++++++---------
 1 file changed, 59 insertions(+), 48 deletions(-)

diff --git a/components/camel-mllp/src/main/java/org/apache/camel/component/mllp/internal/TcpServerBindThread.java b/components/camel-mllp/src/main/java/org/apache/camel/component/mllp/internal/TcpServerBindThread.java
index 1d67748..e006297 100644
--- a/components/camel-mllp/src/main/java/org/apache/camel/component/mllp/internal/TcpServerBindThread.java
+++ b/components/camel-mllp/src/main/java/org/apache/camel/component/mllp/internal/TcpServerBindThread.java
@@ -20,6 +20,7 @@ import java.io.IOException;
 import java.net.BindException;
 import java.net.InetSocketAddress;
 import java.net.ServerSocket;
+import java.net.SocketException;
 
 import org.apache.camel.Route;
 import org.apache.camel.component.mllp.MllpTcpServerConsumer;
@@ -66,63 +67,73 @@ public class TcpServerBindThread extends Thread {
         }
 
         try {
+            // Note: this socket is going to be closed in the TcpServerAcceptThread instance
+            // launched by the consumer
             ServerSocket serverSocket = new ServerSocket();
-            if (consumer.getConfiguration().hasReceiveBufferSize()) {
-                serverSocket.setReceiveBufferSize(consumer.getConfiguration().getReceiveBufferSize());
-            }
-
-            if (consumer.getConfiguration().hasReuseAddress()) {
-                serverSocket.setReuseAddress(consumer.getConfiguration().getReuseAddress());
-            }
-
-            // Accept Timeout
-            serverSocket.setSoTimeout(consumer.getConfiguration().getAcceptTimeout());
-
-            InetSocketAddress socketAddress;
-            if (null == consumer.getEndpoint().getHostname()) {
-                socketAddress = new InetSocketAddress(consumer.getEndpoint().getPort());
-            } else {
-                socketAddress = new InetSocketAddress(consumer.getEndpoint().getHostname(), consumer.getEndpoint().getPort());
-            }
+            InetSocketAddress socketAddress = setupSocket(serverSocket);
 
             log.debug("Attempting to bind to {}", socketAddress);
 
-            long startTicks = System.currentTimeMillis();
-            do {
-                try {
-                    if (consumer.getConfiguration().hasBacklog()) {
-                        serverSocket.bind(socketAddress, consumer.getConfiguration().getBacklog());
-                    } else {
-                        serverSocket.bind(socketAddress);
-                    }
-                    consumer.startAcceptThread(serverSocket);
-                } catch (BindException bindException) {
-                    if (System.currentTimeMillis() > startTicks + consumer.getConfiguration().getBindTimeout()) {
-                        log.error("Failed to bind to address {} within timeout {}", socketAddress,
-                                consumer.getConfiguration().getBindTimeout(), bindException);
-                        break;
-                    } else {
-                        log.warn("Failed to bind to address {} - retrying in {} milliseconds", socketAddress,
-                                consumer.getConfiguration().getBindRetryInterval());
-                        try {
-                            Thread.sleep(consumer.getConfiguration().getBindRetryInterval());
-                        } catch (InterruptedException interruptedEx) {
-                            log.info("Bind to address {} interrupted", socketAddress, interruptedEx);
-                            if (!this.isInterrupted()) {
-                                super.interrupt();
-                            }
-                            break;
+            doAccept(serverSocket, socketAddress);
+        } catch (IOException ioEx) {
+            log.error("Unexpected exception encountered initializing ServerSocket before attempting to bind", ioEx);
+        }
+    }
+
+    private void doAccept(ServerSocket serverSocket, InetSocketAddress socketAddress) {
+        long startTicks = System.currentTimeMillis();
+        do {
+            try {
+                if (consumer.getConfiguration().hasBacklog()) {
+                    serverSocket.bind(socketAddress, consumer.getConfiguration().getBacklog());
+                } else {
+                    serverSocket.bind(socketAddress);
+                }
+                consumer.startAcceptThread(serverSocket);
+            } catch (BindException bindException) {
+                if (System.currentTimeMillis() > startTicks + consumer.getConfiguration().getBindTimeout()) {
+                    log.error("Failed to bind to address {} within timeout {}", socketAddress,
+                            consumer.getConfiguration().getBindTimeout(), bindException);
+                    break;
+                } else {
+                    log.warn("Failed to bind to address {} - retrying in {} milliseconds", socketAddress,
+                            consumer.getConfiguration().getBindRetryInterval());
+                    try {
+                        Thread.sleep(consumer.getConfiguration().getBindRetryInterval());
+                    } catch (InterruptedException interruptedEx) {
+                        log.info("Bind to address {} interrupted", socketAddress, interruptedEx);
+                        if (!this.isInterrupted()) {
+                            super.interrupt();
                         }
+                        break;
                     }
-                } catch (IOException unexpectedEx) {
-                    log.error("Unexpected exception encountered binding to address {}", socketAddress, unexpectedEx);
-                    break;
                 }
-            } while (!this.isInterrupted() && !serverSocket.isBound());
+            } catch (IOException unexpectedEx) {
+                log.error("Unexpected exception encountered binding to address {}", socketAddress, unexpectedEx);
+                break;
+            }
+        } while (!this.isInterrupted() && !serverSocket.isBound());
+    }
 
-        } catch (IOException ioEx) {
-            log.error("Unexpected exception encountered initializing ServerSocket before attempting to bind", ioEx);
+    private InetSocketAddress setupSocket(ServerSocket serverSocket) throws SocketException {
+        if (consumer.getConfiguration().hasReceiveBufferSize()) {
+            serverSocket.setReceiveBufferSize(consumer.getConfiguration().getReceiveBufferSize());
+        }
+
+        if (consumer.getConfiguration().hasReuseAddress()) {
+            serverSocket.setReuseAddress(consumer.getConfiguration().getReuseAddress());
+        }
+
+        // Accept Timeout
+        serverSocket.setSoTimeout(consumer.getConfiguration().getAcceptTimeout());
+
+        InetSocketAddress socketAddress;
+        if (null == consumer.getEndpoint().getHostname()) {
+            socketAddress = new InetSocketAddress(consumer.getEndpoint().getPort());
+        } else {
+            socketAddress = new InetSocketAddress(consumer.getEndpoint().getHostname(), consumer.getEndpoint().getPort());
         }
+        return socketAddress;
     }
 
 }