ActiveMq – Explicitly configure Spring ActiveMq ConnectionFactory with SpringBoot

In the past tutorial, we had learned how to start a Spring JMS ActiveMQ application with auto-configured ConnectionFactory by SpringBoot.
But in the software development, there’re many cases that we need to override the ContainerFactory, so How to do it? JavaSampleApproach makes the tutorial to guide how to explicitly configure Spring ActiveMQ ConnectionFactory.

Related posts:
How to use Spring JMS with ActiveMQ – JMS Consumer and JMS Producer | Spring Boot
How to start Spring Kafka Application with Spring Boot
Spring Jms ActiveMq – How to send Java object messages to ActiveMQ server (specially with Bi-Directional relationship Java objects)
ActiveMq – How to work with Spring JMS ActiveMq Topic (Publisher-Subcribers pattern) using SpringBoot

I. Technologies

– Java 8
– Maven 3.6.1
– Spring Tool Suite: Version 3.8.4.RELEASE
– Spring Boot: 1.5.4.RELEASE
– Apache ActiveMQ 5.13.0

II. Spring ActiveMq connection-factory

For configuring ActiveMQ ContainerFactory, we need to setup a new bean ConnectionFactory to override the auto-configured connection-factory bean of SpringBoot.

Then use the ConnectionFactory bean to configure 2 beans {JmsListenerContainerFactory, JmsTemplate}:

JmsTemplate bean is used to send Jms messages.
JmsListenerContainerFactory is used to listen Jms messages.

III. Practice

In the tutorial, We use SpringBoot to create a Spring JMS ActiveMQ application with explicitly ActiveMQ-ConnectionFactory configuration.

spring activemq connectionfactory - architecture ws

Step to do
– Create SpringBoot project
– Explicitly configure ActiveMq connection-factory
– Implement JmsProducer, JmsListener
– Create a simple RestApi
– Run and check resutls

1. Create SpringBoot project

Using Spring Tool Suite to create a Spring Starter Project, then add dependencies:

2. Explicitly configure ActiveMq connection-factory

– Create a Java configuration file ActiveMqConnectionFactoryConfig for 3 beans {ConnectionFactory, JmsListenerContainerFactory, JmsTemplate}

Open file, makes configuration for {brokerUrl, userName, password, queue}:

3. Implement JmsProducer, JmsListener

Use JmsTemplate which had been setup in ActiveMqConnectionFactoryConfig to create a JmsProducer component for sending Jms messages:

Use annotation @JmsListener and jsaFactory containerFactory (which had been setup in ActiveMqConnectionFactoryConfig) to create a JmsConsumer component for listenning Jms messages:

4. Create a simple RestApi

Create a simple RestController with only one restApi: ‘/produce’ for sending message:

5. Run and check resutls

Start ActiveMQ server with commandline: C:\apache-activemq-5.13.0>.\bin\activemq start.
Build and Run the SpringBoot project with commandlines: mvn clean install and mvn spring-boot:run

Now sending messages with 2 requests:
http://localhost:8080/produce?msg=Hello World!

-> Server Log: “Recieved Message: Hello World!”

http://localhost:8080/produce?msg=This is a message from JavaSampleApproach!

-> Server Log: “Recieved Message: This is a message from JavaSampleApproach!”

spring activemq connectionfactory - activemq console

IV. Sourcecode


By grokonez | June 30, 2017.

Related Posts

Got Something To Say:

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