A fix for OILConnectionFactory on machines with multiple NIC
rastadg Oct 8, 2002 7:16 PMHello,
I'm using the OIL method for JMS connection, but I had some problems connecting from one machine to the other.
I downloaded the jboss sources and looked at the OILClientILService.java, and it struck me that this class doesn't handle hosts with multiple ethernet interfaces.
In my environment all machines have two NICs, one for internet access, and the other for internal access to development servers.
The code, as it is in the OILClientILService.java just binds using new ServerSocket(0), which uses the default interface (and not the one I really want to bind to), so I wrote a new init() method (changes are in bold):
public void init(org.jboss.mq.Connection connection, java.util.Properties props)
throws java.lang.Exception
{
this.connection = connection;
String myIF = System.getProperty("CUSTOM_OIL_BIND_ADDRESS");
cat.debug("CUSTOM_OIL_BIND_ADDRESS=" + myIF);
java.net.InetSocketAddress myAddress = null;
if( myIF != null ) {
// Select correct interface for systems with more than on IF
serverSocket = new ServerSocket();
myAddress = new java.net.InetSocketAddress(myIF, 0);
serverSocket.bind( myAddress );
}
else {
// Use default IF
serverSocket = new ServerSocket(0);
}
cat.debug("Connection settings: " + ((java.net.InetSocketAddress)serverSocket.getLocalSocketAddress()).getHostName() +":" + serverSocket.getLocalPort());
String t = props.getProperty(OILServerILFactory.OIL_TCPNODELAY_KEY);
if (t != null)
enableTcpNoDelay = t.equals("yes");
if( myIF == null ) {
clientIL = new OILClientIL(java.net.InetAddress.getLocalHost(), serverSocket.getLocalPort(), enableTcpNoDelay);
}
else {
clientIL = new OILClientIL(java.net.InetAddress.getByName(myAddress.getHostName()), serverSocket.getLocalPort(), enableTcpNoDelay);
}
}
The last 'if( myIF == null )' block could probably be replaced by a singe line like this:
clientIL = new OILClientIL(((java.net.InetSocketAddress)serverSocket.getLocalSocketAddress()).getHostName(), serverSocket.getLocalPort(), enableTcpNoDelay);
By applying this code I'm now able to use the OIL method between different machines with multiple NICs, and a great improvement in speed compared to the UIL method. However, the client side must pass a system property at startup, like this:
-DCUSTOM_OIL_BIND_ADDRESS=address
Where address is the IP address or hostname for the NIC (or a logical IF) to bind to.
I hope this could be of any help to other people in the same situation (if any)!