0

我有一个接口Channels.javaPollableChannel与Spring集成

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 
@BridgeTo(Channels.OUTPUT) 
public PollableChannel polled() { 
    return new QueueChannel(); 
} 

@Configuration类希望能够利用这个渠道来接收消息?

回答

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条消息并将它们发送到输出通道。

本示例还添加了一个兔子侦听器来消费发送给活页夹的消息。

+0

感谢您的回答,我还可以为桥接创建一个Bean并为@BridgeFrom使用Channels.OUTPUT? – usr1234

+0

是的;它只是一个bean名称。 –

+0

请参阅编辑 – usr1234