You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by de...@apache.org on 2013/07/16 11:29:16 UTC

svn commit: r1503627 - /activemq/trunk/activemq-client/src/main/java/org/apache/activemq/util/IdGenerator.java

Author: dejanb
Date: Tue Jul 16 09:29:16 2013
New Revision: 1503627

URL: http://svn.apache.org/r1503627
Log:
https://issues.apache.org/jira/browse/AMQ-4588 - id generator; some improvements in the socket close routine

Modified:
    activemq/trunk/activemq-client/src/main/java/org/apache/activemq/util/IdGenerator.java

Modified: activemq/trunk/activemq-client/src/main/java/org/apache/activemq/util/IdGenerator.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-client/src/main/java/org/apache/activemq/util/IdGenerator.java?rev=1503627&r1=1503626&r2=1503627&view=diff
==============================================================================
--- activemq/trunk/activemq-client/src/main/java/org/apache/activemq/util/IdGenerator.java (original)
+++ activemq/trunk/activemq-client/src/main/java/org/apache/activemq/util/IdGenerator.java Tue Jul 16 09:29:16 2013
@@ -16,6 +16,7 @@
  */
 package org.apache.activemq.util;
 
+import java.io.IOException;
 import java.net.ServerSocket;
 import java.util.concurrent.atomic.AtomicLong;
 
@@ -50,20 +51,34 @@ public class IdGenerator {
         }
 
         if (canAccessSystemProps) {
+            int idGeneratorPort = 0;
+            ServerSocket ss = null;
             try {
-                int idGeneratorPort = Integer.parseInt(System.getProperty(PROPERTY_IDGENERATOR_PORT, "0"));
+                idGeneratorPort = Integer.parseInt(System.getProperty(PROPERTY_IDGENERATOR_PORT, "0"));
                 LOG.trace("Using port {}", idGeneratorPort);
                 hostName = InetAddressUtil.getLocalHostName();
-                ServerSocket ss = new ServerSocket(idGeneratorPort);
+                ss = new ServerSocket(idGeneratorPort);
                 stub = "-" + ss.getLocalPort() + "-" + System.currentTimeMillis() + "-";
                 Thread.sleep(100);
-                ss.close();
             } catch (Exception ioe) {
                 if (LOG.isTraceEnabled()) {
                     LOG.trace("could not generate unique stub by using DNS and binding to local port", ioe);
                 } else {
                     LOG.warn("could not generate unique stub by using DNS and binding to local port: {} {}", ioe.getClass().getCanonicalName(), ioe.getMessage());
                 }
+            } finally {
+                if (ss != null) {
+                    try {
+                        // TODO: replace the following line with IOHelper.close(ss) when Java 6 support is dropped
+                        ss.close();
+                    } catch (IOException ioe) {
+                        if (LOG.isTraceEnabled()) {
+                            LOG.trace("Closing the server socket failed", ioe);
+                        } else {
+                            LOG.warn("Closing the server socket failed" + " due " + ioe.getMessage());
+                        }
+                    }
+                }
             }
         }
         // fallback