VideoUtil.java
2.99 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
package com.viontech.utils;
import com.viontech.ftp.FTPClientHelper;
import lombok.extern.slf4j.Slf4j;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* .
*
* @author 谢明辉
* @date 2020/9/25
*/
public class VideoUtil {
private final static ConcurrentHashMap<String, byte[]> VIDEO_MAP = new ConcurrentHashMap<>();
private final static ConcurrentHashMap<String, Long> VIDEO_EXPIRE_MAP = new ConcurrentHashMap<>();
private final static ThreadPoolExecutor POOL = new ThreadPoolExecutor(20, 30, 1, TimeUnit.HOURS, new LinkedBlockingQueue<>(), new ThreadPoolExecutor.DiscardPolicy());
public synchronized static void addVideo(String refId, byte[] fileBytes) {
VIDEO_MAP.put(refId, fileBytes);
VIDEO_EXPIRE_MAP.put(refId, System.currentTimeMillis());
}
public synchronized static byte[] getVideo(String refId) {
return VIDEO_MAP.remove(refId);
}
public synchronized static void clean() {
for (Map.Entry<String, Long> entry : VIDEO_EXPIRE_MAP.entrySet()) {
if ((System.currentTimeMillis() - entry.getValue()) > TimeUnit.HOURS.toMillis(3)) {
getVideo(entry.getKey());
}
}
}
public static void submitTask(String refId, String filePath, FTPClientHelper ftpClientHelper) {
POOL.submit(new VideoPushRunnable(refId, filePath, ftpClientHelper));
}
@Slf4j
private static class VideoPushRunnable implements Runnable {
private final String refId;
private final String filePath;
private final FTPClientHelper ftpClientHelper;
public VideoPushRunnable(String refId, String filePath, FTPClientHelper ftpClientHelper) {
this.refId = refId;
this.filePath = filePath;
this.ftpClientHelper = ftpClientHelper;
}
@Override
public void run() {
try {
long begin = System.currentTimeMillis();
while (System.currentTimeMillis() - begin < TimeUnit.MINUTES.toMillis(5)) {
byte[] video = VideoUtil.getVideo(refId);
if (video != null) {
ftpClientHelper.storeFile(filePath, video);
log.info("视频推送成功,refId:[{}],path:[{}]", refId, filePath);
return;
}
Long aLong = VideoUtil.VIDEO_EXPIRE_MAP.get(refId);
if (aLong != null) {
log.info("视频已发送,refId:[{}],path:[{}]", refId, filePath);
return;
}
TimeUnit.SECONDS.sleep(10);
log.info("视频未找到:[{}]", refId);
}
} catch (Exception e) {
log.error("推送视频出错,refId:[" + refId + "],path:[" + filePath + "]", e);
}
}
}
}