其他分享
首页 > 其他分享> > rabbitmq RabbitTemplate

rabbitmq RabbitTemplate

作者:互联网

@Bean
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
   RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
   return rabbitTemplate;
}
@Test
public void testSendMessage() throws Exception {
   //1 创建消息
   MessageProperties messageProperties = new MessageProperties();
   messageProperties.getHeaders().put("desc", "信息描述..");
   messageProperties.getHeaders().put("type", "自定义消息类型..");
   Message message = new Message("Hello RabbitMQ".getBytes(), messageProperties);

   rabbitTemplate.convertAndSend("topic001", "spring.amqp", message, new MessagePostProcessor() {
      @Override
      public Message postProcessMessage(Message message) throws AmqpException {
         System.err.println("------添加额外的设置---------");
         message.getMessageProperties().getHeaders().put("desc", "额外修改的信息描述");
         message.getMessageProperties().getHeaders().put("attr", "额外新加的属性");
         return message;
      }
   });
}

@Test
public void testSendMessage2() throws Exception {
   //1 创建消息
   MessageProperties messageProperties = new MessageProperties();
   messageProperties.setContentType("text/plain");
   Message message = new Message("mq 消息1234".getBytes(), messageProperties);

   rabbitTemplate.send("topic001", "spring.abc", message);

   rabbitTemplate.convertAndSend("topic001", "spring.amqp", "hello object message send!");
   rabbitTemplate.convertAndSend("topic002", "rabbit.abc", "hello object message send!");
}

标签:RabbitTemplate,rabbitTemplate,messageProperties,rabbitmq,getHeaders,new,message,
来源: https://blog.csdn.net/qq_33002953/article/details/90452810