You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by cr...@apache.org on 2021/09/26 15:27:56 UTC

[dubbo] branch master updated: [master] add destroy() calling to close tomcat server port (#8921)

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

crazyhzm pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/master by this push:
     new d1644b7  [master] add destroy() calling to close tomcat server port (#8921)
d1644b7 is described below

commit d1644b760d3b2f997daa532752b9cd0603eb93ef
Author: zrlw <zr...@sina.com>
AuthorDate: Sun Sep 26 23:27:38 2021 +0800

    [master] add destroy() calling to close tomcat server port (#8921)
    
    * add destroy() calling to close tomcat server port
    
    * improve javadoc for isPortInUsed()
---
 .../main/java/org/apache/dubbo/common/utils/NetUtils.java | 15 +++++++++++++++
 .../dubbo/remoting/http/tomcat/TomcatHttpServer.java      |  2 ++
 .../dubbo/remoting/http/tomcat/TomcatHttpBinderTest.java  |  1 +
 3 files changed, 18 insertions(+)

diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java
index 07f84e8..7f1aa86 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java
@@ -118,6 +118,21 @@ public class NetUtils {
         return port;
     }
 
+    /**
+     * Check the port whether is in use in os
+     * 
+     * @param port the port to check if in use
+     * @return is the given port in use or not
+     */
+    public static boolean isPortInUsed(int port) {
+        try (ServerSocket ignored = new ServerSocket(port)) {
+            return false;
+        } catch (IOException e) {
+            // continue
+        }
+        return true;
+    }
+
     public static boolean isInvalidPort(int port) {
         return port < MIN_PORT || port > MAX_PORT;
     }
diff --git a/dubbo-remoting/dubbo-remoting-http/src/main/java/org/apache/dubbo/remoting/http/tomcat/TomcatHttpServer.java b/dubbo-remoting/dubbo-remoting-http/src/main/java/org/apache/dubbo/remoting/http/tomcat/TomcatHttpServer.java
index 0711590..2d0b8a1 100755
--- a/dubbo-remoting/dubbo-remoting-http/src/main/java/org/apache/dubbo/remoting/http/tomcat/TomcatHttpServer.java
+++ b/dubbo-remoting/dubbo-remoting-http/src/main/java/org/apache/dubbo/remoting/http/tomcat/TomcatHttpServer.java
@@ -88,6 +88,8 @@ public class TomcatHttpServer extends AbstractHttpServer {
 
         try {
             tomcat.stop();
+            // close port by destroy()
+            tomcat.destroy();
         } catch (Exception e) {
             logger.warn(e.getMessage(), e);
         }
diff --git a/dubbo-remoting/dubbo-remoting-http/src/test/java/org/apache/dubbo/remoting/http/tomcat/TomcatHttpBinderTest.java b/dubbo-remoting/dubbo-remoting-http/src/test/java/org/apache/dubbo/remoting/http/tomcat/TomcatHttpBinderTest.java
index f31b6ce..cbabaa2 100644
--- a/dubbo-remoting/dubbo-remoting-http/src/test/java/org/apache/dubbo/remoting/http/tomcat/TomcatHttpBinderTest.java
+++ b/dubbo-remoting/dubbo-remoting-http/src/test/java/org/apache/dubbo/remoting/http/tomcat/TomcatHttpBinderTest.java
@@ -51,5 +51,6 @@ public class TomcatHttpBinderTest {
         assertThat(response, is("Tomcat"));
 
         httpServer.close();
+        assertThat(NetUtils.isPortInUsed(port), is(false));
     }
 }