Spring Cloud Stream @SendTo注释不起作用

编程入门 行业动态 更新时间:2024-10-08 14:42:57
本文介绍了Spring Cloud Stream @SendTo注释不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在将Spring Cloud Stream与Spring Boot结合使用.我的申请非常简单:

ExampleService.class:

@EnableBinding(Processor1.class) @Service public class ExampleService { @StreamListener(Processor1.INPUT) @SendTo(Processor1.OUTPUT) public String dequeue(String message){ System.out.println("New message: " + message); return message; } @SendTo(Processor1.OUTPUT) public String queue(String message){ return message; } }

Procesor1.class:

public interface Processor1 { String INPUT = "input1"; String OUTPUT = "output1"; @Input(Processor1.INPUT) SubscribableChannel input1(); @Output(Processor1.OUTPUT) MessageChannel output1(); }

application.properties:

spring.cloud.stream.bindings.input1.destination=test_input spring.cloud.stream.bindings.input1.group=test_group spring.cloud.stream.bindings.input1.binder=binder1 spring.cloud.stream.bindings.output1.destination=test_output spring.cloud.stream.bindings.output1.binder=binder1 spring.cloud.stream.binders.binder1.type=rabbit

spring.cloud.stream.binders.binder1.environment.spring.rabbitmq.host = localhost

场景:

1)当我在"test_input.test_group"队列中推送消息时,消息被正确打印并正确发送到"test_output"交换.所以ExampleService :: dequeue效果很好.

2)当我调用ExampleService :: queue方法(从类外部,在测试中)时,消息永远不会发送到'test_output'交换.

我正在使用Spring Boot 2.0.6.RELEASE和Spring Cloud Stream 2.0.2.RELEASE.

有人知道为什么情况2)无法正常工作吗?预先感谢.

解决方案

是什么让您相信@SendTo本身受支持? @SendTo是许多项目使用的辅助注释,而不仅仅是Spring Cloud Stream;据我所知,没有什么可以自己寻找的.

改为尝试Spring Integration的@Publisher注释(使用@EnablePublisher).

编辑

要强制使用CGLIB而不是JDK代理进行代理,您可以执行此操作...

@Bean public static BeanFactoryPostProcessor bfpp() { return bf -> { bf.getBean(IntegrationContextUtils.PUBLISHER_ANNOTATION_POSTPROCESSOR_NAME, PublisherAnnotationBeanPostProcessor.class).setProxyTargetClass(true); }; }

I'm using Spring Cloud Stream with Spring Boot. My application is very simple:

ExampleService.class:

@EnableBinding(Processor1.class) @Service public class ExampleService { @StreamListener(Processor1.INPUT) @SendTo(Processor1.OUTPUT) public String dequeue(String message){ System.out.println("New message: " + message); return message; } @SendTo(Processor1.OUTPUT) public String queue(String message){ return message; } }

Procesor1.class:

public interface Processor1 { String INPUT = "input1"; String OUTPUT = "output1"; @Input(Processor1.INPUT) SubscribableChannel input1(); @Output(Processor1.OUTPUT) MessageChannel output1(); }

application.properties:

spring.cloud.stream.bindings.input1.destination=test_input spring.cloud.stream.bindings.input1.group=test_group spring.cloud.stream.bindings.input1.binder=binder1 spring.cloud.stream.bindings.output1.destination=test_output spring.cloud.stream.bindings.output1.binder=binder1 spring.cloud.stream.binders.binder1.type=rabbit

spring.cloud.stream.binders.binder1.environment.spring.rabbitmq.host=localhost

Scenarios:

1) When I push a message in 'test_input.test_group' queue, message is correctly printed and correctly sent to 'test_output' exchange. So ExampleService::dequeue works well.

2) When I invoke ExampleService::queue method (from outside the class, in a test), message is never sent to 'test_output' exchange.

I'm working with Spring Boot 2.0.6.RELEASE and Spring Cloud Stream 2.0.2.RELEASE.

Anybody knows why scenario 2) is not working? Thanks in advance.

解决方案

What leads you to believe that @SendTo on its own is supported? @SendTo is a secondary annotation used by many projects, not just Spring Cloud Stream; as far as I know, there is nothing that will look for it on its own.

Try Spring Integration's @Publisher annotation instead (with @EnablePublisher).

EDIT

To force proxying with CGLIB instead of a JDK proxy, you can do this...

@Bean public static BeanFactoryPostProcessor bfpp() { return bf -> { bf.getBean(IntegrationContextUtils.PUBLISHER_ANNOTATION_POSTPROCESSOR_NAME, PublisherAnnotationBeanPostProcessor.class).setProxyTargetClass(true); }; }

更多推荐

Spring Cloud Stream @SendTo注释不起作用

本文发布于:2023-11-25 11:51:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1629628.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:注释   不起作用   Cloud   Spring   SendTo

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!