JMS Application with Spring Boot

spring-boot-starter-web is added just for making an example more dynamic by adding REST Controller. Two other dependencies (spring-boot-starter-artemis and artemis-jms-server) provide API for sending and receiving messages via ActiveMQ Artemis message broker.

A Queue for sending and  receiving messages must be configured, it is done in application.properties file :

spring.artemis.mode=embedded
spring.artemis.embedded.enabled=true
spring.artemis.embedded.queues=springbootQueue
myqueue=springbootQueue

The server is declared to run in embedded mode and the Queue name is “springbootQueue”. The Spring Boot Application class :

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Value("${myqueue}")
    String queue;

    @Bean
    CommandLineRunner start(JmsMessagingTemplate template) {
        return args -> {
            System.out.println("Sending...");
            template.convertAndSend(queue, "Message from Spring Boot!");
        };
    }
}

More about CommandLineRunner – here . The method run(String…args) will be executed after Spring Boot finishes its pre-configuration. Also, this method uses the JmsMessagingTemplate instance, which is autowired in the method automatically. JmsMessagingTemplate instance uses the convenience convertAndSend method to send “Message from Spring Boot!” message in the queue “springbootQueue”.

Consumer class is added for consuming messages :

public class Consumer implements MessageListener {
    @Override
    public void onMessage(Message message) {
        try {
            System.out.println("Received message: " + message.getBody(Object.class));
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}

Consumer class implements MessageListener interface, hence MessageListener’s method onMessage to receive messages asynchronously.

And there is some extra configuration to connect to the ArtemisMQ server :

@Configuration
public class MessagingConfig {
    @Qualifier("jmsConnectionFactory")
    @Autowired
    private ConnectionFactory connectionFactory;
    @Value("${myqueue}")
    private String queue;

    @Bean
    public DefaultMessageListenerContainer messageListener() {
        DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();
        container.setConnectionFactory(this.connectionFactory);
        container.setDestinationName(queue);
        container.setMessageListener(new Consumer());
        return container;
    }
}

@Configuration annotation tells Spring to configured any declared methods annotated with @Bean annotations. ConnectionFactory is @Autowired for creating a connection with the default user identity to the broker. In this case it will create the connection to the ArtemisMQ server with default credentials (usually it is : userName: “admin”, password: “admin” brokerURL: “tcp://localhost:61616”), since ArtemisMQ extensions of ConnectionFactory are presented in the classpath (it is possible because of @EnableAutoConfiguration annotation that is added by @SpringBootApplication). @Bean messageListener defines a bean that will return a DefaultMessageListenerContainer instance. Its properties are :

  • ConnectionFactory instance (ArtemisMQ in this case)
  • Queue name (“springbootQueue”)
  • Consumer instance (must implement MessageListener interface)

Previous Next

Leave a Reply

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