Helius
2020-06-01 4b844113469b203adc40f1e540de98612321f26e
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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());
        System.out.println("发送价格操作:" + content + "==pid:" + 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());
        }
    }
 
 
}