Helius
2020-12-15 2914588a65371a3ce43f678cde0a26cd8da26611
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package com.matrix.component.rabbitmq;
 
import com.matrix.core.tools.LogUtil;
import com.matrix.core.tools.StringUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
 
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeoutException;
 
/**
 * mq消息模板
 *
 * @Author jyy
 */
public class RabiitMqTemplate {
 
    private Connection connection;
 
    private Map<String, MqTask> taskMap = new HashMap<>();
 
    public RabiitMqTemplate(Connection connection) {
        this.connection = connection;
    }
 
    /**
     * 申明一个交换机
     *
     * @param exchangeName
     * @param direct
     * @throws IOException
     */
    public void exchangeDeclare(String exchangeName, String direct) throws IOException {
        Channel channel = connection.createChannel();
        channel.exchangeDeclare(exchangeName, direct);
    }
 
    /**
     * 绑定任务队列以及交换机的关系
     *
     * @param taskList
     * @throws IOException
     */
    public void binding(List<MqTask> taskList) throws IOException {
        for (MqTask task : taskList) {
            LogUtil.info("绑定任务{}", task.getRoutingKey());
            Channel channel = connection.createChannel();
            //申明队列
            channel.queueDeclare(task.getQueue(), true, false, false, null);
            //绑定队列
            channel.queueBind(task.getQueue(), task.getExchange(), task.getRoutingKey());
 
            if (task.getHander() != null) {
                LogUtil.info("消费者不为空{}", task.getHander());
                channel.basicConsume(task.getQueue(), task.getAutoAck(), task.getHander(), consumerTag -> {
                    LogUtil.debug("客户端取消");
                });
 
            } else {
                LogUtil.info("消费者未定义,跳过绑定");
            }
            taskMap.put(task.getRoutingKey(), task);
 
        }
 
 
    }
 
    /**
     * 根据路由key发送消息到交换机
     *
     * @param routingKey
     * @param content
     */
    public void sendMsg(String routingKey, String content) {
        Channel channel = null;
        try {
            channel = connection.createChannel();
            if (channel != null) {
                MqTask task = taskMap.get(routingKey);
                if (task != null) {
                    // 消息内容
                    if (StringUtils.isNotBlank(content)) {
                        channel.basicPublish(task.getExchange(), routingKey, false, null, content.getBytes());
                    } else {
                        LogUtil.info("本次发送空消息");
                        channel.basicPublish(task.getExchange(), routingKey, false, null, null);
                    }
                    //关闭通道和连接
                    channel.close();
                } else {
                    throw new IllegalArgumentException("根据【" + routingKey + "】未获取到绑定的MqTask,请检查路由key是否正确");
                }
            } else {
                LogUtil.error("消息发送失败,通道获取失败 chnnel={},routingKey={},content={}", channel, routingKey, content);
            }
 
        } catch (IOException | TimeoutException e) {
            LogUtil.error("通道IO异常", e);
        }
    }
 
    /**
     * 发送消息到指定的交换机
     * 可以试用与自定义消息和topic广播消息
     *
     * @param routingKey
     * @param content
     */
    public void sendTopicMsg(String exchangeName, String routingKey, String content) {
        Channel channel = null;
        try {
            channel = connection.createChannel();
            if (channel != null) {
 
                    // 消息内容
                    if (StringUtils.isNotBlank(content)) {
                        channel.basicPublish(exchangeName, routingKey, false, null, content.getBytes());
                    } else {
                        LogUtil.info("本次发送空消息");
                        channel.basicPublish(exchangeName, routingKey, false, null, null);
                    }
                    //关闭通道和连接
                    channel.close();
 
            } else {
                LogUtil.error("消息发送失败,通道获取失败 chnnel={},routingKey={},content={}", channel, routingKey, content);
            }
 
        } catch (IOException | TimeoutException e) {
            LogUtil.error("通道IO异常", e);
        }
    }
 
 
 
    /**
     * 发送消息到指定的交换机
     * 可以适用与自定义消息和topic广播消息
     *
     * @param routingKey
     * @param content
     */
    public void sendMsg(String exchangeName, String routingKey, String content) {
        Channel channel = null;
        try {
            channel = connection.createChannel();
            if (channel != null) {
 
                // 消息内容
                if (StringUtils.isNotBlank(content)) {
                    channel.basicPublish(exchangeName, routingKey, false, null, content.getBytes());
                } else {
                    LogUtil.info("本次发送空消息");
                    channel.basicPublish(exchangeName, routingKey, false, null, null);
                }
                //关闭通道和连接
                channel.close();
 
            } else {
                LogUtil.error("消息发送失败,通道获取失败 chnnel={},routingKey={},content={}", channel, routingKey, content);
            }
 
        } catch (IOException | TimeoutException e) {
            LogUtil.error("通道IO异常", e);
        }
    }
 
    public Connection getConnection() {
        return connection;
    }
 
    public void setConnection(Connection connection) {
        this.connection = connection;
    }
}