UcGatewayIptv2IptvConsumer.java
7.45 KB
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
package com.topdraw.mq.consumer;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.Channel;
import com.topdraw.mq.domain.DataSyncMsg;
import com.topdraw.resttemplate.RestTemplateClient;
import com.topdraw.util.FileUtil;
import com.topdraw.util.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import java.io.IOException;
import java.time.LocalDate;
import java.util.Map;
import java.util.Objects;
@Component
@Slf4j
public class UcGatewayIptv2IptvConsumer {
@Autowired
RestTemplateClient restTemplateClient;
@Autowired
AutoRoute autoUser;
@Value("#{rabbitMqErrorLogConfig.getUcgError()}")
private Map<String, String> error;
/**
* 事件
* @param content
* @description 基础数据同步
* @author Hongyan Wang
* @date 2021/9/7 11:26 上午
*/
@RabbitHandler
@RabbitListener(queues = "#{rabbitMqSourceConfig.getUcgEventQueue()}",
containerFactory = "#{rabbitMqSourceConfig.getUcgEventSource()}",
autoStartup = "#{rabbitMqSourceConfig.getUcgEventStartUp()}",
ackMode = "MANUAL")
public void eventConsumer(Channel channel, Message message, String content) throws IOException {
log.info(" receive dataSync msg , content is : {} ", content);
try {
DataSyncMsg dataSyncMsg = this.parseContent(content);
this.taskDeal(dataSyncMsg);
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
} catch (Exception e) {
channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
if (MapUtils.isNotEmpty(error)) {
String errorStart = this.error.get("start");
if (StringUtils.isEmpty(errorStart) || errorStart.equalsIgnoreCase("true")) {
String fileName = this.error.get("fileName")+"_"+ LocalDate.now() +".log";
String filePath = this.error.get("filePath");
String filePath1 = filePath+fileName;
FileUtil.writeStringToFile2(filePath1, content, e.getMessage());
}
}
e.printStackTrace();
}
log.info("ucEventConsumer ====>>>> end");
}
/**
* 数据解析
* @param content
* @return
*/
private DataSyncMsg parseContent(String content) {
DataSyncMsg dataSyncMsg = JSONUtil.parseMsg2Object(content,DataSyncMsg.class);
Assert.notNull(dataSyncMsg,"ERROR -->> operationConsumer -->> parseContent -->> 【dataSyncMsg】 not be null !!");
DataSyncMsg.MsgData msgData = dataSyncMsg.getMsg();
Assert.notNull(msgData,"ERROR -->> operationConsumer -->> parseContent -->> 【msgData】 not be null !!");
return dataSyncMsg;
}
/**
* 任务处理
* @param dataSyncMsg
*/
private void taskDeal(DataSyncMsg dataSyncMsg) {
this.restTemplateClient.dealTask(dataSyncMsg);
}
/**
* @description 添加收藏记录
* @param content 消息内容
*/
@RabbitHandler
@RabbitListener(queues = "#{rabbitMqSourceConfig.getUcgCollectionQueue()}",
containerFactory = "#{rabbitMqSourceConfig.getUcgCollectionSource()}",
autoStartup = "#{rabbitMqSourceConfig.getUcgCollectionStartUp()}",
ackMode = "MANUAL")
public void collectionConsumer(Channel channel, Message message, String content) throws IOException {
log.info("receive UserCollection add message, content {}", content);
try {
JSONObject jsonObject = JSON.parseObject(content, JSONObject.class);
if (Objects.nonNull(content)) {
String evt = jsonObject.get("evt").toString();
String msgData = jsonObject.get("msgData").toString();
switch (evt.toUpperCase()) {
// 添加收藏
case "ADDCOLLECTION":
this.restTemplateClient.addCollection(msgData);
break;
// 删除收藏
case "DELETECOLLECTION":
this.restTemplateClient.deleteCollection(msgData);
break;
// 删除全部收藏
case "DELETEALLCOLLECTION":
this.restTemplateClient.deleteAllCollection(msgData);
break;
default:
break;
}
}
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
} catch (Exception e) {
channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
if (MapUtils.isNotEmpty(error)) {
String errorStart = this.error.get("start");
if (errorStart.equalsIgnoreCase("true")) {
String fileName = this.error.get("fileName")+"_"+ LocalDate.now() +".log";
String filePath = this.error.get("filePath");
String filePath1 = filePath+fileName;
FileUtil.writeStringToFile2(filePath1, content, e.getMessage());
}
}
e.printStackTrace();
}
}
/**
* @description 处理观影记录
* @param content 消息内容
*/
@RabbitHandler
@RabbitListener(queues = "#{rabbitMqSourceConfig.getViewRecordQueue()}",
containerFactory = "#{rabbitMqSourceConfig.getViewRecordSource()}",
autoStartup = "#{rabbitMqSourceConfig.getViewRecordStartUp()}",
ackMode = "MANUAL")
public void viewRecordConsumer(Channel channel, Message message, String content) throws IOException {
log.info("receive ViewRecord add message, content {}", content);
try {
JSONObject jsonObject = JSON.parseObject(content, JSONObject.class);
if (Objects.nonNull(content)) {
String evt = jsonObject.get("evt").toString();
String msgData = jsonObject.get("msgData").toString();
switch (evt.toUpperCase()) {
// 添加收藏
case "VIEWRECORD":
this.restTemplateClient.dealViewRecord(msgData);
break;
default:
break;
}
}
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
} catch (Exception e) {
channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
if (MapUtils.isNotEmpty(error)) {
String errorStart = this.error.get("start");
if (errorStart.equalsIgnoreCase("true")) {
String fileName = this.error.get("fileName")+"_"+ LocalDate.now() +".log";
String filePath = this.error.get("filePath");
String filePath1 = filePath+fileName;
FileUtil.writeStringToFile2(filePath1, content, e.getMessage());
}
}
e.printStackTrace();
}
}
}