首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring集成: QueueChannel

Spring集成: QueueChannel
EN

Stack Overflow用户
提问于 2017-10-27 11:05:34
回答 1查看 1.7K关注 0票数 2

简短摘要:

我希望将消息发送到队列,并使多个线程处理这些消息。应用程序应该只是异步地将消息发送到Gateway,但在队列已满时应该会被阻塞。另外,我还想将消息传递到多线程队列。我的问题是,我的队列永远不会阻塞并接收比实际大小更大的消息。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-27 16:33:49

我不知道你所说的“不妨碍”是什么意思。对我来说很好..。

代码语言:javascript
复制
@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立即发送,然后每秒一个。

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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46973604

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档