• 本周就是将对象转换成字符串进行传递

1.对象序列化实现

  • bean实现序列化接口
  • 生产者消费者的bean包名必须一致
  • 消息生产者

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    @Service
    public class SendMsgService {

    @Autowired
    private AmqpTemplate amqpTemplate;

    public void sendMsg(Goods goods) {
    byte[] bytes = SerializationUtils.serialize(goods);
    amqpTemplate.convertAndSend("queue1",bytes);
    }
    }
  • 消息消费者

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    @Service
    @RabbitListener(queues = "queue1")
    public class ReviceMsgService {

    @RabbitHandler
    public void receiveMsg(byte[] bs) {
    Goods goods = (Goods) SerializationUtils.deserialize(bs);
    System.out.println(goods);
    }

    }

2.JSON字符串实现

  • 消息生产者

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    @Service
    public class SendMsgService {

    @Autowired
    private AmqpTemplate amqpTemplate;

    public void sendMsg(Goods goods) throws JsonProcessingException {

    ObjectMapper mapper=new ObjectMapper();
    String message=mapper.writeValueAsString(goods);
    amqpTemplate.convertAndSend("queue1",message);

    }
    }
  • 消息消费者

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    @Service
    @RabbitListener(queues = "queue1")
    public class ReviceMsgService {

    @RabbitHandler
    public void receiveMsg(String msg) throws JsonProcessingException {
    ObjectMapper mapper=new ObjectMapper();
    Goods goods=mapper.readValue(msg,Goods.class);
    System.out.println(goods);
    }

    }