1
13693261870
2022-09-16 58d012f11dd34564d81b4eb3a6099eb689876597
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package org.apereo.cas.web.landtool.rabbitmq;
 
import javax.annotation.PostConstruct;
 
import org.apereo.cas.web.landtool.rabbitmq.config.RabbitMQProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate.ReturnCallback;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
/**
 * @author wangqin
 *    2019-07-05
 */
@Component
public class RadarRabbitMQTemplate implements RabbitTemplate.ConfirmCallback, ReturnCallback{
    private static final Logger LOGGER = LoggerFactory.getLogger(RadarRabbitMQTemplate.class);
    
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private RabbitMQProperties sysConfig;
    
    @PostConstruct
    public void init() {
        rabbitTemplate.setConfirmCallback(this);
        rabbitTemplate.setReturnCallback(this);
    }
 
    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        if (ack) {  
            LOGGER.debug("消息发送成功:" + correlationData);  
        } else {  
            LOGGER.debug("消息发送失败:" + cause);  
        }  
        
    }
 
    @Override
    public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
        LOGGER.debug(message.getMessageProperties().getCorrelationIdString() + " 发送失败");
        
    }
 
    //发送消息,不需要实现任何接口,供外部调用。
    public void send(String routingKey,String msg){
        rabbitTemplate.convertAndSend(sysConfig.getRmqExchangeName(),routingKey,msg);
    }
    
    //发送消息,不需要实现任何接口,供外部调用。
    public void send(String exchangeName,String routingKey,String msg){
        rabbitTemplate.convertAndSend(exchangeName, routingKey, msg);
    }
}