简体   繁体   English

Spring JMS模板-并发调用

[英]Spring JMS Template - Concurrent Calls

Can I make concurrent calls using Spring JMSTemplate ? 我可以使用Spring JMSTemplate进行并发调用吗?

I want to make 4 external service calls in parallel and am exploring using Spring's JMSTemplate to perform these calls in parallel and wait for the execution to complete. 我想并行进行4个外部服务调用,并且正在探索使用Spring的JMSTemplate并行执行这些调用并等待执行完成。

The other option that I am looking at is to use ExecutorService . 我正在寻找的另一个选项是使用ExecutorService

Is there any advantage using one over the other? 使用一个相对于另一个有什么优势吗?

JMSTemplate is thread-safe, so making parallel calls to it is not a problem. JMSTemplate是线程安全的,因此并行调用它不是问题。

Messaging services are usually fast enough for most tasks and can receive your messages with minimal latency, so adding an ExecutorService doesn't seem as the first thing you usually need. 消息传递服务通常足够快以完成大多数任务,并且可以以最小的延迟接收消息,因此添加ExecutorService似乎并不是您通常需要的第一件事。 What you really need is to correctly configure your JMS connections pool and give it enough open connections (four in your case) so it could handle your parallel requests with no blocking. 您真正需要的是正确配置JMS连接池并为其提供足够的开放连接(在您的情况下为四个),以便它可以无阻塞地处理并行请求。

You only need ExecutorService in case you don't care about guaranteed delivery and your program needs extremely high speed that your messaging service cannot deliver, which is highly unlikely. 您只需要ExecutorService ,以防万一您不关心保证的传递,并且您的程序需要消息传递服务无法传递的极高速度,这是极不可能的。

As for receiving replies from your external service, you need to use JMS Request/Reply pattern (you can find examples in this article). 至于从外部服务接收答复,则需要使用JMS请求/答复模式 (您可以在本文中找到示例)。 Happily, as you're using Spring, you could make Spring Integration do lots of work for you. 令人高兴的是,当您使用Spring时,可以使Spring Integration为您做很多工作。 You need to configure outbound-gateway to send messages and inbound-gateway to receive responses. 您需要配置outbound-gateway发送消息,并配置inbound-gateway接收响应。 Since version 2.2 you can also use reply-listener to simplify things on your client side. 从2.2版开始,您还可以使用reply-listener器简化客户端的操作。 All these components are covered in the official documentation (with examples as well). 所有这些组件都包含在官方文档中 (还有示例)。

So need to talk to more than two JMS queues (send and or receive) parallel using asynchronous methods. 因此需要使用异步方法与两个以上的JMS队列(发送和/或接收)并行交谈。 Best option is usng @Asynch at method level 最好的选择是在方法级别使用@Asynch

This example contains RestTemplate , But in your case create JmsTemplate beans. 此示例包含RestTemplate,但在您的情况下,请创建JmsTemplate Bean。

Prerequisites:- Please create proper JMS Beans to connect to the queue. 先决条件:-请创建正确的JMS Bean以连接到队列。 Proper use of this will help to invoke two queues paralleley. 正确使用此方法将有助于调用两个并行队列。 Its works for sure because already I have implemented. 它的工作肯定是因为我已经实现了。 I just giving the skeleton due to Copyright issues. 由于版权问题,我只给出框架。

More details: Spring Boot + Spring Asynch https://spring.io/guides/gs/async-method/ 更多详细信息:Spring Boot + Spring Asynch https://spring.io/guides/gs/async-method/

Step1: Create a Service Class where JMS Queue 步骤1:在JMS队列中创建一个服务类

@EnableAsynch @EnableAsynch
public class JMSApplication { 公共类JMSApplication {

@Autowired
JmsService jmsService;

     public void invokeMe(){
      // Start the clock
        long start = System.currentTimeMillis();

        // Kick of multiple, asynchronous lookups
        Future<Object> queue1= jmsService.findqueue1();
        Future<Object> queue2= jmsService.findqueue2();

        // Wait until they are all done
        while (!(queue1.isDone() && queue2.isDone())) {
            Thread.sleep(10); //10-millisecond pause between each check
        }

        // Print results, including elapsed time
        System.out.println("Elapsed time: " + (System.currentTimeMillis() - start));
        System.out.println(queue1.get());
        System.out.println(queue2.get());

     }
}

Step2: Write the Service Method which will contain the business logic for Jms 步骤2:编写服务方法,其中将包含Jms的业务逻辑

   @Service
   public Class JmsService{

         @Asynch
         public Object findqueue1(){
         //Logic to invoke the JMS queue 
         }

         @Asynch
         public Object findqueue2(){
         //Logic to invoke the JMS queue 
         }
    }

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

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