|
1 | 1 | package com.souyunku.example.springboot.rabbitmq.ack.producer; |
2 | 2 |
|
| 3 | +import com.souyunku.example.springboot.rabbitmq.ack.receiver.HelloReceiver; |
| 4 | +import org.slf4j.Logger; |
| 5 | +import org.slf4j.LoggerFactory; |
3 | 6 | import org.springframework.amqp.core.Message; |
4 | 7 | import org.springframework.amqp.rabbit.core.RabbitTemplate; |
| 8 | +import org.springframework.amqp.rabbit.support.CorrelationData; |
| 9 | +import org.springframework.beans.factory.InitializingBean; |
5 | 10 | import org.springframework.beans.factory.annotation.Autowired; |
6 | 11 | import org.springframework.stereotype.Component; |
7 | 12 |
|
8 | 13 | import java.util.Date; |
9 | 14 |
|
10 | 15 | @Component |
11 | | -public class HelloSender implements RabbitTemplate.ReturnCallback { |
| 16 | +public class HelloSender implements RabbitTemplate.ReturnCallback, RabbitTemplate.ConfirmCallback { |
| 17 | + |
| 18 | + private Logger logger = LoggerFactory.getLogger(HelloSender.class); |
12 | 19 |
|
13 | 20 | @Autowired |
14 | 21 | private RabbitTemplate rabbitTemplate; |
15 | 22 |
|
16 | 23 | public void sendMessage(String context) { |
17 | | - System.out.println("HelloReceiver发送内容: " + context + ",发送时间:" + new Date()); |
18 | | - |
19 | 24 |
|
20 | 25 | // 消息发送失败返回到队列中, application.properties 配置 spring.rabbitmq.publisher-returns=true |
21 | 26 | rabbitTemplate.setMandatory(true); |
22 | 27 |
|
23 | 28 | this.rabbitTemplate.setReturnCallback(this); |
| 29 | + this.rabbitTemplate.setConfirmCallback(this); |
24 | 30 |
|
25 | 31 | this.rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> { |
26 | 32 | if (!ack) { |
27 | | - System.out.println("sendMessage 发送失败" + cause + correlationData.toString()); |
| 33 | + logger.info("HelloSender 发送失败:" + cause + correlationData.toString()); |
28 | 34 | } else { |
29 | | - System.out.println("sendMessage 发送成功 "); |
| 35 | + logger.info("HelloSender 发送成功"); |
30 | 36 | } |
31 | 37 | }); |
| 38 | + |
| 39 | + logger.info("HelloSender 发送的消息内容:{}", context); |
| 40 | + |
32 | 41 | this.rabbitTemplate.convertAndSend("hello", context); |
| 42 | + |
33 | 43 | } |
34 | 44 |
|
35 | 45 |
|
36 | 46 | /** |
37 | 47 | * 失败后返回消息回调 |
38 | | - * |
39 | | - * @param message 消息返回的消息 |
40 | | - * @param replyCode 回复代码 |
41 | | - * @param replyText 回复文本 |
42 | | - * @param exchange 交换交换 |
43 | | - * @param routingKey 路由密钥 |
| 48 | + * <p> |
| 49 | + * 当消息发送出去找不到对应路由队列时,将会把消息退回 |
| 50 | + * 如果有任何一个路由队列接收投递消息成功,则不会退回消息 |
44 | 51 | */ |
45 | 52 | @Override |
46 | 53 | public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) { |
47 | | - System.out.println("return--message:" + new String(message.getBody()) + ",replyCode:" + replyCode + ",replyText:" + replyText + ",exchange:" + exchange + ",routingKey:" + routingKey); |
| 54 | + logger.info("return--message:" + new String(message.getBody()) + ",replyCode:" + replyCode + ",replyText:" + replyText + ",exchange:" + exchange + ",routingKey:" + routingKey); |
48 | 55 | } |
49 | 56 |
|
| 57 | + /** |
| 58 | + * 实现ConfirmCallback |
| 59 | + * <p> |
| 60 | + * ACK=true仅仅标示消息已被Broker接收到,并不表示已成功投放至消息队列中 |
| 61 | + * ACK=false标示消息由于Broker处理错误,消息并未处理成功 |
| 62 | + */ |
| 63 | + @Override |
| 64 | + public void confirm(CorrelationData correlationData, boolean ack, String cause) { |
| 65 | + logger.info("消息id: " + correlationData + "确认" + (ack ? "成功:" : "失败")); |
| 66 | + } |
| 67 | + |
| 68 | + |
50 | 69 |
|
51 | 70 | } |
0 commit comments