Anirban
Anirban

Reputation: 949

Send message to a JMS queue using Spring Boot

I am new to Spring JMS. My application is developed using Spring Boot and is deployed in JBoss EAP 7.2.0. I have a remote queue which is an ActiveMQ Artemis queue which is also embedded within JBoss EAP 7.2.0. Can anyone please suggest me how to send a message to the remote JMS queue using JmsTemplate of Spring Boot? Basically I am not getting how should I define the remote ConnectionFactory to connect to the remote queue.

Upvotes: 3

Views: 6854

Answers (1)

  1. Add the following to application properties as your application is deployed in application server
  spring.jms.jndi-name=java:/<your connection factory name for artemis>
  1. Add artemis dependency and let spring boot autoconfigure jmsTemplate
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-artemis</artifactId>
   </dependency>
  1. Autowire jmsTemplate and send message
@Component
public class MyMessageSender {

        @Autowired
        JmsTemplate jmsTemplate;


        public void send(String msg){
                jmsTemplate.convertAndSend("my.queue.name", msg);
        }
}
  1. Optionally you can configure message converters and send pojos as message and let spring take care of converting it to json. For example
    @Bean // Serialize message content to json using TextMessage
    public MessageConverter jacksonJmsMessageConverter() {
        MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
        converter.setTargetType(MessageType.TEXT);
        converter.setTypeIdPropertyName("_type");
        return converter;
    }

Upvotes: 2

Related Questions