简体   繁体   English

多线程 Spring 集成 DSL

[英]Multithreading Spring integration DSL

I want to create a simple IntegrationFlow with Spring integration, and I am having difficulties.我想用 Spring 集成创建一个简单的 IntegrationFlow,但遇到了困难。

I want to create an integration flow that takes messages from multiple queues in Rabbit Mq and posts the messages to different Rest endpoints.我想创建一个集成流,从 Rabbit Mq 中的多个队列获取消息并将消息发布到不同的 Rest 端点。

i want to know if i can parallelize this.我想知道我是否可以并行化这个。

i have two scenarios that i want to check the feasibility :我有两种情况要检查可行性:

  • the first one i want to create a thread for every RabbitMq Queue that would listen and execute the flow after receiving a message :第一个我想为每个 RabbitMq 队列创建一个线程,它会在收到消息后监听并执行流:

Scenario 1场景一

  • the second scenario : in this scenario i want to create a dynamic number of threads for every queue , the number of threads goes up or down depending on the number of messages.第二种情况:在这种情况下,我想为每个队列创建动态数量的线程,线程数量根据消息数量增加或减少。

Scenario 2场景二

 HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        RestTemplate restTemplate = new RestTemplate();
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
        container.setQueueNames(BOUTIQUE_QUEUE_NAME);
        container.setAcknowledgeMode(AcknowledgeMode.AUTO);
        return IntegrationFlows.from(Amqp.inboundAdapter(container)) /* Get Message from RabbitMQ */
                .handle(msg ->
                {
                    String msgString = new String((byte[]) msg.getPayload(), StandardCharsets.UTF_8);
                    HttpEntity<String> requestBody = new HttpEntity<String>(msgString, headers);
                    restTemplate.postForObject(ENDPOINT_LOCAL_URL, requestBody, String.class);
                    System.out.println(msgString);
                   
                })
                .get();
    }

For the first scenario, simply configure a inbound adapter for each and set the output channel to a common channel for the downstream flow.对于第一个场景,只需为每个场景配置一个入站适配器,并将输出通道设置为下游流的公共通道。

For the second scenario, simply set the concurrentConsumers and maxConcurrentConsumers on the listener container and it will scale up/down the threads as needed.对于第二种情况,只需在侦听器容器上设置concurrentConsumersmaxConcurrentConsumers ,它将根据需要扩展/缩小线程。

See the Spring AMQP Documentation .请参阅Spring AMQP 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM