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;
      }
   });
}

rabbitmq RabbitTemplate_第1张图片

@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!");
}

你可能感兴趣的:(rabbitmq RabbitTemplate)