春季整合:QueueChannel [英] Spring-Integration: QueueChannel

查看:84
本文介绍了春季整合:QueueChannel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简短摘要:

我想将消息发送到队列,并使多个线程处理该消息.该应用程序应仅将消息异步发送到网关,但在队列已满时应将其阻塞.我也想使传递到Queue的线程是多线程的.我的问题是,我的队列永远不会阻塞并接收更多的消息,而实际大小是

I want to send messages to a queue and make multiple threads process this messages. The application should just send the messages asynchronously to a Gateway but should get blocked when the queue is full. Also I wanted to make the delivery to the Queue multi-threaded. My Problem is that my Queue would never block and take way more messages then its actual size is

推荐答案

我不确定不阻止"是什么意思.这对我来说很好...

I am not sure what you mean by "doesn't block". This works fine for me...

@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个立即发送,然后由于阻塞等待队列空间而每秒发送一次.

The first 10 are sent immediately, then one per second due to blocking waiting for queue space.

如果您想阻止呼叫者,为什么感觉需要异步网关?

Why do you feel you need an async gateway, if you want to block the caller?

这篇关于春季整合:QueueChannel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆