jshark
jshark

Reputation: 113

Uncommon TCP flow with Spring Integration

I need suggestion how to implement, if it is possible, with the Spring integration the following TCP flow:

  1. Only the server side is need.
  2. The TCP server waits for the incoming connection
  3. On connection of the client, server sends data to the client
  4. Client replies with response
  5. Server may reply immediately with the new data or wait for external application events to send new packages to the client.

In groovy the code could be demonstrated as follow:

def serverSocket = new ServerSocket(...)
def connSocket = serverSocket.accept()

connSocket.outputStream.write(...)

while(true) {
   def readBuffer = new byte[256]
   connSocket.inputStream.read(readBuffer)
   if(needToSendBack(readBuffer)) {
       connSocket.outputStream.write(...)
   }
}

def sendByDemand(def data) {
    connSocket.outputStream.write(data)
}

The method sendByDemand could be invoked from the separate thread.

Here is a list of problems which I marked for myself, which prevents me to implement it with the Spring Integration (2.x version):

  1. As far as I understand, the standard "Service Activator" approach cannot work in this scenario, since it is "connection events" driven. So when the application decides to send the new data to the client it cannot use the Service Activator
  2. I have no "On TCP connection" event. I found that version 3.0 comes with the events support in this area, but since I cannot upgrade to 3.0, I implemented the connection check with the interceptors on the connection factory. However, when I know that client is connected, trying using the Direct Channels to send message fails with "no subscribers" error.

If someone could post possible Spring configuration for this scenario or point to the similar flow example it may be very helpful.

Upvotes: 0

Views: 199

Answers (1)

Gary Russell
Gary Russell

Reputation: 174739

Your use case is possible, but it would make your life easier if you could upgrade to 3.0.

'Dispatcher has no subscribers' means there is no consumer subscribed to that channel.

You need to show your configuration; you must use collaborating channel adapters for this (not a gateway).

You need to capture the connectionId of the connection when it is established, and use it to populate the ip_connectionId header so the outbound channel adapter knows which socket to which to write the message.

Upvotes: 0

Related Questions