当前位置: 首页 > 知识库问答 >
问题:

Spring集成-如何处理来自LoggingHandler的异常

公冶渝
2023-03-14

我一直在研究Spring集成文件支持,在这里我需要将文件从输入目录移动到输出目录。成功地将其移动到输出目录后,应该将其归档,然后从输入目录中删除。我正在使用下面的配置来实现这一点。

请纠正我,如果我的配置可以更好的方式。

Spring集成配置:

    <int:channel id="inboundFileChannel"/>
<int:channel id="outboundFileChannel"/>
<int:channel id="archiveFileChannel"/>

 <int-file:inbound-channel-adapter channel="inboundFileChannel"
                              directory="file://${file.input.dir}" filename-pattern="*.txt" auto-startup="true" auto-create-directory="false">
    <int:poller fixed-rate="${file.poller.interval}"/>
</int-file:inbound-channel-adapter>

<int:service-activator input-channel="inboundFileChannel" output-channel="outboundFileChannel" ref="fileHandler"/>

<int-file:outbound-gateway request-channel="outboundFileChannel" reply-channel ="archiveFileChannel" directory="file://${file.output.dir}"
                           mode="REPLACE" delete-source-files="false" auto-create-directory="false" />

<int:service-activator input-channel="archiveFileChannel" ref="archiveFileHandler" method="copyToArchiveDirectory"/>

<int:logging-channel-adapter channel="inboundFileChannel" level="DEBUG"/>

<bean id="fileHandler" class="FileHandler"/>
<bean id="archiveFileHandler" class="ArchiveFileHandler"/>

我在服务激活器中有代码,如果归档目录不可用,它应该终止应用程序。因此,在运行应用程序时,在第一次移动文件后,我已经删除了归档目录。然后,我将新文件放置在input目录中,该文件被轮询并发送到一个通道,但服务激活器处理程序不会被提取,也不会检查存档目录是否存在。

我从日志中发现,第一次消息被发送到ServiceActivatorHandler并移动了文件,但第二次消息被发送到LoggingHandler。

2016-09-30 14:27:36,593 [task-scheduler-1] [file.FileReadingMessageSource] INFO  - Created message: [GenericMessage [payload=c:\tmp\csvfiles\input\test1.txt, headers={id=4f03f37d-7bb4-d630-72c0-360fd07d3b88, timestamp=1475263656593}]]
2016-09-30 14:27:36,593 [task-scheduler-1] [endpoint.SourcePollingChannelAdapter] DEBUG - Poll resulted in Message: GenericMessage [payload=c:\tmp\csvfiles\input\test1.txt, headers={id=4f03f37d-7bb4-d630-72c0-360fd07d3b88, timestamp=1475263656593}]
2016-09-30 14:27:36,593 [task-scheduler-1] [channel.DirectChannel] DEBUG - preSend on channel 'inboundFileChannel', message: GenericMessage [payload=c:\tmp\csvfiles\input\test1.txt, headers={id=4f03f37d-7bb4-d630-72c0-360fd07d3b88, timestamp=1475263656593}]
2016-09-30 14:27:36,624 [task-scheduler-1] [handler.ServiceActivatingHandler] DEBUG - ServiceActivator for [org.springframework.integration.handler.MethodInvokingMessageProcessor@3d1024ab] received message: GenericMessage [payload=c:\tmp\csvfiles\input\test1.txt, headers={id=4f03f37d-7bb4-d630-72c0-360fd07d3b88, timestamp=1475263656593}]
2016-09-30 14:27:36,640 [task-scheduler-1] [handlers.FileHandler] INFO  - File : c:\tmp\csvfiles\input\test1.txt is copied to : c:/tmp/csvfiles/output/
2016-09-30 14:27:36,640 [task-scheduler-1] [channel.DirectChannel] DEBUG - preSend on channel 'outboundFileChannel', message: GenericMessage [payload=c:\tmp\csvfiles\input\test1.txt, headers={id=f53fbb89-d2f2-caab-f40b-cbd825238950, timestamp=1475263656640}]
2016-09-30 14:27:36,640 [task-scheduler-1] [file.FileWritingMessageHandler] DEBUG - org.springframework.integration.file.FileWritingMessageHandler@310cd27 received message: GenericMessage [payload=c:\tmp\csvfiles\input\test1.txt, headers={id=f53fbb89-d2f2-caab-f40b-cbd825238950, timestamp=1475263656640}]
2016-09-30 14:27:36,671 [task-scheduler-1] [channel.DirectChannel] DEBUG - preSend on channel 'archiveFileChannel', message: GenericMessage [payload=c:\tmp\csvfiles\output\test1.txt, headers={file_originalFile=c:\tmp\csvfiles\input\test1.txt, id=ac2a9c1e-243d-5702-ef23-f715ebce18f8, timestamp=1475263656671}]
2016-09-30 14:27:36,671 [task-scheduler-1] [handler.ServiceActivatingHandler] DEBUG - ServiceActivator for [org.springframework.integration.handler.MethodInvokingMessageProcessor@5f6b71ba] received message: GenericMessage [payload=c:\tmp\csvfiles\output\test1.txt, headers={file_originalFile=c:\tmp\csvfiles\input\test1.txt, id=ac2a9c1e-243d-5702-ef23-f715ebce18f8, timestamp=1475263656671}]
2016-09-30 14:27:36,671 [task-scheduler-1] [handlers.FileHandler] INFO  - Service Activator Activated
2016-09-30 14:27:36,671 [task-scheduler-1] [handlers.FileHandler] INFO  - File name : test1.txt
2016-09-30 14:27:36,702 [task-scheduler-1] [utility.FileUtility] INFO  - Archive file name : test1_2016-09-30_14-27-36-702.txt
2016-09-30 14:27:36,702 [task-scheduler-1] [handlers.FileHandler] INFO  - c:\tmp\csvfiles\output
2016-09-30 14:27:36,702 [task-scheduler-1] [handlers.FileHandler] INFO  - c:\tmp\csvfiles\archive
2016-09-30 14:27:36,718 [task-scheduler-1] [handlers.FileHandler] INFO  - Successfully archived file : test1.txt
2016-09-30 14:27:36,718 [task-scheduler-1] [handlers.FileHandler] INFO  - Source FIle Name with Path : c:\tmp\csvfiles\input\test1.txt
2016-09-30 14:27:36,718 [task-scheduler-1] [handlers.FileHandler] INFO  - Successfully deleted file : test1.txtfrom directory: c:/tmp/csvfiles/input/
2016-09-30 14:27:36,718 [task-scheduler-1] [handler.ServiceActivatingHandler] DEBUG - handler 'ServiceActivator for [org.springframework.integration.handler.MethodInvokingMessageProcessor@5f6b71ba]' produced no reply for request Message: GenericMessage [payload=c:\tmp\csvfiles\output\test1.txt, headers={file_originalFile=c:\tmp\csvfiles\input\test1.txt, id=ac2a9c1e-243d-5702-ef23-f715ebce18f8, timestamp=1475263656671}]
2016-09-30 14:27:36,718 [task-scheduler-1] [channel.DirectChannel] DEBUG - postSend (sent=true) on channel 'archiveFileChannel', message: GenericMessage [payload=c:\tmp\csvfiles\output\test1.txt, headers={file_originalFile=c:\tmp\csvfiles\input\test1.txt, id=ac2a9c1e-243d-5702-ef23-f715ebce18f8, timestamp=1475263656671}]
2016-09-30 14:27:36,718 [task-scheduler-1] [channel.DirectChannel] DEBUG - postSend (sent=true) on channel 'outboundFileChannel', message: GenericMessage [payload=c:\tmp\csvfiles\input\test1.txt, headers={id=f53fbb89-d2f2-caab-f40b-cbd825238950, timestamp=1475263656640}]
2016-09-30 14:27:36,718 [task-scheduler-1] [channel.DirectChannel] DEBUG - postSend (sent=true) on channel 'inboundFileChannel', message: GenericMessage [payload=c:\tmp\csvfiles\input\test1.txt, headers={id=4f03f37d-7bb4-d630-72c0-360fd07d3b88, timestamp=1475263656593}]
2016-09-30 14:27:36,718 [task-scheduler-1] [endpoint.SourcePollingChannelAdapter] DEBUG - Received no Message during the poll, returning 'false'
2016-09-30 14:27:37,250 [task-scheduler-2] [endpoint.SourcePollingChannelAdapter] DEBUG - Received no Message during the poll, returning 'false'
2016-09-30 14:27:38,248 [task-scheduler-1] [endpoint.SourcePollingChannelAdapter] DEBUG - Received no Message during the poll, returning 'false'
2016-09-30 14:27:39,250 [task-scheduler-3] [endpoint.SourcePollingChannelAdapter] DEBUG - Received no Message during the poll, returning 'false'
2016-09-30 14:27:40,249 [task-scheduler-3] [endpoint.SourcePollingChannelAdapter] DEBUG - Received no Message during the poll, returning 'false'
2016-09-30 14:27:41,247 [task-scheduler-4] [endpoint.SourcePollingChannelAdapter] DEBUG - Received no Message during the poll, returning 'false'
2016-09-30 14:27:42,246 [task-scheduler-1] [endpoint.SourcePollingChannelAdapter] DEBUG - Received no Message during the poll, returning 'false'
2016-09-30 14:27:43,244 [task-scheduler-5] [endpoint.SourcePollingChannelAdapter] DEBUG - Received no Message during the poll, returning 'false'
2016-09-30 14:27:44,242 [task-scheduler-2] [endpoint.SourcePollingChannelAdapter] DEBUG - Received no Message during the poll, returning 'false'
2016-09-30 14:27:45,241 [task-scheduler-6] [endpoint.SourcePollingChannelAdapter] DEBUG - Received no Message during the poll, returning 'false'
2016-09-30 14:27:46,239 [task-scheduler-6] [endpoint.SourcePollingChannelAdapter] DEBUG - Received no Message during the poll, returning 'false'
2016-09-30 14:27:47,238 [task-scheduler-7] [endpoint.SourcePollingChannelAdapter] DEBUG - Received no Message during the poll, returning 'false'
2016-09-30 14:27:48,252 [task-scheduler-7] [endpoint.SourcePollingChannelAdapter] DEBUG - Received no Message during the poll, returning 'false'
2016-09-30 14:27:49,239 [task-scheduler-8] [endpoint.SourcePollingChannelAdapter] DEBUG - Received no Message during the poll, returning 'false'
2016-09-30 14:27:50,241 [task-scheduler-8] [endpoint.SourcePollingChannelAdapter] DEBUG - Received no Message during the poll, returning 'false'
2016-09-30 14:27:51,239 [task-scheduler-8] [endpoint.SourcePollingChannelAdapter] DEBUG - Received no Message during the poll, returning 'false'
2016-09-30 14:27:52,209 [task-scheduler-5] [endpoint.SourcePollingChannelAdapter] DEBUG - Received no Message during the poll, returning 'false'
2016-09-30 14:27:53,278 [task-scheduler-5] [endpoint.SourcePollingChannelAdapter] DEBUG - Received no Message during the poll, returning 'false'
2016-09-30 14:27:54,247 [task-scheduler-2] [endpoint.SourcePollingChannelAdapter] DEBUG - Received no Message during the poll, returning 'false'
2016-09-30 14:27:55,245 [task-scheduler-2] [endpoint.SourcePollingChannelAdapter] DEBUG - Received no Message during the poll, returning 'false'
2016-09-30 14:27:56,252 [task-scheduler-6] [endpoint.SourcePollingChannelAdapter] DEBUG - Received no Message during the poll, returning 'false'
2016-09-30 14:27:57,238 [task-scheduler-6] [endpoint.SourcePollingChannelAdapter] DEBUG - Received no Message during the poll, returning 'false'
2016-09-30 14:27:58,253 [task-scheduler-7] [endpoint.SourcePollingChannelAdapter] DEBUG - Received no Message during the poll, returning 'false'
2016-09-30 14:27:59,238 [task-scheduler-7] [endpoint.SourcePollingChannelAdapter] DEBUG - Received no Message during the poll, returning 'false'
2016-09-30 14:28:00,252 [task-scheduler-7] [endpoint.SourcePollingChannelAdapter] DEBUG - Received no Message during the poll, returning 'false'
2016-09-30 14:28:01,249 [task-scheduler-7] [endpoint.SourcePollingChannelAdapter] DEBUG - Received no Message during the poll, returning 'false'
2016-09-30 14:28:02,249 [task-scheduler-10] [endpoint.SourcePollingChannelAdapter] DEBUG - Received no Message during the poll, returning 'false'
2016-09-30 14:28:03,247 [task-scheduler-5] [endpoint.SourcePollingChannelAdapter] DEBUG - Received no Message during the poll, returning 'false'
2016-09-30 14:28:04,249 [task-scheduler-3] [endpoint.SourcePollingChannelAdapter] DEBUG - Received no Message during the poll, returning 'false'
2016-09-30 14:28:05,248 [task-scheduler-3] [file.FileReadingMessageSource] DEBUG - Added to queue: [c:\tmp\csvfiles\input\test2.txt]
2016-09-30 14:28:05,248 [task-scheduler-3] [file.FileReadingMessageSource] INFO  - Created message: [GenericMessage [payload=c:\tmp\csvfiles\input\test2.txt, headers={id=be245baf-52b4-8196-714b-8003b55d347b, timestamp=1475263685248}]]
2016-09-30 14:28:05,248 [task-scheduler-3] [endpoint.SourcePollingChannelAdapter] DEBUG - Poll resulted in Message: GenericMessage [payload=c:\tmp\csvfiles\input\test2.txt, headers={id=be245baf-52b4-8196-714b-8003b55d347b, timestamp=1475263685248}]
2016-09-30 14:28:05,248 [task-scheduler-3] [channel.DirectChannel] DEBUG - preSend on channel 'inboundFileChannel', message: GenericMessage [payload=c:\tmp\csvfiles\input\test2.txt, headers={id=be245baf-52b4-8196-714b-8003b55d347b, timestamp=1475263685248}]
2016-09-30 14:28:05,248 [task-scheduler-3] [handler.LoggingHandler] DEBUG - org.springframework.integration.handler.LoggingHandler#0 received message: GenericMessage [payload=c:\tmp\csvfiles\input\test2.txt, headers={id=be245baf-52b4-8196-714b-8003b55d347b, timestamp=1475263685248}]
2016-09-30 14:28:05,263 [task-scheduler-3] [handler.LoggingHandler] DEBUG - c:\tmp\csvfiles\input\test2.txt
2016-09-30 14:28:05,279 [task-scheduler-3] [channel.DirectChannel] DEBUG - postSend (sent=true) on channel 'inboundFileChannel', message: GenericMessage [payload=c:\tmp\csvfiles\input\test2.txt, headers={id=be245baf-52b4-8196-714b-8003b55d347b, timestamp=1475263685248}]
2016-09-30 14:28:06,246 [task-scheduler-3] [endpoint.SourcePollingChannelAdapter] DEBUG - Received no Message during the poll, returning 'false'
2016-09-30 14:28:07,248 [task-scheduler-6] [endpoint.SourcePollingChannelAdapter] DEBUG - Received no Message during the poll, returning 'false'
2016-09-30 14:28:08,232 [task-scheduler-1] [endpoint.SourcePollingChannelAdapter] DEBUG - Received no Message during the poll, returning 'false'
2016-09-30 14:28:09,261 [task-scheduler-9] [endpoint.SourcePollingChannelAdapter] DEBUG - Received no Message during the poll, returning 'false'
2016-09-30 14:28:10,244 [task-scheduler-9] [endpoint.SourcePollingChannelAdapter] DEBUG - Received no Message during the poll, returning 'false'

由于我删除了归档目录(当应用程序运行时),消息被发送到LoggingHandler。有人可以建议如何处理这个异常,我可以写代码来终止应用程序。

如果我遗漏了什么,请指正。

共有1个答案

哈扬
2023-03-14
<int:channel id="inboundFileChannel"/>

<int:service-activator input-channel="inboundFileChannel" output-channel="outboundFileChannel" ref="fileHandler"/>

<int:logging-channel-adapter channel="inboundFileChannel" level="DEBUG"/>

当有两个使用者订阅同一个通道时(默认情况下DirectChannel),消息将交替循环分发到这两个使用者。

如果您希望该文件转到两个使用者,请将其更改为pub/sub通道。

<int:publish-subscribe-channel id="inboundFileChannel"/>
 类似资料:
  • 如果spring集成webflux流中发生异常,则异常本身(带有stacktrace)通过MessagePublishingErrorHandler作为有效负载发送回调用方,该处理器使用来自“errorChannel”头的错误通道,而不是默认错误通道。 如何设置类似于WebExceptionHandler的错误处理程序?我想生成一个Http状态代码,并可能生成一个DefaultErrorAttri

  • 在Spring integration中,我必须处理动态通道创建,但当我调试应用程序时,我看到不同通道之间的“阻塞”问题。 我知道是一个公共通道,在父上下文中共享,但如何为每个子上下文开发一个完整的独立场景?。公共网关是问题所在吗? 我在Spring integration flow async中看到了post错误处理,但对于每个子级,我都有一个完整的分离环境,我希望利用这些动态分离的优势。这可能

  • 在spring integration (Java DSL)中,如何定义一个完整流程的事务? 通过Spring集成,我们可以定义一个示例流程: 我需要一个跨度整个流程的交易。目前,当我使用“aMessage转换器”访问数据库时,事务将在处理完此消息转换器后关闭。但是我需要一个在处理“另一个消息转换器”时仍未提交的事务? 我希望只需添加一个“@Transactional”(或@Transaction

  • 我有以下方法: 在序列化期间调用。 枚举异常扩展了异常 我的控制器方法: 事务类: 在此setter中,引发枚举异常 在调试方法时,我看到它正在从响应EntityExceptionHandler类调用方法而不是我的方法 在抛出EnumException的情况下,我得到400状态代码和空响应正文。 为什么不执行异常处理程序方法? 以下是日志:

  • 问题内容: 我有一个库,其中为我们的客户提供了两种方法,sync和async。他们可以调用他们认为适合其目的的任何方法。 executeSynchronous()-等待直到得到结果,然后返回结果。 executeAsynchronous()-立即返回一个Future,如果需要,可以在完成其他操作之后进行处理。 他们将传递其中包含用户ID的DataKey对象。然后,我们将根据用户ID确定要调用的计算

  • 我有一个http inboundgateway,并定义了一个通往网关的错误通道。 异常消息StackTrace: 我在这里做错了什么?