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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package com.yanzuoguang.mq.service.impl;
import com.alibaba.fastjson.TypeReference;
import com.rabbitmq.client.Channel;
import com.yanzuoguang.mq.dao.QueueServerDao;
import com.yanzuoguang.mq.dao.QueueServerTokenDao;
import com.yanzuoguang.mq.plan.YzgMqProcedure;
import com.yanzuoguang.mq.service.MessageSendService;
import com.yanzuoguang.mq.service.MessageServerService;
import com.yanzuoguang.mq.service.QueueService;
import com.yanzuoguang.mq.vo.MessageVo;
import com.yanzuoguang.mq.vo.QueueServerTokenVo;
import com.yanzuoguang.mq.vo.QueueServerVo;
import com.yanzuoguang.mq.vo.QueueVo;
import com.yanzuoguang.mq.vo.req.RegisterServerTokenReqVo;
import com.yanzuoguang.mq.vo.req.ServerMessageReqVo;
import com.yanzuoguang.mq.vo.req.ServerQueueReqVo;
import com.yanzuoguang.util.helper.DateHelper;
import com.yanzuoguang.util.helper.JsonHelper;
import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.helper.UrlHelper;
import com.yanzuoguang.util.log.Log;
import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
/**
* 服务器程序发布
*
* @author 颜佐光
*/
@Component
public class MessageServeServiceImpl implements MessageServerService, InitializingBean {
@Autowired
private QueueService queueService;
@Autowired
private QueueServerDao queueServerDao;
@Autowired
private QueueServerTokenDao queueServerTokenDao;
@Autowired
private MessageSendService messageSendService;
@Autowired
private YzgMqProcedure yzgMqProcedure;
private String localName = "";
/**
* Invoked by a BeanFactory after it has set all bean properties supplied
* (and satisfied BeanFactoryAware and ApplicationContextAware).
* <p>This method allows the bean instance to perform initialization only
* possible when all bean properties have been set and to throw an
* exception in the event of misconfiguration.
*
* @throws Exception in the event of misconfiguration (such
* as failure to set an essential property) or if initialization fails.
*/
@Override
public void afterPropertiesSet() throws Exception {
this.localName = UrlHelper.getIp();
}
private String getLocalName(String name) {
return this.getServerName(name, this.localName);
}
private String getServerName(String name, String serverId) {
return String.format("%s:%s", name, serverId);
}
/**
* 建立当前服务器的队列
*
* @param req 请求数据
* @return
*/
@Override
public String createServerQueue(ServerQueueReqVo req) {
// 创建主队列
String queueName = req.getQueueName();
String localQueueName = this.getLocalName(req.getQueueName());
queueService.create(new QueueVo(queueName, queueName, queueName));
// 返回当前队列的名称
return localQueueName;
}
/**
* 删除当前服务器的队列
*
* @param req 请求数据
* @return
*/
@Override
public String removeServerQueue(ServerQueueReqVo req) {
String localName = this.getLocalName(req.getQueueName());
QueueServerVo vo = new QueueServerVo();
vo.setServerId(StringHelper.getMD5Id(localName));
queueServerDao.remove(vo);
queueServerTokenDao.remove(vo);
return "删除成功";
}
/**
* 注册当前服务器的token,超期后需要重新注册
*
* @param req
* @return
*/
@Override
public String registerServerToken(RegisterServerTokenReqVo req) {
String queueName = req.getQueueName();
String localQueueName = this.getLocalName(req.getQueueName());
String serverId = StringHelper.getMD5Id(localQueueName);
String serverTokenId = StringHelper.getMD5Id(req.getToken(), queueName);
QueueServerTokenVo serverTokenVo = new QueueServerTokenVo();
serverTokenVo.setServerTokenId(serverTokenId);
serverTokenVo.setQueueName(queueName);
serverTokenVo.setTokenId(req.getToken());
serverTokenVo.setServerId(serverId);
serverTokenVo.setUpdateDate(DateHelper.getNow());
serverTokenVo.setTokenVersion(StringHelper.getNewID());
if (queueServerTokenDao.load(serverTokenVo, QueueServerTokenVo.class) == null) {
queueServerTokenDao.create(serverTokenVo);
} else {
queueServerTokenDao.update(serverTokenVo);
}
if (req.getFairTime() > 0) {
yzgMqProcedure.sendRemove(req);
}
return serverTokenVo.getServerTokenId();
}
/**
* 删除token的执行
*
* @param req
*/
@ApiOperation(value = "删除token的执行")
@Override
public void removeServerToken(RegisterServerTokenReqVo req) {
String queueName = req.getQueueName();
String serverTokenId = StringHelper.getMD5Id(req.getToken(), queueName);
queueServerTokenDao.remove(serverTokenId);
}
/**
* 注册当前消费队列的回调
*
* @param req 请求数据
* @param listener 处理函数
* @return
*/
@Override
public String setServerQueueConsumer(ServerQueueReqVo req, ChannelAwareMessageListener listener) {
return this.setServerQueueConsumer(req, 0, listener);
}
/**
* 注册当前消费队列的回调
*
* @param req 请求数据
* @param concurrency 消费者数量
* @param listener 处理函数
* @return
*/
@Override
public String setServerQueueConsumer(ServerQueueReqVo req, int concurrency, ChannelAwareMessageListener listener) {
// 删除历史队列
removeServerQueue(req);
// 队列名称
String queueName = req.getQueueName();
String localQueueName = this.getLocalName(req.getQueueName());
String serverId = StringHelper.getMD5Id(localQueueName);
// 创建延迟队列和主队列的关系
queueService.create(new QueueVo(localQueueName, localQueueName, localQueueName, req.getServerWaitTime(),
queueName, queueName, queueName));
this.messageSendService.init(queueName, concurrency, new ChannelAwareMessageListener() {
@Override
public void onMessage(Message message, Channel channel) throws Exception {
String json = new String(message.getBody());
ServerMessageReqVo msg = null;
try {
msg = JsonHelper.deserialize(json, new TypeReference<ServerMessageReqVo>() {
});
sendServerMessage(msg);
} catch (Exception ex) {
Log.error(MqServiceImpl.class, ex);
} finally {
messageSendService.basicAck(message, channel);
}
}
});
// 注册到队列服务器到数据库表
QueueServerVo vo = new QueueServerVo();
vo.setServerId(serverId);
vo.setQueueName(queueName);
vo.setQueueServer(localQueueName);
if (queueServerDao.load(vo, QueueServerVo.class) == null) {
queueServerDao.create(vo);
} else {
queueServerDao.update(vo);
}
// 注册本服务器的唯一识别编码
RegisterServerTokenReqVo to = JsonHelper.to(req, RegisterServerTokenReqVo.class);
to.setToken(localQueueName);
this.registerServerToken(to);
// 设置延迟队列的回调函数
this.messageSendService.init(localQueueName, concurrency, listener);
return localQueueName;
}
/**
* 发送给指定服务器消息
*
* @param req
* @return
*/
@ApiOperation(value = "发送给指定服务器消息")
@Override
public String sendServerMessage(ServerMessageReqVo req) {
// 发送消息,等待下次重新发送
req.addPos();
if (!req.isNext()) {
Log.error(MqServiceImpl.class, "达到最大次数,不会继续发送");
return StringHelper.EMPTY;
}
String queueName = req.getQueueName();
List<String> sendQueueName = new ArrayList<>();
try {
if (!StringHelper.isEmpty(req.getToken())) {
String serverTokenId = StringHelper.getMD5Id(req.getToken(), queueName);
// 获取token所在服务器
QueueServerTokenVo tokenVo = queueServerTokenDao.load(serverTokenId, QueueServerTokenVo.class);
if (tokenVo != null) {
// 获取服务器的队列名称
QueueServerVo server = queueServerDao.load(tokenVo.getServerId(), QueueServerVo.class);
if (server != null) {
sendQueueName.add(server.getQueueServer());
}
}
} else {
// 获取服务器的队列名称
QueueServerVo loadReq = new QueueServerVo();
loadReq.setQueueName(queueName);
List<QueueServerVo> servers = queueServerDao.loadList(loadReq, QueueServerVo.class);
for (QueueServerVo server : servers) {
sendQueueName.add(server.getQueueServer());
}
}
} catch (Exception ex) {
Log.error(MqServiceImpl.class, ex);
}
if (sendQueueName.isEmpty()) {
String json = JsonHelper.serialize(req);
return yzgMqProcedure.send(new MessageVo(req.getQueueName(), req.getQueueName(), json, req.getNextDelayTime()));
} else {
String ret = StringHelper.EMPTY;
for (String name : sendQueueName) {
req.setToken(name);
String json = JsonHelper.serialize(req);
ret = yzgMqProcedure.send(new MessageVo(name, name, json));
}
return ret;
}
}
}