package com.example.demo; import javax.jms.ConnectionFactory; import javax.jms.JMSException; import org.apache.activemq.command.ActiveMQTextMessage; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jms.DefaultJmsListenerContainerFactoryConfigurer; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.jms.annotation.EnableJms; import org.springframework.jms.config.DefaultJmsListenerContainerFactory; import org.springframework.jms.config.JmsListenerContainerFactory; import org.springframework.jms.core.JmsTemplate; import org.springframework.jms.support.converter.MappingJackson2MessageConverter; import org.springframework.jms.support.converter.MessageConverter; import org.springframework.jms.support.converter.MessageType; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; @SpringBootApplication @EnableJms @Controller public class JmstestApplication { static Object context=null; @Bean public JmsListenerContainerFactory myFactory(ConnectionFactory connectionFactory, DefaultJmsListenerContainerFactoryConfigurer configurer) { DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory(); // This provides all boot's default to this factory, including the message converter configurer.configure(factory, connectionFactory); // You could still override some of Boot's default if necessary. return factory; } @Bean // Serialize message content to json using TextMessage public MessageConverter jacksonJmsMessageConverter() { MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter(); converter.setTargetType(MessageType.TEXT); converter.setTypeIdPropertyName("_type"); return converter; } public static void main(String[] args) { // Launch the application context = SpringApplication.run(JmstestApplication.class, args); } @RequestMapping("/hello") public String hello(@RequestParam(value = "name", defaultValue = "World") String name) { JmsTemplate jmsTemplate = ((ConfigurableApplicationContext) context).getBean(JmsTemplate.class); // Send a message with a POJO - the template reuse the message converter System.out.println("Sending an test message. "); jmsTemplate.convertAndSend("testjms", new TestMsg("testjms", "test message")); return "redirect:/jmsReply"; } @RequestMapping("/jmsReply") @ResponseBody public String jmsReply() throws JMSException { JmsTemplate jmsTemplate = ((ConfigurableApplicationContext) context).getBean(JmsTemplate.class); ActiveMQTextMessage replyMsg = (ActiveMQTextMessage) jmsTemplate.receive("testjms2Reply"); return replyMsg.getText(); } }