我使用
Spring API的JmsTemplate和MappingJackson2MessageConverter(版本:spring-jms-4.3.4.RELEASE.jar)来发布消息
到ActiveMQ主题,如下面的代码所示.
@H_403_15@解决方法
到ActiveMQ主题,如下面的代码所示.
Topicpublisher类:
@Component public class Topicpublisher { @Autowired private JmsTemplate jmsTemplate; @Autowired private MessageConverter messageConverter; public void send() { Product product = new Product(); product.setName("abcd"); product.setPrice(10); jmsTemplate.setMessageConverter(messageConverter); jmsTemplate.convertAndSend("product.topic",product); } }
MappingJackson2MessageConverter类:
@Configuration public class JMSTextMessageConverter { @Bean public MessageConverter jacksonJmsMessageConverter() { MappingJackson2MessageConverter mappingJackson2MessageConverter = new MappingJackson2MessageConverter(); mappingJackson2MessageConverter.setTargetType(MessageType.TEXT); mappingJackson2MessageConverter.setTypeIdPropertyName("_type"); return mappingJackson2MessageConverter; } }
您可以通过发送MessagePostProcessor从JmsTemplate使用convertAndSendmethod添加自定义属性,如下所示:
jmsTemplate.convertAndSend("product.topic",product,new MessagePostProcessor() { @Override public Message postProcessMessage(Message message) throws JMSException { message.setStringProperty("my_property","my_value"); return message; } });