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() + " 发送失败");
|
|
}
|
|
/**
|
* 发送消息,不需要实现任何接口,供外部调用。
|
* @param routingKey
|
* @param msg
|
*/
|
public void send(String routingKey,String msg){
|
rabbitTemplate.convertAndSend(sysConfig.getRmqExchangeName(),routingKey,msg);
|
}
|
|
/**
|
* 发送消息,不需要实现任何接口,供外部调用。
|
* @param exchangeName
|
* @param routingKey
|
* @param msg
|
*/
|
public void send(String exchangeName,String routingKey,String msg){
|
rabbitTemplate.convertAndSend(exchangeName, routingKey, msg);
|
}
|
}
|