RabbitMQ – How to send/receive Java object messages with Spring RabbitMq | SpringBoot

In the previous posts, Spring RabbitMQ applications had produced/consumed String messages. Today, JavaSampleApproach will guide how to send/receive Java object messages with RabbitMQ server.

Related posts:
RabbitMq – How to create Spring RabbitMq Publish/Subcribe pattern with SpringBoot
RabbitMQ – How to create Spring RabbitMQ Producer/Consumer applications with SpringBoot
SpringBoot RabbitMQ Topic Exchange
SpringBoot RabbitMq Headers Exchange

I. Technologies

– Java 8
– Maven 3.6.1
– Spring Tool Suite: Version 3.8.4.RELEASE
– Spring Boot: 1.5.4.RELEASE
– RabbitMQ

II. RabbitMq – Produce/consume Java object messages

In the tutorial, we create 2 SpringBoot applications {Producer, Consumer} for sending/receiving Java object messages to/from RabbitMQ:

Spring RabbitMq - Send Java Objects - architecture

1. Message Converter

We create a simple Java model:

Then send a Company object to RabbitMQ by segment code:

And receiving it from Consumer by segment code:

-> Got Exception:

How to resolve it? -> We need a MessageConverter for Producer and Consumer:

– Producer: set MessageConverter for rabbitTemplate

– Consumer: set MessageConverter for listenerContainerFactory

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 RabbitMQ again:

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

Why? -> Beacause of the Bidirectional-Relationships between Company object and Product objects.

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

– Produce

III. Practice

We create 2 SpringBoot projects {Producer, Consumer}:

Spring RabbitMq - Send Java Objects - project structures

Step to do:
– Create SpringBoot projects
– Create Java models
– Create RabbitMq configuration
– Implement RabbitMq producer/consumer
– Implement sending client
– Run and check results

1. Create SpringBoot projects

Create 2 SpringBoot projects {Producer, Consumer}, then add dependency:

2. Create Java models

– Company:

– Product

3. Create RabbitMq Configuration

For each projects, open application.properties file, configure spring.rabbitmq.*:

For logging in file, under /src/main/resources folder, create a logback-spring.xml file:

3.1 Producer Config

– Create a RabbitMqConfig:

Note: we create 3 bean {Queue, DirectExchange, Binding} for automatically creating a direct RabbitMq exchange, a queue and binding them together.

3.2 Consumer Config

– Create a RabbitMqConfig:

4. Implement RabbitMq Producer/Consumer
4.1 Implement Producer

4.2 Consumer

5. Implement Sending Client

In SpringBoot main class, use CommandLineRunner to implement a client for producer:

6. Run and check results

Build and run the SpringBoot projects {Producer, Consumer} with commandlines: mvn clean install, mvn spring-boot:run.

– Go to http://localhost:15672/#/exchanges/%2F/jsa.exchange
-> Checking the automatically creating and binding of RabbitMq exchange jsa.exchange and queue jsa.queue with routingKey jsa.routingkey:

Spring RabbitMq - Send Java Objects - exchange binding with queue

– Checking console logs:
-> From Producer: ‘Send msg = {"name":"Apple","products":[{"name":"Iphone 7"},{"name":"IPadPro"}]}
-> From Consumer: ‘Recieved Message: {"name":"Apple","products":[{"name":"Iphone 7"},{"name":"IPadPro"}]}

IV. Sourcecode


By grokonez | July 12, 2017.

Last updated on October 11, 2017.

Related Posts

3 thoughts on “RabbitMQ – How to send/receive Java object messages with Spring RabbitMq | SpringBoot”

  1. Hi! Great article!
    Question: what if the consumer does not know details of the objects in the queue? I mean, the consumer eclipse project does not have to have the pojos synchronized with the producer project. Could the publisher and suscriber be run in the same application?

Got Something To Say:

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