Skip to content

Instantly share code, notes, and snippets.

@garyrussell
Created November 6, 2013 18:06
Show Gist options
  • Save garyrussell/7341115 to your computer and use it in GitHub Desktop.
Save garyrussell/7341115 to your computer and use it in GitHub Desktop.
Custom Socket Factory to Configure local TCP Port Bear in mind this will fail if the port is already in use (even if it's in TIME_WAIT since the last use)
<int-ip:tcp-connection-factory id="client"
type="client"
host="localhost"
port="${availableServerSocket}"
single-use="true"
socket-factory-support="localPortProvider"
so-timeout="10000"/>
<bean id="localPortProvider" class="foo.LocallPortTcpSocketFactorySupport">
<constructor-arg value="10.0.0.3" />
<constructor-arg value="12345" />
</bean>
/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package foo;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.net.ServerSocketFactory;
import javax.net.SocketFactory;
import org.springframework.integration.ip.tcp.connection.support.TcpSocketFactorySupport;
/**
* @author Gary Russell
*
*/
public class LocallPortTcpSocketFactorySupport implements TcpSocketFactorySupport {
private final int localPort;
private final InetAddress localInterface;
private LocallPortTcpSocketFactorySupport(String localInterfaceIp, int localPort) throws IOException {
this.localInterface = InetAddress.getByName(localInterfaceIp);
this.localPort = localPort;
}
@Override
public ServerSocketFactory getServerSocketFactory() {
throw new UnsupportedOperationException("This does not support server sockets");
}
@Override
public SocketFactory getSocketFactory() {
return new MySocketFactory();
}
private class MySocketFactory extends SocketFactory {
private final SocketFactory delegate = SocketFactory.getDefault();
@Override
public Socket createSocket() throws IOException {
return delegate.createSocket();
}
@Override
public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
return this.delegate.createSocket(host, port, localInterface, localPort);
}
@Override
public Socket createSocket(InetAddress host, int port) throws IOException {
return this.delegate.createSocket(host, port);
}
@Override
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException,
UnknownHostException {
return this.delegate.createSocket(host, port, localHost, localPort);
}
@Override
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
return this.delegate.createSocket(address, port, localAddress, localPort);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment