Spring Jms ActiveMq – How to send Java object messages to ActiveMQ server (specially with Bi-Directional relationship Java objects)

The previous Spring ActiveMQ tutorials, We had learned How to produce and consume JMS messages with String format and configure Explicity ActiveMq ConnectionFactory. But How to send Java object messages to ActiveMQ server? With the tutorial, JavaSampleApproach will show you how to produce/consume Java object messages with Spring JMS ActiveMq for both case: normal Java objects and complex Java objects with Bi-directional relationship.

Related posts:
How to use Spring JMS with ActiveMQ – JMS Consumer and JMS Producer | Spring Boot
ActiveMq – Explicitly configure Spring ActiveMq ConnectionFactory with SpringBoot
How to resolve Json Infinite Recursion problem when working with Jackson
ActiveMq – How to work with Spring JMS ActiveMq Topic (Publisher-Subcribers pattern) using SpringBoot

I. Sending Java object messages to ActiveMq

1. Jms MessageConverter

We start with a simple model:

Send a Company object to ActiveMq:

-> thrown MessageConversionException exception:

Why?
The default MessageConverter is able to convert only basic types (such as String, Map, Serializable). But Company is not serializable, so we need to use Jackson to serialize the content to Json in text format.
-> Solution: setup a jacksonJmsMessageConverter bean as below,

Then use jacksonJmsMessageConverter as message converter for {JmsListenerContainerFactory, JmsTemplate}

2. Bi-Directional Java object messages

Now we change models with more complex structure – Bi-Directional relationship:

– Company:

– Product

Init a Company object as below:

Then send a message to ActiveMQ again:

-> We got an Infinite recursion (StackOverflowError) exception:

Why? -> Beacause of the Bidirectional-Relationships between Company object and Product objects.
See more at: Json Infinite Recursion problem

-> Solution:
We have can refer solutions at How to resolve Json Infinite Recursion problem when working with Jackson.

For preserving Bidirectional-Relationships when deserialize Json strings to Java objects, we can choose a solution with @JsonIdentityInfo, details as below code:

– Company:

– Product

II. Practice

In the tutorial, We create a Spring JMS ActiveMQ application which can send Bi-Directional Java object messages to ActiveMQ server.

spring activemq java object message - architecture ws

Step to do
– Create SpringBoot project
– Create Bi-Directional Java models
– Configure ActiveMq connection-factory with MessageConverter
– Implement JmsProducer, JmsListener
– Implement Client
– Run and check resutls

spring activemq java object message - project structure

1. Create SpringBoot project

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

2. Create Bi-Directional Java models

Create 2 Java models {Company, Product}:

– Company

– Product

3. Configure ActiveMq connection-factory with MessageConverter

– Create a Java configuration file ActiveMqConnectionFactoryConfig with 4 beans {ConnectionFactory, JmsListenerContainerFactory, JmsTemplate, MessageConverter}:

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

4. Implement JmsProducer, JmsListener

Using 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:

5. Implement Client

In the main class SpringActiveMqApplication, using CommandLineRunner interface to implemnt a sending client:

6. Run and check resutls

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

-> Results:

– Server’s Log:

spring activemq java object message - activemq log

– See Bi-Directional deserialized object with Debug mode:

spring activemq java object message - bi-directional relationship with debug mode

III. Sourcecode

SpringActiveMQ


Related Posts



Got Something To Say:

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

*