Spring Integration

org.springframework.integration.test.util
Class SocketUtils

java.lang.Object
  extended by org.springframework.integration.test.util.SocketUtils

public final class SocketUtils
extends java.lang.Object

Contains several socket-specific utility methods. For example, you may have test cases that require an open port. Rather than hard-coding the relevant port, it will be better to use methods from this utility class to automatically select an open port, therefore improving the portability of your test-cases across systems.

Since:
2.2
Author:
Gunnar Hillert

Field Summary
static int DEFAULT_PORT_RANGE_MAX
           
static int DEFAULT_PORT_RANGE_MIN
           
 
Constructor Summary
SocketUtils()
          The constructor is intentionally public.
 
Method Summary
static int findAvailableServerSocket()
          Determines a free available server socket (port) using an automatically chosen start seed port.
static int findAvailableServerSocket(int seed)
          Determines a free available server socket (port) using the 'seed' value as the starting port.
static java.util.List<java.lang.Integer> findAvailableServerSockets(int seed, int numberOfRequestedPorts)
          Determines a free available server socket (port) using the 'seed' value as the starting port.
static int findAvailableUdpSocket()
          Determines a free available Udp socket using an automatically chosen start seed port.
static int findAvailableUdpSocket(int seed)
          Determines a free available Udp socket (port) using the 'seed' value as the starting port.
static java.util.List<java.lang.Integer> findAvailableUdpSockets(int seed, int numberOfRequestedPorts)
          Determines free available udp socket(s) (port) using the 'seed' value as the starting port.
static int getRandomSeedPort()
          Determines a random seed port number within the port range 10000 and 60000.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_PORT_RANGE_MIN

public static final int DEFAULT_PORT_RANGE_MIN
See Also:
Constant Field Values

DEFAULT_PORT_RANGE_MAX

public static final int DEFAULT_PORT_RANGE_MAX
See Also:
Constant Field Values
Constructor Detail

SocketUtils

public SocketUtils()
The constructor is intentionally public. In several test cases you may have the need to use the methods of this class multiple times from within your Spring Application Context XML file using SpEL. Of course you can do:
 ...port="#{T(org.springframework.integration.test.util.SocketUtils).findAvailableServerSocket(12000)}"
 
 
But unfortunately, you would need to repeat the package for each usage. This will be acceptable for single use, but if you need to invoke the methods numerous time, you may instead want to do this:
 <bean id="tcpIpUtils" class="org.springframework.integration.test.util.SocketUtils" />

 ...port="#{tcpIpUtils.findAvailableServerSocket(12000)}"
 
 

Method Detail

findAvailableServerSocket

public static int findAvailableServerSocket(int seed)
Determines a free available server socket (port) using the 'seed' value as the starting port. The utility methods will probe for 200 sockets but will return as soon an open port is found.

Parameters:
seed - The starting port, which must not be negative.
Returns:
An available port number
Throws:
java.lang.IllegalStateException - when no open port was found.

findAvailableServerSockets

public static java.util.List<java.lang.Integer> findAvailableServerSockets(int seed,
                                                                           int numberOfRequestedPorts)
Determines a free available server socket (port) using the 'seed' value as the starting port. The utility methods will probe for 200 sockets but will return as soon an open port is found.

Parameters:
seed - The starting port, which must not be negative.
numberOfRequestedPorts - How many open ports shall be retrieved?
Returns:
A list containing the requested number of open ports
Throws:
java.lang.IllegalStateException - when no open port was found.

findAvailableServerSocket

public static int findAvailableServerSocket()
Determines a free available server socket (port) using an automatically chosen start seed port.

Returns:
An available port number
Throws:
java.lang.IllegalStateException - when no open port was found.

findAvailableUdpSocket

public static int findAvailableUdpSocket(int seed)
Determines a free available Udp socket (port) using the 'seed' value as the starting port. The utility methods will probe for 200 sockets but will return as soon an open port is found.

Parameters:
seed - The starting port, which must not be negative.
Returns:
An available port number
Throws:
java.lang.IllegalStateException - when no open port was found.

findAvailableUdpSockets

public static java.util.List<java.lang.Integer> findAvailableUdpSockets(int seed,
                                                                        int numberOfRequestedPorts)
Determines free available udp socket(s) (port) using the 'seed' value as the starting port. The utility methods will probe for 200 sockets but will return as soon an open port is found.

Parameters:
seed - The starting port, which must not be negative.
numberOfRequestedPorts - How many open ports shall be retrieved?
Returns:
A list containing the requested number of open ports
Throws:
java.lang.IllegalStateException - when no open port was found.

findAvailableUdpSocket

public static int findAvailableUdpSocket()
Determines a free available Udp socket using an automatically chosen start seed port.

Returns:
An available port number
Throws:
java.lang.IllegalStateException - when no open port was found.

getRandomSeedPort

public static int getRandomSeedPort()
Determines a random seed port number within the port range 10000 and 60000.

Returns:
A number with the the specified range

Spring Integration