package com.xcong.excoin.rabbit.producer; import com.xcong.excoin.configurations.RabbitMqConfig; import lombok.extern.slf4j.Slf4j; import org.springframework.amqp.rabbit.connection.CorrelationData; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.amqp.rabbit.core.RabbitTemplate.ConfirmCallback; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.UUID; /** * rabbitMq示例生产者 */ @Slf4j @Component public class OrderProducer implements ConfirmCallback { /** * 配置中配置的RabbitTemplate的是prototype类型,不能直接注入 */ private RabbitTemplate rabbitTemplate; /** * 在构造方法上注入RabbitTemplate * * @param */ @Autowired public OrderProducer(RabbitTemplate rabbitTemplate) { this.rabbitTemplate = rabbitTemplate; rabbitTemplate.setConfirmCallback(this); } /** * P发送消息方法 开多止盈 */ public void sendMorePro(String content) { CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString()); System.out.println("发送开多止盈:" + content + "==pid:" + correlationData.getId()); rabbitTemplate.convertAndSend(RabbitMqConfig.EXCHANGE_A, RabbitMqConfig.ROUTINGKEY_MOREPRO, content, correlationData); } /** * 开空止盈 * * @param content */ public void sendLessPro(String content) { CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString()); System.out.println("发送开空止盈:" + content + "==pid:" + correlationData.getId()); rabbitTemplate.convertAndSend(RabbitMqConfig.EXCHANGE_A, RabbitMqConfig.ROUTINGKEY_LESSPRO, content, correlationData); } /** * 开多止损 * * @param content */ public void sendMoreLoss(String content) { CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString()); System.out.println("发送开多止损:" + content + "==pid:" + correlationData.getId()); rabbitTemplate.convertAndSend(RabbitMqConfig.EXCHANGE_A, RabbitMqConfig.ROUTINGKEY_MORELOSS, content, correlationData); } /** * 开空止损 * * @param content */ public void sendLessLoss(String content) { CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString()); System.out.println("发送开空止损:" + content + "==pid:" + correlationData.getId()); rabbitTemplate.convertAndSend(RabbitMqConfig.EXCHANGE_A, RabbitMqConfig.ROUTINGKEY_LESSLOSS, content, correlationData); } /** * 发送委托交易消息 * * @param content */ public void sendLimit(String content) { CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString()); System.out.println("发送限价委托:" + content + "==pid:" + correlationData.getId()); rabbitTemplate.convertAndSend(RabbitMqConfig.EXCHANGE_A, RabbitMqConfig.ROUTINGKEY_LIMIT, content, correlationData); } /** * 发送爆仓消息 * * @param content */ public void sendCoinout(String content) { CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString()); System.out.println("发送爆仓:" + content + "==pid:" + correlationData.getId()); rabbitTemplate.convertAndSend(RabbitMqConfig.EXCHANGE_A, RabbitMqConfig.ROUTINGKEY_COINOUT, content, correlationData); } /** * 发送价格操作消息 * * @param content */ public void sendPriceOperate(String content) { CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString()); log.info("发送价格操作 : {}==pid : {}", content, correlationData.getId()); rabbitTemplate.convertAndSend(RabbitMqConfig.EXCHANGE_A, RabbitMqConfig.ROUTINGKEY_PRICEOPERATE, content, correlationData); } /** * 发送平仓 * * @param content */ public void sendCloseTrade(String content) { CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString()); log.info("发送平仓消息:{}==pid : {}", content, correlationData.getId()); rabbitTemplate.convertAndSend(RabbitMqConfig.EXCHANGE_A, RabbitMqConfig.ROUTINGKEY_CLOSETRADE, content, correlationData); } /** * 用于确认消息是否成功发送到队列 */ @Override public void confirm(CorrelationData correlationData, boolean ack, String cause) { if (ack) { //System.out.println("消息发送成功"+correlationData.getId()); //LogUtil.info("消息发送成功,correlationId={}", correlationData.getId()); } else { System.out.println("消息发送失败" + correlationData.getId()); //LogUtil.info("消息发送失败,correlationId={}", correlationData.getId()); } } }