尝试正常关闭应用程序时,Dispatcher没有订阅者

时间:2018-12-06 15:21:21

标签: spring-integration

我对我的申请有以下要求:

我有一个集成流程,该流程通过

从目录中获取文件
Files.inboundAdapter

和以下轮询配置:

@Bean public PollerSpec orderOutboundFlowTempFileInPoller() {
    return Pollers
        .fixedDelay(pollerDelay)
        .maxMessagesPerPoll(100)
        .transactional();

}

应通过RemoteFileTemplate将文件传输到远程主机。该应用程序在docker容器中运行,该容器应为维护或推出目的而可停止运行。 当容器关闭时,该流应完成将文件写入远程主机,并且不应接受新的传入文件。 因此,我实现了如下正常关闭:

@Override public void onApplicationEvent(final ContextClosedEvent event) {
  LOG.info("Trying to gracefully shutdown App");
  //CHECKSTYLE:OFF
  allFlowPollers.forEach(
    p -> {
      try {
        p.destroy();
      } catch (final Exception e) {
        LOG.warn("Unable to destroy poller.");
      }
    }
  );
  //CHECKSTLYE:ON
  FLOWS_TO_SHUTDOWN.forEach(GracefulShutdownAware::shutdown);
}

我假设销毁轮询器时,不会从源中读取其他文件。 RemoteFileTemplate确实可以正确发送当前文件,没有问题。 但是轮询器似乎仍然会获取新文件,并且在应用程序几乎关闭时,将出现如下异常:

timestamp=15:55:56.599, thread=task-scheduler-2, severity=ERROR, class=o.s.i.h.LoggingHandler, message=org.springframework.messaging.MessageDeliveryException: Dispatcher has no subscribers for channel 'application.flowTempFileIn.channel#0'.; nested exception is org.springframework.integration.MessageDispatchingException: Dispatcher has no subscribers, failedMessage=GenericMessage [payload=/servicedata/tmp/1544088550162_57280.xml, headers={file_originalFile=/servicedata/tmp/1544088550162_57280.xml, id=d04473ff-d1bd-173e-d801-b7b9fd31596c, file_name=1544088550162_57280.xml, file_relativePath=1544088550162_57280.xml, timestamp=1544108156591}], failedMessage=GenericMessage [payload=/servicedata/tmp/1544088550162_57280.xml, headers={file_originalFile=/servicedata/tmp/1544088550162_57280.xml, id=d04473ff-d1bd-173e-d801-b7b9fd31596c, file_name=1544088550162_57280.xml, file_relativePath=1544088550162_57280.xml, timestamp=1544108156591}]
        at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:77)
        at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:445)
        at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:394)
        at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:181)
        at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:160)
        at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:47)
        at org.springframework.messaging.core.AbstractMessageSendingTemplate.send(AbstractMessageSendingTemplate.java:108)
        at org.springframework.integration.endpoint.SourcePollingChannelAdapter.handleMessage(SourcePollingChannelAdapter.java:227)
        at org.springframework.integration.endpoint.AbstractPollingEndpoint.doPoll(AbstractPollingEndpoint.java:290)
        at sun.reflect.GeneratedMethodAccessor292.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:197)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
        at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294)
        at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
        at com.sun.proxy.$Proxy138.call(Unknown Source)
        at org.springframework.integration.endpoint.AbstractPollingEndpoint$Poller.lambda$run$0(AbstractPollingEndpoint.java:391)
        at org.springframework.integration.util.ErrorHandlingTaskExecutor.lambda$execute$0(ErrorHandlingTaskExecutor.java:57)
        at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50)
        at org.springframework.integration.util.ErrorHandlingTaskExecutor.execute(ErrorHandlingTaskExecutor.java:55)
        at org.springframework.integration.endpoint.AbstractPollingEndpoint$Poller.run(AbstractPollingEndpoint.java:385)
        at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
        at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:93)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:748)
Caused by: org.springframework.integration.MessageDispatchingException: Dispatcher has no subscribers, failedMessage=GenericMessage [payload=/servicedata/tmp/1544088550162_57280.xml, headers={file_originalFile=/servicedata/tmp/1544088550162_57280.xml, id=d04473ff-d1bd-173e-d801-b7b9fd31596c, file_name=1544088550162_57280.xml, file_relativePath=1544088550162_57280.xml, timestamp=1544108156591}]
        at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:138)
        at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:105)
        at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:73)
        ... 33 more

还有其他方法可以实现此要求吗?那是:停止应用程序应该让当前文件完成,不接受其他文件读取,并且在没有任何奇怪异常的情况下仍然关闭吗?

我认为这是某种时序问题,因为集成流程和对ContextClosedEvent的响应正在不同的线程上运行。轮询程序尚未完全销毁,但出站通道的转换用户已被销毁。

我还试图通过控制总线停止轮询器,但是结果是相同的。

预先感谢:)

1 个答案:

答案 0 :(得分:0)

问题不是关于轮询器,而是InboundChannelAdapter。很高兴看到您的IntegrationFlow定义。实际上,它必须在应用程序上下文关闭期间停止。而且您不需要从身边做任何其他事情。

要点是,所有活动的Spring Integration组件均实现SmartLifecycle,并且在适当的应用程序上下文关闭阶段可以对它们进行stop()的修饰。