【问题标题】:Multithread processing with RedisQueueInboundGateway使用 RedisQueueInboundGateway 进行多线程处理
【发布时间】:2019-04-23 01:16:32
【问题描述】:

我正在使用 Spring 与 Redis 的集成。生产者使用 RedisQueueOutboundGateway,而接收者使用 RedisQueueInboundGateway 定义流。

从文档中读到下面这句话

task-executor 必须配置多个线程进行处理

我需要并发执行,以加快请求的详细说明,但我可以看到即使我配置了一个自定义 ThreadPoolTask​​Executor 如下所示,总是有一个线程

public Executor getAsyncExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(5);
    executor.setMaxPoolSize(40);
    executor.setQueueCapacity(40);
    executor.setThreadNamePrefix("QueueAsyncExecutor-");
    executor.initialize();
    return executor;
}

而这个线程池的使用是

final RedisQueueInboundGateway rqig = new RedisQueueInboundGateway(finalDestination, jedisConnectionFactory);
rqig.setTaskExecutor(getAsyncExecutor());

最终结果是对请求的顺序处理,所有这些都使用同一个线程完成,正如我从日志中看到的那样。在这种情况下是否可以启用多线程处理?怎么样?

【问题讨论】:

    标签: spring multithreading redis spring-integration


    【解决方案1】:

    没错。 RedisQueueInboundGateway 目前是单线程的。只有一个ListenerTask

    private void restart() {
        this.taskExecutor.execute(new ListenerTask());
    }
    

    听起来我们需要在RedisQueueInboundGateway 中引入concurrency 选项!随意就此事提出 JIRA,欢迎贡献!

    您可以为同一个 Redis 队列实现多个 RedisQueueInboundGateway 实例的人工并发。这样他们每个人都会开始自己的ListenerTask

    【讨论】:

    • 感谢您的快速回复。使用RedisInboundChannelAdapter(可以是多线程)模拟并发是否正确,最后使用自定义流向redis添加“.reply”消息,以便使用RedisQueueOutboundGateway的调用者可以读取它?
    • 是的,您也可以使用它。只有RedisQueueInboundGateway 对弹出请求的 UUID 有一些硬逻辑的问题。
    • 利用您的好意,您能告诉我为什么我在文档中找到了 Redis Queue Inbound Channel Adapter 并且它可以作为 xml 实体 (int-redis:queue-inbound-channel-adapter) 但我没有找到等效的 Java DSL
    猜你喜欢
    • 1970-01-01
    • 2018-12-17
    • 1970-01-01
    • 2019-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-04
    • 1970-01-01
    相关资源
    最近更新 更多