Java 带Spring集成的PollableChannel

Java 带Spring集成的PollableChannel,java,spring,spring-integration,spring-annotations,spring-messaging,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.

我有一个接口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?我做错了什么

编辑

@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);
        }
    }

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

我现在有了@Configuration类和

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

想使用此频道接收消息吗?

网桥必须是
@Bean
而不是接口方法上的注释-请参阅

编辑

@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);
        }
    }

}
@springboot应用程序
@EnableBinding(Source.class)
公共类SO44018382应用程序实现CommandLineRunner{
最终记录器=LoggerFactory.getLogger(getClass());
公共静态void main(字符串[]args)引发异常{
ConfigurableApplicationContext上下文=SpringApplication.run(So44018382Application.class,args);
睡眠(60_000);
context.close();
}
@RabbitListener(绑定=
@QueueBinding(value=@Queue(value=“foo”,autoDelete=“true”),
exchange=@exchange(value=“output”,type=“topic”),key=“#”)
//将队列绑定到输出交换
公共void侦听(字符串输入){
this.logger.info(“已接收”+in);
}
@BridgeTo(值=Source.OUTPUT,
轮询器=@poller(fixedDelay=“5000”,maxMessagesPerPoll=“2”))
@豆子
公共可轮询频道已轮询(){
返回新的队列通道(5);
}
@凌驾
公共无效运行(字符串…参数)引发异常{
对于(int i=0;i<30;i++){
polled().send(新的GenericMessage(“foo”+i));
this.logger.info(“发送的foo”+i);
}
}
}
这对我来说很好;队列深度为5;当它满时,发送器阻塞;轮询器一次只删除2条消息,并将它们发送到输出通道


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

桥接器必须是
@Bean
而不是接口方法上的注释-请参阅

编辑

@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);
        }
    }

}
@springboot应用程序
@EnableBinding(Source.class)
公共类SO44018382应用程序实现CommandLineRunner{
最终记录器=LoggerFactory.getLogger(getClass());
公共静态void main(字符串[]args)引发异常{
ConfigurableApplicationContext上下文=SpringApplication.run(So44018382Application.class,args);
睡眠(60_000);
context.close();
}
@RabbitListener(绑定=
@QueueBinding(value=@Queue(value=“foo”,autoDelete=“true”),
exchange=@exchange(value=“output”,type=“topic”),key=“#”)
//将队列绑定到输出交换
公共void侦听(字符串输入){
this.logger.info(“已接收”+in);
}
@BridgeTo(值=Source.OUTPUT,
轮询器=@poller(fixedDelay=“5000”,maxMessagesPerPoll=“2”))
@豆子
公共可轮询频道已轮询(){
返回新的队列通道(5);
}
@凌驾
公共无效运行(字符串…参数)引发异常{
对于(int i=0;i<30;i++){
polled().send(新的GenericMessage(“foo”+i));
this.logger.info(“发送的foo”+i);
}
}
}
这对我来说很好;队列深度为5;当它满时,发送器阻塞;轮询器一次只删除2条消息,并将它们发送到输出通道


此示例还添加了一个rabbit侦听器,以使用发送到绑定器的消息。

感谢您的回答,我仍然可以为桥接器创建一个Bean并使用@BridgeFrom的Channels.OUTPUT吗?是;这只是一个bean名称。请参阅EDITJust自动连接它
@Autowired public PollableChannel polled
@ServiceActivator(inputChannel=“polled”)未运行。感谢您的回答,我仍然可以为网桥创建bean并使用Channels.OUTPUT作为@BridgeFrom吗?是;这只是一个bean名称。请参阅EDITJust自动连线它
@Autowired public PollableChannel polled
@ServiceActivator(inputChannel=“polled”)不运行。