@SendTo 报 Multiple destinations cannot be specified

最近博客园被闭站了,我这边的博客访问量明显感觉到了增多!

废话不多说,今天我们来说一个关于 Spring Cloud Stream 的问题。目前 Spring Cloud Stream 使用的用户还不是很多,所以抛出一些异常后,很多人素手无策。

详细异常如下:

java.lang.IllegalArgumentException: Multiple destinations cannot be specified at org.springframework.util.Assert.isTrue(Assert.java:118) at org.springframework.cloud.stream.binding.StreamListenerMethodUtils.getOutboundBindingTargetName(StreamListenerMethodUtils.java:146) at org.springframework.cloud.stream.binding.StreamListenerAnnotationBeanPostProcessor$DefaultStreamListenerSetupMethodOrchestrator.orchestrateStreamListenerSetupMethod(StreamListenerAnnotationBeanPostProcessor.java:349) at org.springframework.cloud.stream.binding.StreamListenerAnnotationBeanPostProcessor.doPostProcess(StreamListenerAnnotationBeanPostProcessor.java:195) at org.springframework.cloud.stream.binding.StreamListenerAnnotationBeanPostProcessor.lambda$postProcessAfterInitialization$0(StreamListenerAnnotationBeanPostProcessor.java:167) at java.lang.Iterable.forEach(Iterable.java:75) at org.springframework.cloud.stream.binding.StreamListenerAnnotationBeanPostProcessor.injectAndPostProcessDependencies(StreamListenerAnnotationBeanPostProcessor.java:285) at org.springframework.cloud.stream.binding.StreamListenerAnnotationBeanPostProcessor.afterSingletonsInstantiated(StreamListenerAnnotationBeanPostProcessor.java:105) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:863) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546) at org.springframework.boot.web.servle

这是错误使用 @SendTo 导致的。比如下面的用法:

@StreamListener(SampleBinding.INPUT1)
@SendTo({SampleBinding.XTTBLOG_OUTPUT, SampleBinding.XTTBLOG_OUTPUT})
public String handleM(String sampleMessage){
    log.info("xttblog Received message=" + sampleMessage.toString());
    sampleMessage = sampleMessage.toUpperCase();
    return sampleMessage;
}

@SendTo 注解虽然支持配置一个数组,但是对某些消息组件支持不是很友好,会抛出这个异常。可以采用硬编码或是其他动态转发方案。

举例一个正确用法:

@StreamListener(SampleBinding.INPUT1)
@SendTo(SampleBinding.XTTBLOG_OUTPUT)
public String handleM(String sampleMessage){
    log.info("xttblog Received message=" + sampleMessage.toString());
    sampleMessage = sampleMessage.toUpperCase();
    return sampleMessage;
}

另外需要注意的是,@StreamListener 和 @SendTo 组合使用时,不能配置 condition。

@SendTo 报 Multiple destinations cannot be specified

: » @SendTo 报 Multiple destinations cannot be specified

原创文章,作者:745907710,如若转载,请注明出处:https://blog.ytso.com/252271.html

(0)
上一篇 2022年5月4日
下一篇 2022年5月4日

相关推荐

发表回复

登录后才能评论