Commit 1fddc0e2 authored by yanzg's avatar yanzg

修改实例化关系

parent 11c2dcd0
package com.yanzuoguang.media;
import java.util.HashMap;
import java.util.Map;
/**
* 本地缓存实现
*
* @author 颜佐光
*/
public class MediaCacheLocal implements MediaCacheBase {
private Map<String, MapLock> mapLock = new HashMap<String, MapLock>();
/**
* 获取缓存是否正在执行
*
......@@ -38,4 +44,34 @@ public class MediaCacheLocal implements MediaCacheBase {
public void sub(MediaResVo res) {
}
/**
* 锁定临时文件,防止被人修改
*
* @param req
* @param runnable
*/
@Override
public void lockTempFile(MediaReqVo req, Runnable runnable) {
MapLock lock = getMapLock(req);
synchronized (lock.lockTemp) {
runnable.run();
}
}
private MapLock getMapLock(MediaReqVo req) {
synchronized (mapLock) {
MapLock lock = this.mapLock.get(req.getUrl());
if (lock == null) {
lock = new MapLock();
this.mapLock.put(req.getUrl(), lock);
}
return lock;
}
}
private static class MapLock {
Object lockTemp = new Object();
MediaResVo res;
}
}
......@@ -9,6 +9,11 @@ import org.springframework.stereotype.Component;
import java.io.File;
/**
* 直播首页操作
*
* @author 颜佐光
*/
@Component
public class MediaFirst {
......@@ -53,7 +58,7 @@ public class MediaFirst {
});
// 通过判断,当没有开启线程运行时,则开启线程运行
MediaResVo res = this.getCache().start(req, new Runnable() {
return this.getCache().start(req, new Runnable() {
@Override
public void run() {
// 开始开启线程运行
......@@ -65,7 +70,6 @@ public class MediaFirst {
});
}
});
return res;
}
private File getFile(MediaReqVo req) {
......@@ -89,11 +93,10 @@ public class MediaFirst {
MediaResVo res = cache.get(req);
if (res == null) {
res = JsonHelper.to(req, MediaResVo.class);
} else if (res.getCount() < 1) {
break;
}
// 下载m3mu并转换成mp4
downM3mu(req);
// 转换为图片
catImage(req);
// 将下载临时文件移动到临时文件
cache.lockTempFile(req, new Runnable() {
......@@ -108,6 +111,11 @@ public class MediaFirst {
});
// 写入缓存
cache.sub(res);
// 判断是否还有执行次数
if (res.getCount() < 1) {
break;
}
// 等待下一次截图
ThreadHelper.sleep(res.getSplit());
} while (true);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment