Note that there are some explanatory texts on larger screens.

plurals
  1. PO'Write-Only' over a TCP connection with Spring Integration
    text
    copied!<p>I am using <code>int-ip:tcp-connection-factory</code> and <code>int-ip:tcp-outbound-gateway</code> to communicate with an external server. The protocol for most of the services offered by the server follows the standard request-response style... which is working great. However, there are a few situations where I only need to send a request and no response is expected.</p> <p>My question is, how do I configure my channels and connections so that I can specify whether or not to wait for a response? Currently I can't find a way and Spring always blocks after sending the request even if I am not expecting a response.</p> <p>EDIT: As suggested, I have used a <code>tcp-outbound-channel-adapter</code>. My config file has only the followings:</p> <pre><code>&lt;int:channel id="requestChannel" /&gt; &lt;int-ip:tcp-outbound-channel-adapter channel="requestChannel" connection-factory="client" /&gt; &lt;int-ip:tcp-connection-factory id="client" type="client" host="smtp.gmail.com" port="587" single-use="false" so-timeout="10000" /&gt; </code></pre> <p>And here's my Main class:</p> <pre><code>public final class Main { private static final Logger LOGGER = Logger.getLogger(Main.class); public static void main(final String... args) throws IOException { LOGGER.debug("entered main()..."); final AbstractApplicationContext context = new ClassPathXmlApplicationContext( "classpath:*-context.xml"); MessageChannel requestChannel = context.getBean("requestChannel", MessageChannel.class); requestChannel.send(MessageBuilder.withPayload("QUIT").build()); LOGGER.debug("exiting main()..."); } } </code></pre> <p>Finally this is what I get in my log:</p> <pre><code>11:57:15.877 INFO [main][com.together.email.Main] entered main()... 11:57:16.295 INFO [main][org.springframework.integration.config.xml.DefaultConfiguringBeanFactoryPostProcessor] No bean named 'errorChannel' has been explicitly defined. Therefore, a default PublishSubscribeChannel will be created. 11:57:16.295 INFO [main][org.springframework.integration.config.xml.DefaultConfiguringBeanFactoryPostProcessor] No bean named 'taskScheduler' has been explicitly defined. Therefore, a default ThreadPoolTaskScheduler will be created. 11:57:16.480 INFO [main][org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory] started client 11:57:16.480 INFO [main][org.springframework.integration.endpoint.EventDrivenConsumer] Adding {ip:tcp-outbound-channel-adapter} as a subscriber to the 'requestChannel' channel 11:57:16.480 INFO [main][org.springframework.integration.channel.DirectChannel] Channel 'requestChannel' has 1 subscriber(s). 11:57:16.480 INFO [main][org.springframework.integration.endpoint.EventDrivenConsumer] started org.springframework.integration.config.ConsumerEndpointFactoryBean#0 11:57:16.480 INFO [main][org.springframework.integration.endpoint.EventDrivenConsumer] Adding {logging-channel-adapter:_org.springframework.integration.errorLogger} as a subscriber to the 'errorChannel' channel 11:57:16.481 INFO [main][org.springframework.integration.channel.PublishSubscribeChannel] Channel 'errorChannel' has 1 subscriber(s). 11:57:16.481 INFO [main][org.springframework.integration.endpoint.EventDrivenConsumer] started _org.springframework.integration.errorLogger 11:57:16.509 DEBUG [main][org.springframework.integration.channel.DirectChannel] preSend on channel 'requestChannel', message: [Payload=QUIT][Headers={timestamp=1381021036509, id=860ebe82-06c6-4393-95c7-0ece1a0a0e5d}] 11:57:16.509 DEBUG [main][org.springframework.integration.ip.tcp.TcpSendingMessageHandler] org.springframework.integration.ip.tcp.TcpSendingMessageHandler#0 received message: [Payload=QUIT][Headers={timestamp=1381021036509, id=860ebe82-06c6-4393-95c7-0ece1a0a0e5d}] 11:57:16.509 DEBUG [main][org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory] Opening new socket connection to smtp.gmail.com:587 11:57:16.745 DEBUG [main][org.springframework.integration.ip.tcp.connection.TcpNetConnection] New connection smtp.gmail.com:587:550c9b68-10a0-442d-b65d-d25d28df306b 11:57:16.748 DEBUG [main][org.springframework.integration.ip.tcp.TcpSendingMessageHandler] Got Connection smtp.gmail.com:587:550c9b68-10a0-442d-b65d-d25d28df306b 11:57:16.749 DEBUG [pool-1-thread-1][org.springframework.integration.ip.tcp.connection.TcpNetConnection] TcpListener exiting - no listener and not single use 11:57:16.750 DEBUG [main][org.springframework.integration.ip.tcp.connection.TcpNetConnection] Message sent [Payload=QUIT][Headers={timestamp=1381021036509, id=860ebe82-06c6-4393-95c7-0ece1a0a0e5d}] 11:57:16.750 DEBUG [main][org.springframework.integration.channel.DirectChannel] postSend (sent=true) on channel 'requestChannel', message: [Payload=QUIT][Headers={timestamp=1381021036509, id=860ebe82-06c6-4393-95c7-0ece1a0a0e5d}] 11:57:16.751 INFO [main][com.together.email.Main] exiting main()... </code></pre> <p>I have set Spring's logging to debug level so that I can give you more details. As you can see from the last line of the log, my main exits. However, unfortunately the application doesn't terminate as <code>[pool-1-thread-1]</code> continues running. This thread comes to life as soon as <code>send</code> is invoked on <code>requestChannel</code>. Any idea what's going on here?</p> <p><em>[In this example, I am sending an SMTP QUIT message as soon as the application connects to Google. In practice, I would actually not start with a QUIT. For example, at the beginning I may start with a HELO message. I have tried hooking in a <code>tcp-inbound-channel-adapter</code> to get the message response and that works great. The problem is with messages where I don't expect a reply.]</em></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload