You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ch...@apache.org on 2006/07/01 01:06:55 UTC

svn commit: r418413 - /incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/transport/udp/UdpTransport.java

Author: chirino
Date: Fri Jun 30 16:06:54 2006
New Revision: 418413

URL: http://svn.apache.org/viewvc?rev=418413&view=rev
Log:
http://issues.apache.org/activemq/browse/AMQ-787

Modified:
    incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/transport/udp/UdpTransport.java

Modified: incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/transport/udp/UdpTransport.java
URL: http://svn.apache.org/viewvc/incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/transport/udp/UdpTransport.java?rev=418413&r1=418412&r2=418413&view=diff
==============================================================================
--- incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/transport/udp/UdpTransport.java (original)
+++ incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/transport/udp/UdpTransport.java Fri Jun 30 16:06:54 2006
@@ -33,6 +33,7 @@
 
 import java.io.EOFException;
 import java.io.IOException;
+import java.net.BindException;
 import java.net.DatagramSocket;
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
@@ -51,6 +52,9 @@
 public class UdpTransport extends TransportThreadSupport implements Transport, Service, Runnable {
     private static final Log log = LogFactory.getLog(UdpTransport.class);
 
+	private static final int MAX_BIND_ATTEMPTS = 50;
+	private static final long BIND_ATTEMPT_DELAY = 100;
+
     private CommandChannel commandChannel;
     private OpenWireFormat wireFormat;
     private ByteBufferPool bufferPool;
@@ -387,7 +391,26 @@
         if (log.isDebugEnabled()) {
             log.debug("Binding to address: " + localAddress);
         }
-        socket.bind(localAddress);
+        
+        //
+        // We have noticed that on some platfoms like linux, after you close down
+        // a previously bound socket, it can take a little while before we can bind it again.
+        // 
+        for(int i=0; i < MAX_BIND_ATTEMPTS; i++){
+			try {
+				socket.bind(localAddress);
+				return;
+			} catch (BindException e) {
+				if ( i+1 == MAX_BIND_ATTEMPTS )
+					throw e;
+				try {
+					Thread.sleep(BIND_ATTEMPT_DELAY);
+				} catch (InterruptedException e1) {
+					throw e;
+				}
+			}			
+    	}
+
     }
 
     protected DatagramChannel connect(DatagramChannel channel, SocketAddress targetAddress2) throws IOException {