【问题标题】:PollableChannel with Spring Integration与 Spring 集成的 PollableChannel
【发布时间】:2017-10-16 12:49:16
【问题描述】:

我有一个界面Channels.java

    final String OUTPUT = "output";

    final String INPUT = "input";


    @Output(OUTPUT)
    MessageChannel output();

    @BridgeFrom(OUTPUT)
    PollableChannel input();

我有另一个类,我在其中执行所有消息传递操作:

@Autowired
@Qualifier(Channels.OUTPUT)
private MessageChannel Output;

我可以向交易所发送消息。如何在这里使用我的 PollableChannel?我做错了什么?

编辑

以及如何访问 @Component 类中的 bean?

我现在有了@Configuration 类

@Bean
@BridgeTo(Channels.OUTPUT)
public PollableChannel polled() {
    return new QueueChannel();
}

希望能够使用此频道接收消息吗?

【问题讨论】:

    标签: java spring spring-integration spring-annotations spring-messaging


    【解决方案1】:

    桥必须是@Bean 而不是接口方法上的注释 - 请参阅the answer to your general question here

    编辑

    @SpringBootApplication
    @EnableBinding(Source.class)
    public class So44018382Application implements CommandLineRunner {
    
        final Logger logger = LoggerFactory.getLogger(getClass());
    
        public static void main(String[] args) throws Exception {
            ConfigurableApplicationContext context = SpringApplication.run(So44018382Application.class, args);
            Thread.sleep(60_000);
            context.close();
        }
    
        @RabbitListener(bindings =
                @QueueBinding(value = @Queue(value = "foo", autoDelete = "true"),
                                exchange = @Exchange(value = "output", type = "topic"), key = "#"))
        // bind a queue to the output exchange
        public void listen(String in) {
            this.logger.info("received " + in);
        }
    
        @BridgeTo(value = Source.OUTPUT,
                poller = @Poller(fixedDelay = "5000", maxMessagesPerPoll = "2"))
        @Bean
        public PollableChannel polled() {
            return new QueueChannel(5);
        }
    
        @Override
        public void run(String... args) throws Exception {
            for (int i = 0; i < 30; i++) {
                polled().send(new GenericMessage<>("foo" + i));
                this.logger.info("sent foo" + i);
            }
        }
    
    }
    

    这对我来说很好;队列的深度为 5;当它已满时,发件人阻塞; poller 一次只删除 2 条消息并将它们发送到输出通道。

    这个例子还添加了一个 rabbit 监听器来消费发送到 binder 的消息。

    【讨论】:

    • 感谢您的回答,我还能为桥创建一个 Bean 并为 @BridgeFrom 使用 Channels.OUTPUT 吗?
    • 是的;它只是一个 bean 名称。
    • 请参阅编辑
    • 自动接线@Autowired public PollableChannel polled
    • @ServiceActivator(inputChannel = "polled") 没有运行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-11
    • 1970-01-01
    • 2018-07-02
    • 1970-01-01
    • 2015-02-06
    • 2016-05-08
    • 2015-10-27
    相关资源
    最近更新 更多