Spring DSL:将错误消息发送到 JMS 队列。收到错误“单向‘MessageHandler’并且不适合配置‘outputChannel’”

Sta*_*Dev 2 java spring spring-integration

我一定是在流程定义中遗漏了一些非常基本的东西。出现此错误

is a one-way 'MessageHandler' and it isn't appropriate to configure 'outputChannel'. This is the end of the integration flow.
Run Code Online (Sandbox Code Playgroud)

我的理论是,由于适配器是单向组件,因此在流程的处理步骤中不会生成输出。这就是导致运行时错误的原因。但是,不知道如何定义这个简单的流程。

代码:

@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;

@Bean
public Queue errorQueue() {
    return new ActiveMQQueue(fatalQueue);
}
@Bean
public DirectChannel errorChannel() {
    return new DirectChannel();
}
@Bean
public IntegrationFlow handleErrors() {
    return IntegrationFlows
            .from(errorChannel())
            .handle(x -> System.out.println("error handling invoked.x="+x))
            .handle(Jms.outboundAdapter(jmsMessagingTemplate.getConnectionFactory()).destination(fatalQueue))
            .get();
}
Run Code Online (Sandbox Code Playgroud)

并且,堆栈跟踪显示:

Caused by: org.springframework.beans.factory.BeanCreationException: The 'currentComponent' (MessageReceiver$$Lambda$1/1495414981@76c52298) is a one-way 'MessageHandler' and it isn't appropriate to configure 'outputChannel'. This is the end of the integration flow.
  at org.springframework.integration.dsl.IntegrationFlowDefinition.registerOutputChannelIfCan(IntegrationFlowDefinition.java:2630) ~[spring-integration-java-dsl-1.1.0.RELEASE.jar:na]
  at org.springframework.integration.dsl.IntegrationFlowDefinition.register(IntegrationFlowDefinition.java:2554) ~[spring-integration-java-dsl-1.1.0.RELEASE.jar:na]
  at org.springframework.integration.dsl.IntegrationFlowDefinition.handle(IntegrationFlowDefinition.java:1136) ~[spring-integration-java-dsl-1.1.0.RELEASE.jar:na]
  at org.springframework.integration.dsl.IntegrationFlowDefinition.handle(IntegrationFlowDefinition.java:1116) ~[spring-integration-java-dsl-1.1.0.RELEASE.jar:na]
  at org.springframework.integration.dsl.IntegrationFlowDefinition.handle(IntegrationFlowDefinition.java:863) ~[spring-integration-java-dsl-1.1.0.RELEASE.jar:na]
Run Code Online (Sandbox Code Playgroud)

Art*_*lan 5

你的问题在这里:

.handle(x -> System.out.println("error handling invoked.x="+x))
Run Code Online (Sandbox Code Playgroud)

StackTrace 正是讨论了这一点。

这并不奇怪。你的 Lambda 是这样的:

.handle(new MessageHandler() {

        public void handleMessage(Message<?> message) throws MessagingException {
               System.out.println("error handling invoked.x="+x);
        }
})
Run Code Online (Sandbox Code Playgroud)

注意void返回类型。所以,下游没有什么可传递的。

要修复它,你应该做类似的事情:

.handle((p, h) -> {
        System.out.println("error handling invoked.x=" + new MutableMessage(p, h));
        return p;
 })
Run Code Online (Sandbox Code Playgroud)

哪里是一个GenericHandler实现。

.handle(Jms.outboundAdapter())这里很好。这确实是流程的结束。