当前位置:   article > 正文

RabbitMq传递对象_this.rabbittemplate.convertandsend

this.rabbittemplate.convertandsend

对象

  1. public class Order implements Serializable {
  2. private String no = null;
  3. public String getNo() {
  4. return no;
  5. }
  6. public void setNo(String no) {
  7. this.no = no;
  8. }
  9. }

生产者:

  1. public void sendOrder(String no) {
  2. rabbitTemplate.setConfirmCallback(this);
  3. Order order = new Order();
  4. order.setNo(no);
  5. /* 消息ID */
  6. CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
  7. this.rabbitTemplate.convertAndSend("orderExchange", "topic.message", order, correlationData);
  8. }

消费者:

  1. public void onMessage(Message message, Channel channel) throws Exception {
  2. Order order=(Order)SerializationUtils.deserialize(message.getBody());
  3. long id = message.getMessageProperties().getDeliveryTag();
  4. System.out.println("===接收到的消息:" + order.getNo() + ",id:" + id);
  5. channel.basicAck(id, false);
  6. }

这里要引进lang3的包

  1. <dependency>
  2. <groupId>org.apache.commons</groupId>
  3. <artifactId>commons-lang3</artifactId>
  4. <version>3.8</version>
  5. </dependency>

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/399237
推荐阅读
相关标签
  

闽ICP备14008679号