I have a small programme that listens to a port 30003. It is a server class that processes bytestream data as follows:
sbsSocket = new Socket(sbsHost, sbsPort);
sbsReader = new BufferedReader(new InputStreamReader(sbsSocket.getInputStream()));
private void startSBSMessageReceiverThread() {
new Thread(new Runnable() {
public void run() {
while(true) {
try {
String message = sbsReader.readLine();
// System.out.println(message);
tracker.handleSBSMessage(message);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}).start();
}
This works fine – however I want to move towards using Spring Integration. Here is my tcp-context for something that is meant to achieve the same thing:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:ip="http://www.springframework.org/schema/integration/ip"
xsi:schemaLocation="http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-2.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/integration/ip
http://www.springframework.org/schema/integration/ip/spring-integration-ip-2.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<context:component-scan base-package="com.atlaschase.falcon.integration"></context:component-scan>
<int:channel id="heathrowChannel"></int:channel>
<ip:tcp-connection-factory id="heathrowConnectionFactory" type="server" host="127.0.0.1" port="30003"/>
<ip:tcp-inbound-channel-adapter id="heathrowInboundAdapter" channel="heathrowChannel" connection-factory="heathrowConnectionFactory"/>
<int:service-activator id="adsbHandler" input-channel="heathrowChannel" ref="sbsListener"/>
</beans>
When I start the programme using the Spring Integration approach, I get the following stack trace – telling me that the socket address is already in use.
SEVERE: Error on ServerSocket
java.net.BindException: Address already in use: JVM_Bind
at java.net.PlainSocketImpl.socketBind(Native Method)
at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:383)
at java.net.ServerSocket.bind(ServerSocket.java:328)
at java.net.ServerSocket.<init>(ServerSocket.java:194)
at java.net.ServerSocket.<init>(ServerSocket.java:150)
at javax.net.DefaultServerSocketFactory.createServerSocket(ServerSocketFactory.java:163)
at org.springframework.integration.ip.tcp.connection.TcpNetServerConnectionFactory.run(TcpNetServerConnectionFactory.java:61)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
I don’t understand why when my standard socket programme works fine – why the integration approach falls over. There is a bit of software on my machine that makes data available on the 30003 port.
I hope you can help.
Thanks
This is solved by clearing up confusion between server and clients. I could not generate a server socket as an application was already bound to that port number.
To ‘listen’ to the port ‘30003’ I had to generate a connectionFactory of type ‘client’: