对象
public class Order implements Serializable {
private String no = null;
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
}
生产者:
public void sendOrder(String no) {
rabbitTemplate.setConfirmCallback(this);
Order order = new Order();
order.setNo(no);
/* 消息ID */
CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
this.rabbitTemplate.convertAndSend("orderExchange", "topic.message", order, correlationData);
}
消费者:
public void onMessage(Message message, Channel channel) throws Exception {
Order order=(Order)SerializationUtils.deserialize(message.getBody());
long id = message.getMessageProperties().getDeliveryTag();
System.out.println("===接收到的消息:" + order.getNo() + ",id:" + id);
channel.basicAck(id, false);
}
这里要引进lang3的包
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8</version>
</dependency>