使用 Spring Integration 通过 TCP 连接“只写" [英] 'Write-Only' over a TCP connection with Spring Integration
问题描述
我使用 int-ip:tcp-connection-factory
和 int-ip:tcp-outbound-gateway
与外部服务器通信.服务器提供的大多数服务的协议遵循标准的请求 - 响应风格......效果很好.但是,有几种情况,我只需要发送请求而无需响应.
I am using int-ip:tcp-connection-factory
and int-ip:tcp-outbound-gateway
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.
我的问题是,如何配置我的频道和连接,以便我可以指定是否等待响应?目前我找不到方法,即使我不期待响应,Spring 也总是在发送请求后阻塞.
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.
按照建议,我使用了 tcp-outbound-channel-adapter
.我的配置文件只有以下内容:
As suggested, I have used a tcp-outbound-channel-adapter
. My config file has only the followings:
<int:channel id="requestChannel" />
<int-ip:tcp-outbound-channel-adapter
channel="requestChannel" connection-factory="client" />
<int-ip:tcp-connection-factory id="client"
type="client" host="smtp.gmail.com" port="587" single-use="false"
so-timeout="10000" />
这是我的主课:
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()...");
}
}
最后这是我在日志中得到的:
Finally this is what I get in my log:
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()...
我已将 Spring 的日志记录设置为调试级别,以便为您提供更多详细信息.从日志的最后一行可以看出,my main 退出了.但是,不幸的是,应用程序不会在 [pool-1-thread-1]
继续运行时终止.一旦 send
在 requestChannel
上被调用,这个线程就会活跃起来.知道这里发生了什么吗?
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 [pool-1-thread-1]
continues running. This thread comes to life as soon as send
is invoked on requestChannel
. Any idea what's going on here?
[在此示例中,我将在应用程序连接到 Google 后立即发送 SMTP QUIT 消息.实际上,我实际上不会从 QUIT 开始.例如,一开始我可能会从 HELO 消息开始.我尝试在 tcp-inbound-channel-adapter
中挂钩以获得消息响应,效果很好.问题在于我不希望得到回复的消息.]
[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 tcp-inbound-channel-adapter
to get the message response and that works great. The problem is with messages where I don't expect a reply.]
推荐答案
所以,我建议你在
中注入一些 'fake' <强>任务执行器:
So, I suggest you to inject into the <int-ip:tcp-connection-factory>
some 'fake' task-executor:
public class NullExecutor implements Executor {
public void execute(Runnable command) {}
}
在这种情况下,您来自 AbstractClientConnectionFactory#obtainConnection()
的连接不会被配置(运行)为从套接字读取.
In this case your connection from AbstractClientConnectionFactory#obtainConnection()
won't be configured (runned) to read from socket.
但是 System.exit(0);
作为 main 的最后一行就足够了.在这种情况下,所有线程都将被终止,如果它们不是守护进程.
However System.exit(0);
as last line of your main is enough. In this case all thread will be terminated, if they aren't daemons.
这篇关于使用 Spring Integration 通过 TCP 连接“只写"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!