简体   繁体   中英

Spring JMS Template - Concurrent Calls

Can I make concurrent calls using 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.

The other option that I am looking at is to use ExecutorService .

Is there any advantage using one over the other?

JMSTemplate is thread-safe, so making parallel calls to it is not a problem.

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. 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.

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.

As for receiving replies from your external service, you need to use JMS Request/Reply pattern (you can find examples in this article). Happily, as you're using Spring, you could make Spring Integration do lots of work for you. You need to configure outbound-gateway to send messages and inbound-gateway to receive responses. Since version 2.2 you can also use reply-listener to simplify things on your client side. 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. Best option is usng @Asynch at method level

This example contains RestTemplate , But in your case create JmsTemplate beans.

Prerequisites:- Please create proper JMS Beans to connect to the queue. 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/

Step1: Create a Service Class where JMS Queue

@EnableAsynch
public class 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

   @Service
   public Class JmsService{

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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