添加文件异步下载的功能

This commit is contained in:
udf_admin@163.com
2017-12-17 20:26:44 +08:00
parent bc11bea8d1
commit ca9884ab6b
7 changed files with 234 additions and 4 deletions
+5
View File
@@ -20,6 +20,11 @@
<groupId>org.itkk.udf</groupId>
<artifactId>udf-starter-core</artifactId>
</dependency>
<!-- udf-starter-cache-redis -->
<dependency>
<groupId>org.itkk.udf</groupId>
<artifactId>udf-starter-cache-redis</artifactId>
</dependency>
<!-- aliyun-sdk-oss -->
<dependency>
<groupId>com.aliyun.oss</groupId>
@@ -182,8 +182,9 @@ public class OssWarpper {
*
* @param code code
* @param file file
* @return String
*/
public void uploadFile(String code, File file) {
public String uploadFile(String code, File file) {
//判空
if (!aliyunOssProperties.getAuth().containsKey(code) || !aliyunOssProperties.getPath().containsKey(code)) {
throw new ParameterValidException("aliyun oss code:" + code + "未定义", null);
@@ -194,7 +195,9 @@ public class OssWarpper {
//实例化oss对象
OSSClient client = new OSSClient(auth.getEndPoint(), auth.getAccessId(), auth.getAccessKey());
try {
client.putObject(path.getBucketName(), file.getName(), file);
String objectKey = "upload/" + file.getName();
client.putObject(path.getBucketName(), objectKey, file);
return objectKey;
} finally {
client.shutdown();
}
@@ -0,0 +1,63 @@
/**
* BaseMqConstant.java
* Created at 2016-12-05
* Created by Administrator
* Copyright (C) 2016 egridcloud.com, All rights reserved.
*/
package org.itkk.udf.file.aliyun.oss.api.download;
/**
* 描述 : DownConstant
*
* @author Administrator
*/
public class DownConstant {
/**
* 下载状态
*/
public enum DOWNLOAD_PROCESS_STATUS {
/**
* 执行中
*/
STATUS_1(1),
/**
* 执行完成
*/
STATUS_2(2),
/**
* 执行错误
*/
STATUS_3(3);
/**
* value
*/
private Integer value;
/**
* PAY_TYPE
*
* @param value value
*/
DOWNLOAD_PROCESS_STATUS(Integer value) {
this.value = value;
}
/**
* value
*
* @return Integer
*/
public Integer value() {
return this.value;
}
}
/**
* 描述 : 私有化构造函数
*/
private DownConstant() {
}
}
@@ -0,0 +1,38 @@
/**
* Message.java
* Created at 2016-09-19
* Created by wangkang
* Copyright (C) 2016 itkk.org, All rights reserved.
*/
package org.itkk.udf.file.aliyun.oss.api.download;
import lombok.Data;
import lombok.ToString;
import java.io.Serializable;
/**
* 描述 : 下载消息
*
* @author wangkang
*/
@Data
@ToString
public class DownLoadParam<T> implements Serializable {
/**
* 描述 : id
*/
private static final long serialVersionUID = 1L;
/**
* 描述 : 消息ID
*/
private String id;
/**
* 描述 : 消息体
*/
private T body; //NOSONAR
}
@@ -0,0 +1,97 @@
/**
* RabbitmqListenerAspect.java
* Created at 2016-11-17
* Created by wangkang
* Copyright (C) 2016 itkk.org, All rights reserved.
*/
package org.itkk.udf.file.aliyun.oss.api.download;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.itkk.udf.cache.redis.CacheRedisProperties;
import org.itkk.udf.core.exception.SystemRuntimeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
/**
* 描述 : DownLoadProcessAspect
*
* @author wangkang
*/
@Aspect
@Component
@Order(101)
@Slf4j
public class DownLoadProcessAspect {
/**
* DOWNLOAD_CACHE_PREFIX
*/
public static final String DOWNLOAD_CACHE_PREFIX = ":DOWNLOAD_CACHE:";
/**
* DOWNLOAD_CACHE_EXPIRATION
*/
public static final long DOWNLOAD_CACHE_EXPIRATION = 10;
/**
* redisTemplate
*/
@Autowired
private RedisTemplate<Object, Object> redisTemplate;
/**
* cacheRedisProperties
*/
@Autowired
private CacheRedisProperties cacheRedisProperties;
/**
* around
*
* @param proceedingJoinPoint proceedingJoinPoint
* @return Object
*/
@Around("this(org.itkk.udf.file.aliyun.oss.api.download.IDownLoadProcess)")
public Object around(ProceedingJoinPoint proceedingJoinPoint) {
//定义返回值
Object result = null;
//判断类型
if (proceedingJoinPoint.getTarget() instanceof IDownLoadProcess) {
//获得参数
DownLoadParam<?> param = (DownLoadParam<?>) proceedingJoinPoint.getArgs()[0];
//获得key
String key = cacheRedisProperties.getPrefix() + DOWNLOAD_CACHE_PREFIX + param.getId();
//构造对象
DownloadInfo info = new DownloadInfo();
info.setId(param.getId());
try {
//更新缓存
info.setStatus(DownConstant.DOWNLOAD_PROCESS_STATUS.STATUS_1.value());
redisTemplate.opsForValue().set(key, info, DOWNLOAD_CACHE_EXPIRATION, TimeUnit.MINUTES);
//执行
result = proceedingJoinPoint.proceed();
//更新缓存
info.setStatus(DownConstant.DOWNLOAD_PROCESS_STATUS.STATUS_2.value());
info.setObjectKey(result.toString());
redisTemplate.opsForValue().set(key, info, DOWNLOAD_CACHE_EXPIRATION, TimeUnit.MINUTES);
} catch (Throwable e) {
//更新缓存
info.setStatus(DownConstant.DOWNLOAD_PROCESS_STATUS.STATUS_3.value());
info.setErrorMsg(e.getMessage());
redisTemplate.opsForValue().set(key, info, DOWNLOAD_CACHE_EXPIRATION, TimeUnit.MINUTES);
//抛出异常
throw new SystemRuntimeException(e);
}
}
//返回
return result;
}
}
@@ -1,4 +1,4 @@
package org.itkk.udf.core.domain.aliyun.oss;
package org.itkk.udf.file.aliyun.oss.api.download;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@@ -20,7 +20,7 @@ public class DownloadInfo {
/**
* status
*/
@ApiModelProperty(value = "下载状态 ( 1 : 待执行 , 2 : 执行中 , 3 : 执行完成 , 4 : 执行错误 )", required = true, dataType = "int")
@ApiModelProperty(value = "下载状态 ( 1 : 执行中 , 2 : 执行完成 , 3 : 执行错误 )", required = true, dataType = "int")
private Integer status;
/**
@@ -0,0 +1,24 @@
/**
* IRabbitmqListener.java
* Created at 2016-11-17
* Created by wangkang
* Copyright (C) 2016 itkk.org, All rights reserved.
*/
package org.itkk.udf.file.aliyun.oss.api.download;
/**
* 描述 : IDownLoadProcess
*
* @author wangkang
*/
public interface IDownLoadProcess<T> {
/**
* process
*
* @param param param
* @return String
*/
String process(DownLoadParam<T> param);
}