RabbitMQ message listener in Spring Boot

Last time I was introducing a RabbitMQ message receiver in an application using Spring Boot 1.5.4. To match the version of the message broker in an organisation I used spring-rabbit 1.7.3. As I was interested in receiving messages from a queue in a transaction I’ve quickly found the blog-post Using transactions with RabbitMQ and SQL database in Spring Boot application. The example of the message listener bean given there is:

@Component
@Transactional
@RabbitListener(queues = {"myqueue"})
public class RabbitReceiver {

    @RabbitHandler
    public void receive(String event) {
        //...
    }
}

The problem is this doesn’t work! The application was not receiving messages from a RabbitMQ queue which were successfully sent and were visible in RabbitMQ Management Console.

Solution

I’ve changed this bean in such a way:

@Component
public class RabbitReceiver {

    @Transactional
    @RabbitListener(queues = {"myqueue"})
    public void receive(String event) {
        //...
    }
}

…and things started to work!. Switching to @RabbitListener annotation at the method level solved the problem!

BTW: apart from the above problem the mentioned blog-post is a good example of implementing RabbitMQ message sender and message listener in a Spring Boot application, so you can refer to it for other details.

About krzysztoftomaszewski

I've got M.Sc. in software engineering. I graduated in 2005 at Institute of Computer Science, Warsaw University of Technology, Faculty of Electronics and Information Technology. I'm working on computer software design and engineering continuously since 2004.
This entry was posted in Java, messaging, Spring and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s