Lambda和匿名转换器的行为不同

时间:2018-11-19 18:38:26

标签: spring-integration

当使用lambda表达式定义接受类型为Message的源的转换器时,将引发ClassCastException。但是,如果使用匿名内部类定义语义相同的转换器,则不会发生错误。

@Bean
public IntegrationFlow lambda(MessageChannel outputChannel) {
    return f -> f
            .enrichHeaders(h -> h.header("add", 1))
            .<Message<Integer>, Integer>transform(m -> m.getPayload() + m.getHeaders().get("add", Integer.class))
            .channel(outputChannel);
}

@Bean
public IntegrationFlow anonymous(MessageChannel outputChannel) {
    return f -> f
            .enrichHeaders(h -> h.header("add", 1))
            .transform(new GenericTransformer<Message<Integer>, Integer>() {
                @Override
                public Integer transform(Message<Integer> source) {
                    return source.getPayload() + source.getHeaders().get("add", Integer.class);
                }
            })
            .channel(outputChannel);
}

完整的堆栈跟踪为:

org.springframework.integration.transformer.MessageTransformationException: Failed to transform Message; nested exception is org.springframework.messaging.MessageHandlingException: nested exception is java.lang.ClassCastException: java.lang.Integer cannot be cast to org.springframework.messaging.Message, failedMessage=GenericMessage [payload=0, headers={add=1, id=ed1e39d9-a91b-0bdf-5083-48218ba4e26f, timestamp=1542652005152}], failedMessage=GenericMessage [payload=0, headers={add=1, id=ed1e39d9-a91b-0bdf-5083-48218ba4e26f, timestamp=1542652005152}]
at org.springframework.integration.transformer.MessageTransformingHandler.handleRequestMessage(MessageTransformingHandler.java:114)
at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:123)
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:169)
at org.springframework.integration.dispatcher.AbstractDispatcher.tryOptimizedDispatch(AbstractDispatcher.java:115)
at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:133)
at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:106)
at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:73)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:459)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:400)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:187)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:166)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:47)
at org.springframework.messaging.core.AbstractMessageSendingTemplate.send(AbstractMessageSendingTemplate.java:109)
at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutput(AbstractMessageProducingHandler.java:415)
at org.springframework.integration.handler.AbstractMessageProducingHandler.produceOutput(AbstractMessageProducingHandler.java:334)
at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutputs(AbstractMessageProducingHandler.java:225)
at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:129)
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:169)
at org.springframework.integration.dispatcher.AbstractDispatcher.tryOptimizedDispatch(AbstractDispatcher.java:115)
at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:133)
at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:106)
at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:73)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:459)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:400)
at org.example.LambdaVsAnonymousTest.lambda(LambdaVsAnonymousTest.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
Caused by: org.springframework.messaging.MessageHandlingException: nested exception is java.lang.ClassCastException: java.lang.Integer cannot be cast to org.springframework.messaging.Message, failedMessage=GenericMessage [payload=0, headers={add=1, id=ed1e39d9-a91b-0bdf-5083-48218ba4e26f, timestamp=1542652005152}]
at org.springframework.integration.handler.LambdaMessageProcessor.processMessage(LambdaMessageProcessor.java:125)
at org.springframework.integration.transformer.AbstractMessageProcessingTransformer.transform(AbstractMessageProcessingTransformer.java:113)
at org.springframework.integration.transformer.MessageTransformingHandler.handleRequestMessage(MessageTransformingHandler.java:108)
... 55 more
Caused by: java.lang.ClassCastException: java.lang.Integer cannot be cast to org.springframework.messaging.Message
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.integration.handler.LambdaMessageProcessor.processMessage(LambdaMessageProcessor.java:122)
... 57 more

我想知道哪个是期望的行为;变压器应该能够接受Message还是应该因有效载荷类型而失败?但是我认为两种样式的行为应该相同。

可以找到一个示例项目here。请检查org.example.LambdaVsAnonymousorg.example.LambdaVsAnonymousTest

2 个答案:

答案 0 :(得分:3)

我相信您必须使用transform(..)的类型化变体。例如.transform(Message.class, m -> m.getPayload() + m.getHeaders().get("add", Integer.class))。 就是说,我认为我们应该改进javadoc,因为它谈论<P> the payload type - 'transform from',而它只是谈论应该接受的输入转换器的类型。

答案 1 :(得分:1)

这是Java Lambda的局限性。

Method.getParameterTypes() 

在Lambda上调用时仅返回Object

此处的类型信息...

.<Message<Integer>, Integer>transform(m -> m.getPayload() + m.getHeaders().get("add", Integer.class))

...只是语法糖,使您可以在lambda中引用适当的类型(类似于在lambda中进行显式转换)。它没有烤入lambda。

无法推断出lambda期望Message<?>

对于匿名内部类,由于该方法具有具体类型,因此保留了类型信息。