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
package com.yanzuoguang.cloud.file;
import com.rabbitmq.client.Channel;
import com.yanzuoguang.cloud.vo.req.YzgFileCreateReqVo;
import com.yanzuoguang.cloud.vo.req.YzgFileStatusReqVo;
import com.yanzuoguang.mq.service.MqService;
import com.yanzuoguang.util.exception.CodeException;
import com.yanzuoguang.util.helper.JsonHelper;
import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.log.Log;
import com.yanzuoguang.util.vo.file.YzgFileMoveReqVo;
import com.yanzuoguang.util.vo.file.YzgFileVideoImageItemReqVo;
import com.yanzuoguang.util.vo.file.YzgFileVideoImageReqVo;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
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;
/**
* 文件处理
*/
@Component
public class YzgFileConsumer implements InitializingBean {
@Autowired
private MqService mqService;
@Autowired
private YzgFileService fileService;
@Autowired
private YzgFileProcedure fileProcedure;
@Autowired
private YzgExcelService excelService;
/**
* 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 {
}
/**
* 删除临时木经
*
* @param tempFolder
* @param message
* @param channel
*/
@RabbitListener(queues = {YzgFileProcedure.YZG_REMOVE_TEMP_FOLDER})
public void notesCheckBack(String tempFolder, Message message, Channel channel) {
try {
fileService.removeTempFolder(tempFolder);
} catch (CodeException ex) {
Log.error(YzgFileConsumer.class, ex);
} catch (Exception ex) {
Log.error(YzgFileConsumer.class, ex);
fileProcedure.removeTempFolder(tempFolder);
} finally {
mqService.basicAck(message, channel);
}
}
/**
* 移动临时目录
*
* @param json
* @param message
* @param channel
*/
@RabbitListener(queues = {YzgFileProcedure.YZG_MOVE_FILE})
public void moveFile(String json, Message message, Channel channel) {
try {
YzgFileMoveReqVo req = JsonHelper.deserialize(json, YzgFileMoveReqVo.class);
fileService.moveFile(req);
} catch (CodeException ex) {
Log.error(YzgFileConsumer.class, ex);
} catch (Exception ex) {
Log.error(YzgFileConsumer.class, ex);
fileProcedure.moveFile(json, 60 * 1000);
} finally {
mqService.basicAck(message, channel);
}
}
/**
* 处理文件转换
*
* @param json
* @param message
* @param channel
*/
@RabbitListener(queues = {YzgFileProcedure.YZG_CONVERT_IMAGE})
public void convertImage(String json, Message message, Channel channel) {
try {
YzgFileVideoImageReqVo req = JsonHelper.deserialize(json, YzgFileVideoImageReqVo.class);
// 等待下一次MQ执行
List<YzgFileVideoImageItemReqVo> cacheList = req.getList();
if (cacheList == null || cacheList.isEmpty()) {
return;
}
// 本次执行的转换操作
List<YzgFileVideoImageItemReqVo> runList = new ArrayList<>();
runList.add(cacheList.remove(0));
req.setList(runList);
// 执行本次转换
fileService.convertImage(req);
// 继续执行下一次转换
if (!cacheList.isEmpty()) {
req.setList(cacheList);
fileProcedure.convertImage(req);
}
} catch (CodeException ex) {
Log.error(YzgFileConsumer.class, ex);
} catch (Exception ex) {
Log.error(YzgFileConsumer.class, ex);
fileProcedure.convertImage(json, 60 * 1000);
} finally {
mqService.basicAck(message, channel);
}
}
/**
* 处理视频转换
*
* @param json
* @param message
* @param channel
*/
@RabbitListener(queues = {YzgFileProcedure.YZG_CONVERT_VIDEO})
public void convertVideo(String json, Message message, Channel channel) {
try {
YzgFileVideoImageReqVo req = JsonHelper.deserialize(json, YzgFileVideoImageReqVo.class);
// 等待下一次MQ执行
List<YzgFileVideoImageItemReqVo> cacheList = req.getList();
if (cacheList == null || cacheList.isEmpty()) {
return;
}
// 本次执行的转换操作
List<YzgFileVideoImageItemReqVo> runList = new ArrayList<>();
runList.add(cacheList.remove(0));
req.setList(runList);
// 执行本次转换
fileService.convertVideo(req);
// 继续执行下一次转换
if (!cacheList.isEmpty()) {
req.setList(cacheList);
fileProcedure.convertVideo(req);
}
} catch (CodeException ex) {
Log.error(YzgFileConsumer.class, ex);
} catch (Exception ex) {
Log.error(YzgFileConsumer.class, ex);
fileProcedure.convertVideo(json, 60 * 1000);
} finally {
mqService.basicAck(message, channel);
}
}
/**
* 处理视频转换
*
* @param json
* @param message
* @param channel
*/
@RabbitListener(queues = {YzgFileProcedure.YZG_FILE_CREATE})
public void fileCreate(String json, Message message, Channel channel) {
YzgFileCreateReqVo fileReq = null;
try {
fileReq = JsonHelper.deserialize(json, YzgFileCreateReqVo.class);
if (StringHelper.isEmpty(fileReq.getFileId(), fileReq.getCallbackMQ())) {
return;
}
excelService.fileCreate(json, fileReq);
} catch (CodeException ex) {
Log.error(YzgFileConsumer.class, ex);
} catch (Exception ex) {
Log.error(YzgFileConsumer.class, ex);
fileProcedure.fileCreateDelay(json);
} finally {
mqService.basicAck(message, channel);
}
}
/**
* 处理视频转换
*
* @param json
* @param message
* @param channel
*/
@RabbitListener(queues = {YzgFileProcedure.YZG_FILE_STATUS})
public void fileStatus(String json, Message message, Channel channel) {
try {
YzgFileStatusReqVo fileReq = JsonHelper.deserialize(json, YzgFileStatusReqVo.class);
if (StringHelper.isEmpty(fileReq.getFileId(), fileReq.getFileStatus())) {
return;
}
excelService.updateStatus(fileReq);
} catch (CodeException ex) {
Log.error(YzgFileConsumer.class, ex);
} catch (Exception ex) {
Log.error(YzgFileConsumer.class, ex);
fileProcedure.fileStatus(json);
} finally {
mqService.basicAck(message, channel);
}
}
}