Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 632
Anirban Avatar asked Oct 29 '25 13:10

Anirban


1 Answers

  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;
    }
like image 94
Kavithakaran Kanapathippillai Avatar answered Oct 31 '25 09:10

Kavithakaran Kanapathippillai



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!