1. Introduction

In this short tutorial, we'll show how to send a JMS message to the RabbitMQ queue using a Spring Boot application and JmsTemplate. For the beginning, let's say that RabbitMQ is not a JMS provider by default but includes a specific plugin needed to support the JMS Queue and Topic messaging models.

2. Implementation

In this example, we'll use Spring Scheduler to make events that will produce messages that we'll package to be suitable for JMS. Messages will be simple text messages containing randomly created UUID.

2.1. Maven dependencies

First, let's see which Maven dependencies for our use-case:

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-jms</artifactId>
</dependency>
<dependency>
  <groupId>com.rabbitmq.jms</groupId>
  <artifactId>rabbitmq-jms</artifactId>
  <version>2.1.1</version>
</dependency>

Since JMS is just API definition, we'll need some components from RabbitMQ JMS implementation, and also Spring JMS implementation to be able to use all Spring JMS shortcuts.

We'll try to keep code as simple as possible, and also hardcode all params for the simplicity purpose. The complete example code could be found on our GitHub.

2.2. Annotations

Let's add the proper annotation to our class that contains main method. The first one will be @EnableJms that will allow our application to send JMS messages. The second one will be @EnableScheduling, that's not needed for messaging itself, but for creating events that will trigger the sending of JMS messages.

So, our main class will look like this:

@SpringBootApplication
@EnableJms
@EnableScheduling
public class RabbitMqDemoApplication {
  
  public static void main(String[] args) throws Exception {
    SpringApplication.run(RabbitMqDemoApplication.class, args);
  }
}

2.3. JMS Message Sender

And now, let's see how does our MessageSender class look like:

import java.util.UUID;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.TextMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@Service
public class MessageSender {

  @Autowired
  ConnectionFactory connectionFactory;
  
  @Scheduled(fixedRate = 1000)
  public void rpcWithSpringJmsTemplate() throws Exception {
    Connection clientConnection = connectionFactory.createConnection();
    clientConnection.start();
    String messageContent = UUID.randomUUID().toString();
    
    JmsTemplate tpl = new JmsTemplate(connectionFactory);
    tpl.setReceiveTimeout(2000);
    tpl.send("demoqueue", session -> {
		TextMessage message = session.createTextMessage(messageContent);
			message.setJMSCorrelationID(messageContent);
			return message;
		});
	}
}

Notice that we're using fixedRate on @Scheduler that will send a message every single second.

2.4. Connection Factory Configuration

Also, notice that in code we have ConnectionFactory that we configured in the following class:

@Configuration
public class RabbitMqDemoConfig {

  @Bean
  public ConnectionFactory jmsConnectionFactory() {
    RMQConnectionFactory connectionFactory = new RMQConnectionFactory();
    connectionFactory.setUsername("guest");
    connectionFactory.setPassword("guest");
    connectionFactory.setVirtualHost("/");
    connectionFactory.setHost("localhost");
    connectionFactory.setPort(5672);
    return connectionFactory;
  }
}

Here, we're using and configuring RMQConnectionFactory as an implementation of javax.jms.ConnectionFactory interface. We're hardcoding the connection parameters needed to connect to the RabbitMQ server that's running on the localhost/on my machine.

2.5. Testing our JMS sender

Let's start our application and check the status of queues on the RabbitMQ management console available on http://localhost:15672/#/queues in our example. Notice that after a short time our demoqueue got 79 messages since it's getting one message per second. You'll see in the column Message rates/incoming the average rate for the specific queue. Also, when the queue is active - it receives messages or messages are read from it, it will have status running.

Send JMS message to RabbitMQ queue with Spring Boot

If you're not able to access this console, check out this tutorial to see how to set it up

3.Conclusion

In this tutorial, we showed how to send JMS message to RabbitMQ queue with Spring Boot and RabbitMQ implementation of JMS. The complete code example is available on GitHub.