使用Client ConnectionFactory停止TCP适配器

时间:2018-06-04 05:51:30

标签: spring-integration spring-integration-dsl

我正在尝试使用控制总线命令来停止带有客户端连接工厂的TCP适配器,即@AdapterID.stop() 创建流程的代码如下:

 IntegrationFlow flow = IntegrationFlows.from(Tcp.inboundAdapter(Tcp.nioClient("127.0.0.1",3226))
             .serializer(customSerializer)
             .deserializer(customSerializer)
             .id("abc")).clientMode(true).retryInterval(1000).errorChannel("testChannel"))
             .enrichHeaders(f->f.header("abc","abc"))
             .channel(directChannel())
             .handle(Jms.outboundAdapter(ConnectionFactory())
             .destination(hostConnection.getConnectionNumber()))
             .get();

     theFlow = this.flowContext.registration(flow).id("out.flow").register(); 

当我使用控制总线停止适配器时:

 public String stopConnectionAdapter(String connectionName) {
    MessageChannel controlChannel = ac.getBean("controlBus.input", MessageChannel.class); 
    String exp = "@"+connectionName+".stop()";
    controlChannel.send(new GenericMessage<String>(exp));
    return "STOPPED";
}

适配器已停止,但始终在控制台上打印以下异常

2018-06-04 11:09:45.551 ERROR 34860 --- [ask-scheduler-7] o.s.i.i.t.c.ClientModeConnectionManager  : Could not establish connection using conncr3, host=127.0.0.1, port=3226

java.io.IOException: conncr3, host=127.0.0.1, port=3226 connection factory has not been started
at org.springframework.integration.ip.tcp.connection.AbstractConnectionFactory.checkActive(AbstractConnectionFactory.java:873) ~[spring-integration-ip-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.integration.ip.tcp.connection.TcpNioClientConnectionFactory.checkActive(TcpNioClientConnectionFactory.java:68) ~[spring-integration-ip-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.integration.ip.tcp.connection.AbstractClientConnectionFactory.getConnection(AbstractClientConnectionFactory.java:68) ~[spring-integration-ip-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.integration.ip.tcp.connection.AbstractClientConnectionFactory.getConnection(AbstractClientConnectionFactory.java:33) ~[spring-integration-ip-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.integration.ip.tcp.connection.ClientModeConnectionManager.run(ClientModeConnectionManager.java:55) ~[spring-integration-ip-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) [spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_111]
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308) [na:1.8.0_111]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180) [na:1.8.0_111]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) [na:1.8.0_111]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_111]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_111]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_111]

如何停止此错误并关闭连接而不会出现任何异常和错误。

此外,以预期方式顺序处理(在运行时启动和停止)这些连接(积分流组件)的有效方法是什么,没有任何错误?

2 个答案:

答案 0 :(得分:1)

.clientMode(true)是罪魁祸首。我试图在流定义中停止提供id为“abc”的ConnectionFactory .id("abc")).clientMode(true).retryInterval(1000).errorChannel("testChannel"))

现在,我已向InboundAdapter添加了一个ID,如下所示: .id("abc")).clientMode(true).retryInterval(1000).errorChannel("testChannel").id("test"))

使用控制总线首先关闭connectionFactory,然后关闭适配器 @abc.stop() @test.stop() 上述错误将得到解决。

但我仍然不知道以有效的方式停止IntegrationFlow的合适方法,即我必须为每个组件提供一个唯一的ID,并使用Control Bus命令逐个停止。

答案 1 :(得分:1)

你不应该停止工厂;它的生命周期由适配器控制;只需停止适配器;没有必要停止其他组件。

您还可以stop()整个IntegrationFlow bean(out.flow),它将停止组件。