package cc.mrbird.febs.rabbit.producter;
|
|
import cc.mrbird.febs.rabbit.constants.ExchangeConstants;
|
import cc.mrbird.febs.rabbit.constants.RouteKeyConstants;
|
import cc.mrbird.febs.rabbit.enumerates.RabbitQueueEnum;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.amqp.AmqpException;
|
import org.springframework.amqp.core.Message;
|
import org.springframework.amqp.core.MessagePostProcessor;
|
import org.springframework.amqp.rabbit.connection.CorrelationData;
|
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Component;
|
|
import javax.validation.constraints.NotNull;
|
import java.util.Date;
|
import java.util.UUID;
|
|
/**
|
* @author wzy
|
* @date 2021-09-25
|
**/
|
@Slf4j
|
@Component
|
public class AgentProducer implements RabbitTemplate.ConfirmCallback {
|
|
/**
|
* 配置中配置的RabbitTemplate的是prototype类型,不能直接注入
|
*/
|
private RabbitTemplate rabbitTemplate;
|
|
/**
|
* 在构造方法上注入RabbitTemplate
|
*
|
* @param
|
*/
|
@Autowired
|
public AgentProducer(RabbitTemplate rabbitTemplate) {
|
this.rabbitTemplate = rabbitTemplate;
|
rabbitTemplate.setConfirmCallback(this);
|
}
|
|
@Override
|
public void confirm(CorrelationData correlationData, boolean ack, String cause) {
|
|
}
|
|
public void sendOrderReturn(Long id) {
|
log.info("发送消息:{}", id);
|
CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
|
rabbitTemplate.convertAndSend(RabbitQueueEnum.ORDER_RETURN_MONEY.getExchange(), RabbitQueueEnum.ORDER_RETURN_MONEY.getRoute(), id, correlationData);
|
}
|
|
public void sendOrderCancelDelayMsg(Long id, Long times) {
|
log.info("发送自动取消订单消息:{}, {}", id, times);
|
rabbitTemplate.convertAndSend(RabbitQueueEnum.ORDER_CANCEL_DELAY_TTL.getExchange(), RabbitQueueEnum.ORDER_CANCEL_DELAY_TTL.getRoute(), id, new MessagePostProcessor() {
|
@Override
|
public Message postProcessMessage(Message message) throws AmqpException {
|
message.getMessageProperties().setExpiration(String.valueOf(times));
|
return message;
|
}
|
});
|
}
|
|
/**
|
* 发送代理自动升级消息
|
*
|
* @param memberId
|
*/
|
public void sendAutoLevelUpMsg(@NotNull Long memberId) {
|
log.info("发送代理自动升级消息:{}", memberId);
|
rabbitTemplate.convertAndSend(ExchangeConstants.EXCHANGE_DEFAULT, RouteKeyConstants.ROUTE_KEY_DEFAULT, memberId);
|
}
|
|
public void sendReturnMoneyMsg(Long orderId) {
|
log.info("发送返利消息:{}", orderId);
|
rabbitTemplate.convertAndSend(RabbitQueueEnum.AGENT_RETURN_MONEY.getExchange(), RabbitQueueEnum.AGENT_RETURN_MONEY.getRoute(), orderId);
|
}
|
}
|