Spring Integration @ServiceActivator消息处理器无输出问题排查
为什么testOutputHandler()没有任何打印输出?
看起来alarmHandler()并未将消息推送到outputChannel指定的"testOutput"通道。
代码示例
@Bean @ServiceActivator(inputChannel = CHANNEL_ALARM, requiresReply = "true", outputChannel = "testOutput") public MessageHandler alarmHandler(SessionFactory<FTPFile> sessionFactory) { return message -> { System.out.println(CHANNEL_ALARM); System.out.println(message.getHeaders()); }; } @Bean @ServiceActivator(inputChannel = "testOutput" ) public MessageHandler testOutputHandler() { return message -> { System.out.println("test"); System.out.println(message.getHeaders()); }; }
控制台输出
2022-10-04 17:56:53.710 INFO 26092 --- [ main] o.s.b.a.b.JobLauncherApplicationRunner : Running default command line with: [] alarm {file_remoteHostPort=192.168.1.44:21, file_remoteFileInfo={"directory":false,"filename":"Alarm_Logs_12.9.2022_0.41.csv","link":false,"modified":1664782140000,"permissions":"----------","remoteDirectory":"for_it","size":88804}, file_remoteDirectory=for_it, channel=alarm, id=67d21ff6-e0ef-1992-2b32-ab759a8c1076, closeableResource=org.springframework.integration.file.remote.session.CachingSessionFactory$CachedSession@4b9ffb22, file_remoteFile=Alarm_Logs_12.9.2022_0.41.csv, timestamp=1664884618273}
问题原因与解决方案
核心问题是alarmHandler未返回任何消息,但你设置了requiresReply = "true"。
在Spring Integration中,@ServiceActivator标注的处理器若设置requiresReply = true,则必须返回非null的消息(或有效负载),否则不会将消息发送到outputChannel,甚至会抛出ReplyRequiredException。你的alarmHandler仅执行打印操作,没有返回值,因此没有消息被推送到testOutput通道,testOutputHandler自然不会触发。
解决方法:
- 需要传递消息到下一个通道:修改
alarmHandler,让它返回输入消息或新消息,示例:
return message -> { System.out.println(CHANNEL_ALARM); System.out.println(message.getHeaders()); return message; // 返回原消息,触发outputChannel的消息传递 };
- 不需要强制返回消息:将
requiresReply = "true"改为requiresReply = "false"(或直接移除该属性,默认值为false)。但注意,这种情况下若要传递消息到outputChannel,仍需处理器返回消息;若无需传递,仅执行当前逻辑,此修改即可避免因无返回值导致的异常。
另外可检查日志,确认是否存在ReplyRequiredException异常,这也能佐证上述问题。
内容的提问来源于stack exchange,提问作者Jekshenov Chingiz
相关产品推荐
相关产品推荐

