Well, I have a Spring integrated TCP client. I am trying to connect to remote TCP server and receive data from the socket which is asynchronously written by Server.
But , it so happens that, my client is receiving the first message and not going further with receiving further messages from server socket.(Actually looking at server log you can make out client has lost connection, but why?)
And, one more thing is how can I trigger certain functionality the moment I receive the message? - may be TcpConnectionHandler-handleMessage() or TcpLisetner - onMessage().
Eventually, I would like to have - a Spring TCP client, who connects to remote server and receive data, as it comes on to the socket. Below are my configurations and code:
my config:
<bean id="javaSerializer" class="com.my.client.CustomSerializerDeserializer" />
<bean id="javaDeserializer" class="com.my.client.CustomSerializerDeserializer" />
<context:property-placeholder />
<!-- Client side -->
<int:gateway id="gw" service-interface="com.zebra.client.SimpleGateway" default-request-channel="input" default-reply-channel="replies" />
<int-ip:tcp-connection-factory id="client"
type="client" host="localhost" port="5678" single-use="false"
so-timeout="100000" serializer="javaSerializer" deserializer="javaDeserializer"
so-keep-alive="true" />
<int:channel id="input" />
<int:channel id="replies">
<int:queue />
</int:channel>
<int-ip:tcp-outbound-channel-adapter
id="outboundClient" channel="input" connection-factory="client" />
<int-ip:tcp-inbound-channel-adapter
id="inboundClient" channel="replies" connection-factory="client"
client-mode="true" auto-startup="true" />
My Tcp server:
while(true)
{
try
{
System.out.println("Waiting for client on port " +
serverSocket.getLocalPort() + "...");
Socket server = serverSocket.accept();
System.out.println("Just connected to "
+ server.getRemoteSocketAddress());
DataOutputStream out =
new DataOutputStream(server.getOutputStream());
out.write("ACK\r\n".getBytes());
out.flush();
//server.close();
}catch(SocketTimeoutException s)
{
System.out.println("Socket timed out!");
break;
}catch(IOException e)
{
e.printStackTrace();
break;
}
}
Server log:
Waiting for client on port 5678... Just connected to /127.0.0.1:56108 Waiting for client on port 5678...
My TcpClient:
final GenericXmlApplicationContext context = new GenericXmlApplicationContext();
context.load("classpath:config.xml");
context.registerShutdownHook();
context.refresh();
final SimpleGateway gateway = context.getBean(SimpleGateway.class);
int i=0;
while(i++<10){
String h = gateway.receive();
System.out.println("Received message "+h);
}try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
My client log:
Received message ACK
Received message Received message Received message Received message Received message Received message Received message Received message Received message
My custom deserializer:
@Override
public String deserialize(InputStream inputStream) throws IOException {
// TODO Auto-generated method stub
StringBuilder builder = new StringBuilder();
int size = inputStream.available();
int c;
for (int i = 0; i < size; ++i) {
c = inputStream.read();
if(c!=-1){
builder.append((char)c);
}
else{
break;
}
}
return builder.toString();
}
my gateway:
public interface SimpleGateway {
public String receive();
}
Please let me know if you have more questions.