PollableChannel与Spring集成

时间:2017-05-17 07:34:00

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

我有一个界面 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();
}

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

1 个答案:

答案 0 :(得分:0)

网桥必须是@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;当它满了,发送者阻塞;轮询器一次只删除2条消息并将它们发送到输出通道。

此示例还添加了一个兔子侦听器来使用发送到绑定器的消息。