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