Dariush Jafari
Dariush Jafari

Reputation: 5453

spring integration tcp client send simple message

I've been developing a project using spring framework 4. I'm trying to create a simple TCP client via spring-integration-ip library. I've adjusted all configurations:

applicationContext.xml

...
<int:channel id="tcpChannel" />

<int-ip:tcp-outbound-channel-adapter id="outboundClient"
channel="tcpChannel"
connection-factory="tcpConnectionFactory"/>

...

bean configuration:

@Configuration
public class MyConfiguration{

   @Bean
   public AbstractClientConnectionFactory tcpConnectionFactory() { 
       return new TcpNetClientConnectionFactory("localhost", 2345);
   }
} 

I've read all documentations about spring tcp here.
I guess I must use tcp-outbound-channel-adapter or gateway to send messages. but I wonder how to use it; what method should I invoke. I'm not supposed to receive any messages from server.

Upvotes: 1

Views: 1490

Answers (1)

Dariush Jafari
Dariush Jafari

Reputation: 5453

I found the solution. I didn't need gateway. spring messaging gateways have been designed to implement the request-response scenario. So the only thing I need to do is that I send message vi channel. Perhaps there be some better solutions.

import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.MessageChannel;

public class MyOwnService{

   @Inject
   private MessageChannel channel;

   public void someMethod(String message){
      Message<String> m = MessageBuilder.withPayload(message).build();
      channel.send(m);
   }
}

Upvotes: 1

Related Questions