Reputation: 396
I need to implement a TCP server which basicliy should open a socket as part of handshake with a client .
after socket is open server need to keep the socket open , and be able to push a message from the server to the client via the open socket
I look on some spring integration examples but not sure that examples I saw are actully a reference to my requiremnt .
1 .does spring integration tcp has a this ability to keep open socket and send a message from server to client ?
the server should support as well incoming requests
client side implemenatation is a mock written as a simple Tcp java client
Thanks! Nir
here context configuration
<int-ip:tcp-connection-factory id="server"
type="server"
port="5679"
host="localhost"
single-use="false"
deserializer="javaDeserializer"
serializer="javaSerializer" />
<int-ip:tcp-inbound-channel-adapter id="inboundServer"
channel="inloop"
connection-factory="server" client-mode="false"/>
<int-ip:tcp-outbound-channel-adapter id="outboundServer"
channel="outloop"
connection-factory="server" client-mode="false"/>
<channel id="inloop"/>
<channel id="outloop"/>
on the server side I use
outputchanel.send(new GenericMessage<String>("HI World from server16\n",header));
and on the client side read push message with
BufferedReader stdIn = new BufferedReader(new InputStreamReader(socketClient.getInputStream()));
while ((serverResponse = stdIn.readLine()) != null) {
_logger.info("RESPONSE FROM SERVER::"+serverResponse);
}
the client side is a java base tcp client not configure with spring integration , this is a mock client for future integration
for support echo server for a request with byte array not terminate with '\n' , I extend AbstractByteArraySerializer , and override deserialize
public byte[] deserialize(InputStream inputStream) throws IOException {
_Logger.trace("start deserialize");
byte[] result;
try {
byte[] buffer = new byte[getMaxMessageSize()];
int numOfBytes = inputStream.read(buffer, 0, buffer.length);
result = copyToSizedArray(buffer, numOfBytes);
} catch (IOException e) {
_Logger.error("Exception on deserialize tcp inbound stream ", e);
//publishEvent(e, , n);
throw e;
}
return result;
}
Upvotes: 1
Views: 2750
Reputation: 174494
You can use collaborating channel adapters for completely arbitrary messaging between peers.
See TCP Events.
The tcp inbound channel adapter (actually the connection factory) will publish a TcpConnectionOpenEvent
when the client connects.
You can use an ApplicationListener
to receive these events.
The event contains a connectionId
. You can then start sending messages to a tcp outbound channel adapter with this connectionId
in the header named ip_connectionId
(IpHeaders.CONNECTION_ID
).
Inbound messages (if any) from the client received by the inbound adapter will have the same value in the header.
Simply configure a server connection factory and configure both adapters to use it.
If the server has to open the socket, use client-mode="true"
and inject a client connection factory.
Upvotes: 1