ActiveMq – How to work with Spring JMS ActiveMq Topic (Publisher-Subcribers pattern) using SpringBoot

The previous tutorials, We had learned how to create a Spring JMS applications for working with ActiveMq Queue. In the tutorial, JavaSampleApproach will guide you how to create Spring JMS applications for working with ActiveMq Topic.

Related articles:
How to use Spring JMS with ActiveMQ – JMS Consumer and JMS Producer | Spring Boot
Spring Jms ActiveMq – How to send Java object messages to ActiveMQ server (specially with Bi-Directional relationship Java objects)
ActiveMq – Explicitly configure Spring ActiveMq ConnectionFactory with SpringBoot
How to resolve Json Infinite Recursion problem when working with Jackson

I. Spring Jms ActiveMq Topic

ActiveMq provides the Publish-Subscribe pattern (pub-sub) for building Jms message distributed systems.
How it work? -> When you publish a messages, all active subscribers will receive a copy of the message.

spring activeMq - publiser subcriber

With SpringBoot application, we need to enable pubSubDomain (.setPubSubDomain(true)) for 2 beans {JmsTemplate, JmsListenerContainerFactory}:

And set in file.

II. Practice

In the tutorial, we use SpringBoot to create 2 applications: PublisherSubcriber.

– 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

spring jms activeMq topic- project structure

Step to do:
– Create SpringBoot projects
– Create Java message models
– Configure ConnectionFactory
– Create Jms Publisher/Subcriber
– Implement Client for Publisher
– Run and check results

1. Create SpringBoot projects

Using SpringToolSuite to create 2 SpringBoot projects: one for Publisher, one for Subcriber. Then add dependencies for both of them:

2. Create Java message models

Create 2 Java message models: Company & Product with one-to-many relationship:

– Company

– Product

Note: We use @JsonIdentityInfo annotation to handle the Infinite Recursion problem for serializing Bi-Directional relationship objects with Jackson.

3. Configure ConnectionFactory

We create ConnectionFactoryConfig for both Publisher and Subcriber.

We use jacksonJmsMessageConverter bean to serialize Java object messages.
>>> See more at: How to send Java object messages to ActiveMQ server.

With Publisher application, we configure additional jmsTemplate bean in ConnectionFactoryConfig file as below code:

With Subcriber application, we configure additional jsaFactory bean in ConnectionFactoryConfig file as below code:

4. Create Jms Publisher/Subcriber

With Publisher application, create a JmsPublisher component as below:

With Subcriber application, create a JmsPublisher component as below:

5. Implement Client for Publisher

In main class SpringActiveMqTopicProducerApplication, we use CommandLineRunner interface to implement code:
– Initial 2 Company object messages {apple, samsung}
– Sending them to ActiveMQ Topic server.

6. Run and check results

– Start ActiveMQ server with commandline: C:\apache-activemq-5.13.0>.\bin\activemq start.
– Build and Run the SpringBoot applications by commandlines: {mvn clean install, mvn spring-boot:run} with the following order:

6.1 Enable one active subscriber

– Start an instance of SpringBoot Subcriber on ActiveMQ topic: jsa-topic
– Then start an instance of SpringBoot Publiser.

-> We receive 2 Company messages {apple, samsung}. See the subscriber’s console logs:

6.2 Enable two active subcribers

Again, we start another instance of SpringBoot Subcriber on ActiveMQ topic: jsa-topic.
-> Now having 2 active subcribers on jsa-topic topic.

Start the SpringBoot Publiser application again for sending messages.

-> Results: we receive 2 new Company messages for each Subcribers.
So with the first Subcriber, it had recieved total 4 Company messages:

And with the second Subcriber (the newer), it had recieved 2 Company messages:

Spring Jms Applications (Publisher – Subcribers) are working fine with ActiveMq server! -> Let’s start now!

III. Sourcecode


Related Posts

Got Something To Say:

Your email address will not be published. Required fields are marked *