Java基础、中级、高级、架构面试资料

@SendTo 报 Multiple destinations cannot be specified

JAVA herman 1940浏览
公告:“业余草”微信公众号提供免费CSDN下载服务(只下Java资源),关注业余草微信公众号,添加作者微信:xttblog2,发送下载链接帮助你免费下载!
本博客日IP超过2000,PV 3000 左右,急需赞助商。
极客时间所有课程通过我的二维码购买后返现24元微信红包,请加博主新的微信号:xttblog2,之前的微信号好友位已满,备注:返现
受密码保护的文章请关注“业余草”公众号,回复关键字“0”获得密码
所有面试题(java、前端、数据库、springboot等)一网打尽,请关注文末小程序
视频教程免费领
腾讯云】1核2G5M轻量应用服务器50元首年,高性价比,助您轻松上云

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

废话不多说,今天我们来说一个关于 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。

业余草公众号

最后,欢迎关注我的个人微信公众号:业余草(yyucao)!可加作者微信号:xttblog2。备注:“1”,添加博主微信拉你进微信群。备注错误不会同意好友申请。再次感谢您的关注!后续有精彩内容会第一时间发给您!原创文章投稿请发送至532009913@qq.com邮箱。商务合作也可添加作者微信进行联系!

本文原文出处:业余草: » @SendTo 报 Multiple destinations cannot be specified