1.配置文件 ---->详情---->移步 https://www.cnblogs.com/zyhzsq/p/13397543.html
2.发送消息
public void send(Object message, Map<String, Object> map) {
MessageHeaders messageHeaders = new MessageHeaders(map);
Message<?> msg = MessageBuilder.createMessage(message, messageHeaders);
rabbitTemplate.setConfirmCallback(confirmCallback);
CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
rabbitTemplate.convertAndSend("miaosha_exchange", "zyh.miaosha", msg, messagePostProcessor, correlationData);
}
3.接收消息
@RabbitHandler
@RabbitListener(queues = {"miaosha_queue"})
public void onMessage(Message message, Channel channel) throws IOException {
String o = (String) message.getPayload();
boolean store = ordersService.store(o);
if (store) {
channel.basicAck((Long) message.getHeaders().get(AmqpHeaders.DELIVERY_TAG), true);
System.out.println("消费成功");
}else{
channel.basicNack((Long) message.getHeaders().get(AmqpHeaders.DELIVERY_TAG),true,false);
//
System.out.println("消息被拒绝");
}
}