0
votes

Still new to spring and spring integration so please bear with me. =)

I have set up a client to connect to a remote server using TCP. The server sends a message as soon as a connection is established. Using ngrep I have verified that the connection is up and that the message is sent from the server.

By using the gateway interface "gw" I can successfully receive the message. However what I would like to do is to trigger the com.example.Client.onMessage method when a message is received. My understanding is that this should be possible using a ServiceActivator as shown below. Is this true or do I have to use my own dedicated thread doing a blocking receive? Thanks.

Configuration

  <bean id="javaDeserializer"
      class="org.springframework.integration.ip.tcp.serializer.ByteArrayLfSerializer" />

  <int-ip:tcp-connection-factory id="client"
    type="client" host="localhost" port="12000"
    single-use="false" so-timeout="10000" so-keep-alive="true" deserializer="javaDeserializer"
    serializer="javaSerializer"/>

  <int:gateway id="gw" service-interface="com.example.Interface"
    default-request-channel="input" default-reply-channel="replies" />

  <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" retry-interval="10000" auto-startup="true" />

  <int:service-activator input-channel="input" output-channel="replies" ref="com.example.Client" method="onMessage" />

ServiceActivator

@EnableIntegration
@IntegrationComponentScan
@MessageEndpoint
public class Client {

    @ServiceActivator
    public void onMessage(byte[] received) {
        //Not called
    }
}
1

1 Answers

2
votes
  1. @EnableIntegration and @IntegrationComponentScan must be configured on the @Configuration. The general Spring annotations configuration principles: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-java. Although having an XML config you don't need those annotations at all.

  2. If you want to receive messages from TCP to the <service-activator>, you have to configure its input-channel to the replies.

  3. Right now you have a mess in your config: several subscribers for the input channel. In this case they receive incoming messages via round-robin manner.

  4. If I understand correctly you should remove all the <int:gateway> staff and just perform step #2. Although it isn't clear how you are going to send messages to the input channel...