概括
这章内容主要是截止到项目视频p17,以下主要提供这几章用用到的一些枚举变量和工具方法。
枚举变量
VideoFileTransferResultEnum
,代码如下:
public enum VideoFileTransferResultEnum {TRANSFER(1,"转码中"),SUCCESS(1,"转码成功"),FAIL(1,"转码失败");private Integer status;private String desc;VideoFileTransferResultEnum(Integer status,String desc){this.status = status;this.desc = desc;}public Integer getStatus() {return status;}public String getDesc() {return desc;}
}
VideoFileUpdateTypeEnum
,代码如下:
public enum VideoFileUpdateTypeEnum {NO_UPDATE(0,"无更新"),UPDATE(1,"有更新");private Integer status;private String desc;VideoFileUpdateTypeEnum(Integer status,String desc){this.status = status;this.desc = desc;}public Integer getStatus() {return status;}public String getDesc() {return desc;}
}
VideoStatusEnum
,代码如下:
public enum VideoStatusEnum {STATUS0(0,"转码中"),STATUS1(1,"转码失败"),STATUS2(2,"待审核"),STATUS3(3,"审核成功"),STATUS4(4,"审核不通关");private Integer status;private String desc;VideoStatusEnum(Integer status, String desc) {this.status = status;this.desc = desc;}public Integer getStatus() {return status;}public String getDesc() {return desc;}public static VideoStatusEnum getEnumByStatus(Integer status) {for (VideoStatusEnum videoStatusEnum : VideoStatusEnum.values()) {if (videoStatusEnum.getStatus().equals(status)) {return videoStatusEnum;}}return null;}
}
工具类
RedisComponent
,代码添加如下:
主要是增加了将上传信息添加到消息队列中,然后设置一个消费者不停的去消费信息,从而达到异步文件上传和文件删除的效果。
因为如果说删除的文件量很大,并且请求量高的情况下,会造成速度的缺失,这个时候只需要先将数据库中的数据进行删除,之后再用消费者去获取需要删除的视频的文件地址慢慢去删除即可。
public void delVideoFileInfo(String userId,String uploadId){redisUtils.delete(Constants.REDIS_KEY_UPLOADING_FILE + userId + uploadId);}public void addFile2DelQueue(String videoId, List<String> deleteFileList) {redisUtils.lpushAll(Constants.REDIS_KEY_FILE_DEL + videoId , deleteFileList,Constants.REDIS_KEY_EXPIRE_ONE_DAY * 7);}public void addFile2TransferQueue(List<VideoInfoFilePost> addFileList) {redisUtils.lpushAll(Constants.REDIS_KEY_QUEUE_TRANSFER , addFileList,0);}public VideoInfoFilePost getFileFromTransferQueue() {return (VideoInfoFilePost) redisUtils.rpop(Constants.REDIS_KEY_QUEUE_TRANSFER);}
ExecuteQueueTask
,代码添加如下:
private ExecutorService executorService = Executors.newFixedThreadPool(Constants.LENGTH_2);@Resourceprivate RedisComponent redisComponent;@Resourceprivate VideoInfoPostService videoInfoPostService;@PostConstructpublic void consumTransferFileQueue() {executorService.execute(() -> {while (true) {try{VideoInfoFilePost videoInfoFile = redisComponent.getFileFromTransferQueue();if (videoInfoFile ==null){Thread.sleep(1500);continue;}videoInfoPostService.transferVideoFile(videoInfoFile);}catch (Exception e){log.error("获取转码文件队列信息失败",e);}}});}