【问题标题】:Spring-Integration: QueueChannel弹簧集成:QueueChannel
【发布时间】:2018-04-08 23:23:08
【问题描述】:

简短摘要:

我想将消息发送到队列并让多个线程处理这些消息。应用程序应该只将消息异步发送到网关,但当队列已满时应该被阻塞。我也想多线程传递到队列。我的问题是我的队列永远不会阻塞并接收比实际大小更多的消息

【问题讨论】:

    标签: java asynchronous spring-integration gateway


    【解决方案1】:

    我不确定您所说的“不阻止”是什么意思。这对我来说很好......

    @SpringBootApplication
    public class So46973604Application {
    
        private final Logger LOGGER = LoggerFactory.getLogger(So46973604Application.class);
    
        public static void main(String[] args) {
            SpringApplication.run(So46973604Application.class, args).close();
        }
    
        @Bean
        ApplicationRunner runner(Gate gate) {
            return args -> {
                for (int i = 0; i < 20; i++) {
                    gate.send("foo");
                    LOGGER.info("Sent " + i);
                }
            };
        }
    
        @Bean
        QueueChannel channel() {
            return new QueueChannel(10);
        }
    
        @ServiceActivator(inputChannel = "channel", poller = @Poller(fixedDelay = "0"))
        public void handle(String in) throws InterruptedException {
            Thread.sleep(1_000);
        }
    
        @MessagingGateway(defaultRequestChannel = "channel")
        public interface Gate {
    
            void send(String out);
    
        }
    
    }
    

    前 10 个立即发送,然后由于阻塞等待队列空间而每秒发送一个。

    如果你想阻止调用者,为什么你觉得需要一个异步网关?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-22
      • 1970-01-01
      相关资源
      最近更新 更多