Spring Jms ActiveMQ – How to create a SpringBoot ActiveMQ Response Management application by @SendTo annotation
Tags: springboot, springjms, activemq, sendto
Published: 2021-05-06 12:30:49

https://grokonez.com/spring-framework/spring-jms/activemq-create-springboot-activemq-response-management-application-sendto-annotation

Spring Jms ActiveMQ – How to create a SpringBoot ActiveMQ Response Management application by @SendTo annotation

In the past <a href="https://grokonez.com/tag/activemq">posts</a>, we had learned how to consume/produce ActiveMq JMS messages. Today, <a href="https://grokonez.com">JavaSampleApproach</a> will show you way to create a <strong>SpringBoot ActiveMQ Response Management</strong> application by <code>@SendTo</code> annotation.

Related posts:
- <a href="https://grokonez.com/spring-framework/spring-boot/create-spring-rabbitmq-producerconsumer-application-springboot">Spring ActiveMQ JMS Consumer and JMS Producer</a>
- <a href="https://grokonez.com/spring-framework/spring-boot/spring-jms-activemq-send-java-object-messages-activemq-server-specially-bi-directional-relationship-java-objects">Send Java object messages to ActiveMQ server (specially with Bi-Directional relationship Java objects)</a>
- <a href="https://grokonez.com/spring-framework/spring-boot/activemq-work-spring-jms-activemq-topic-publisher-subcribers-pattern-using-springboot">Spring JMS ActiveMq Topic (Publisher-Subcribers pattern)</a>

<!--more-->
<iframe width="853" height="480" src="https://www.youtube.com/embed/SovlRTXowB8?rel=0" frameborder="0" allowfullscreen></iframe>
<h2>I. Technologies</h2>
– Java 8
– Maven 3.6.1
– Spring Tool Suite: Version 3.8.4.RELEASE
– Spring Boot: 1.5.4.RELEASE
– Apache ActiveMQ 5.14.0
<h2>II. ActiveMQ Response Management</h2>
With the Spring JMS improvements (from 4.1), we can used <code>@SendTo</code> annotation to define the default next destination with <code>@JmsListener</code>:

<img src="https://grokonez.com/wp-content/uploads/2017/07/SpringBoot-ActivMQ-Response-Management-application-sendto-aannotation-architecture.png" alt="SpringBoot ActiveMQ Response Management application - sendto aannotation - architecture" width="524" height="267" class="alignnone size-full wp-image-7024" />

<pre><code class="language-java">
@JmsListener(destination = "${jsa.activemq.queue.listen}", containerFactory="jsaFactory")
@SendTo("${jsa.activemq.queue.sendto}")
public Product processOrder(Product product) {
    // process a newProduct
    return newProduct;
}
</code></pre>

For additional headers, you could return a Message object instead:

<pre><code class="language-java">
@JmsListener(destination = "${jsa.activemq.queue.listen}", containerFactory="jsaFactory")
@SendTo("${jsa.activemq.queue.sendto}")
public Message<Product>  receive(Product product, @Header("company") String companyName){
	
	...
	
	Message<Product>  mesage = MessageBuilder
            .withPayload(product)
            .setHeader("type", product.getType())
            .build();
	
	return mesage;	
}</code></pre>

<div class="jsa-quote">
When having several <code>@JmsListener</code> methods, we can also place the <code>@SendTo</code> annotation at the class level to share a default reply destination.
</div>
<h2>III. Practice</h2>
In the tutorial, we create 2 SpringBoot projects {<strong>SpringActiveMqProducerConsumer</strong>, <strong>SpringActiveMqSendTo</strong>}:

<img src="https://grokonez.com/wp-content/uploads/2017/07/SpringBoot-ActivMQ-Response-Management-application-sendto-aannotation-project-structure.png" alt="SpringBoot ActiveMQ Response Management application - sendto aannotation - project structure" width="724" height="259" class="alignnone size-full wp-image-7025" />

MORE AT:

https://grokonez.com/spring-framework/spring-jms/activemq-create-springboot-activemq-response-management-application-sendto-annotation

Spring Jms ActiveMQ – How to create a SpringBoot ActiveMQ Response Management application by @SendTo annotation