Commit 4c4d285f authored by yanzg's avatar yanzg

修改实例化关系

parent a7a18976
...@@ -3,7 +3,8 @@ package com.yanzuoguang.util.cache; ...@@ -3,7 +3,8 @@ package com.yanzuoguang.util.cache;
import com.yanzuoguang.util.helper.StringHelper; import com.yanzuoguang.util.helper.StringHelper;
import java.util.Date; import java.util.Date;
import java.util.Hashtable; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/** /**
* 内存缓存 * 内存缓存
...@@ -16,7 +17,7 @@ public class MemoryCache<T> { ...@@ -16,7 +17,7 @@ public class MemoryCache<T> {
/** /**
* 缓存的对象 * 缓存的对象
*/ */
private Hashtable<String, MemoryCacheItem<T>> cache = new Hashtable<String, MemoryCacheItem<T>>(); private Map<String, MemoryCacheItem<T>> cache = new ConcurrentHashMap<>();
/** /**
* 清除时间 * 清除时间
...@@ -231,7 +232,7 @@ public class MemoryCache<T> { ...@@ -231,7 +232,7 @@ public class MemoryCache<T> {
* @throws Throwable * @throws Throwable
*/ */
@Override @Override
protected void finalize(){ protected void finalize() {
this.close(); this.close();
} }
} }
package com.yanzuoguang.cloud.file; package com.yanzuoguang.cloud.file;
import com.yanzuoguang.util.vo.file.YzgFileMoveReqVo;
import com.yanzuoguang.util.vo.file.YzgFileVideoImageReqVo;
import com.yanzuoguang.mq.service.MqService; import com.yanzuoguang.mq.service.MqService;
import com.yanzuoguang.mq.vo.MessageVo; import com.yanzuoguang.mq.vo.MessageVo;
import com.yanzuoguang.mq.vo.QueueVo; import com.yanzuoguang.mq.vo.QueueVo;
...@@ -9,8 +7,12 @@ import com.yanzuoguang.util.cache.MemoryCache; ...@@ -9,8 +7,12 @@ import com.yanzuoguang.util.cache.MemoryCache;
import com.yanzuoguang.util.helper.DateHelper; import com.yanzuoguang.util.helper.DateHelper;
import com.yanzuoguang.util.helper.JsonHelper; import com.yanzuoguang.util.helper.JsonHelper;
import com.yanzuoguang.util.helper.StringHelper; import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.vo.file.YzgFileMoveReqVo;
import com.yanzuoguang.util.vo.file.YzgFileVideoImageReqVo;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.*;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.Date; import java.util.Date;
...@@ -21,14 +23,13 @@ import java.util.Date; ...@@ -21,14 +23,13 @@ import java.util.Date;
* @author 颜佐光 * @author 颜佐光
*/ */
@Component @Component
public class YzgFileProcedure implements InitializingBean { public class YzgFileProcedure implements InitializingBean, ApplicationContextAware {
public static final String YZG_REMOVE_TEMP_FOLDER = "YZG_REMOVE_TEMP_FOLDER"; public static final String YZG_REMOVE_TEMP_FOLDER = "YZG_REMOVE_TEMP_FOLDER";
public static final String YZG_MOVE_FILE = "YZG_MOVE_FILE"; public static final String YZG_MOVE_FILE = "YZG_MOVE_FILE";
public static final String YZG_CONVERT_IMAGE = "YZG_CONVERT_IMAGE"; public static final String YZG_CONVERT_IMAGE = "YZG_CONVERT_IMAGE";
public static final String YZG_CONVERT_VIDEO = "YZG_CONVERT_VIDEO"; public static final String YZG_CONVERT_VIDEO = "YZG_CONVERT_VIDEO";
@Autowired
private MqService mqService; private MqService mqService;
/** /**
...@@ -36,6 +37,25 @@ public class YzgFileProcedure implements InitializingBean { ...@@ -36,6 +37,25 @@ public class YzgFileProcedure implements InitializingBean {
*/ */
private MemoryCache<Boolean> folderCache = new MemoryCache<>(24 * 60 * 60); private MemoryCache<Boolean> folderCache = new MemoryCache<>(24 * 60 * 60);
/**
* Set the ApplicationContext that this object runs in.
* Normally this call will be used to initialize the object.
* <p>Invoked after population of normal bean properties but before an init callback such
* as {@link InitializingBean#afterPropertiesSet()}
* or a custom init-method. Invoked after {@link ResourceLoaderAware#setResourceLoader},
* {@link ApplicationEventPublisherAware#setApplicationEventPublisher} and
* {@link MessageSourceAware}, if applicable.
*
* @param applicationContext the ApplicationContext object to be used by this object
* @throws ApplicationContextException in case of context initialization errors
* @throws BeansException if thrown by application context methods
* @see BeanInitializationException
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
mqService = applicationContext.getBean(MqService.class);
}
/** /**
* Invoked by a BeanFactory after it has set all bean properties supplied * Invoked by a BeanFactory after it has set all bean properties supplied
* (and satisfied BeanFactoryAware and ApplicationContextAware). * (and satisfied BeanFactoryAware and ApplicationContextAware).
...@@ -133,5 +153,4 @@ public class YzgFileProcedure implements InitializingBean { ...@@ -133,5 +153,4 @@ public class YzgFileProcedure implements InitializingBean {
public void convertVideo(String json, int dedTime) { public void convertVideo(String json, int dedTime) {
mqService.message(new MessageVo(YZG_CONVERT_VIDEO, json, dedTime)); mqService.message(new MessageVo(YZG_CONVERT_VIDEO, json, dedTime));
} }
} }
package com.yanzuoguang.cloud.file; package com.yanzuoguang.cloud.file;
import com.rabbitmq.client.Channel; import com.rabbitmq.client.Channel;
import com.yanzuoguang.util.vo.file.YzgFileMoveReqVo;
import com.yanzuoguang.util.vo.file.YzgFileVideoImageItemReqVo;
import com.yanzuoguang.util.vo.file.YzgFileVideoImageReqVo;
import com.yanzuoguang.mq.service.MqService; import com.yanzuoguang.mq.service.MqService;
import com.yanzuoguang.util.exception.CodeException; import com.yanzuoguang.util.exception.CodeException;
import com.yanzuoguang.util.helper.JsonHelper; import com.yanzuoguang.util.helper.JsonHelper;
import com.yanzuoguang.util.log.Log; 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.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.*;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -21,17 +23,35 @@ import java.util.List; ...@@ -21,17 +23,35 @@ import java.util.List;
* 文件处理 * 文件处理
*/ */
@Component @Component
public class YzgFileConsumer implements InitializingBean { public class YzgFileConsumer implements InitializingBean, ApplicationContextAware {
@Autowired
private MqService mqService; private MqService mqService;
@Autowired
private YzgFileService fileService; private YzgFileService fileService;
@Autowired
private YzgFileProcedure fileProcedure; private YzgFileProcedure fileProcedure;
/**
* Set the ApplicationContext that this object runs in.
* Normally this call will be used to initialize the object.
* <p>Invoked after population of normal bean properties but before an init callback such
* as {@link InitializingBean#afterPropertiesSet()}
* or a custom init-method. Invoked after {@link ResourceLoaderAware#setResourceLoader},
* {@link ApplicationEventPublisherAware#setApplicationEventPublisher} and
* {@link MessageSourceAware}, if applicable.
*
* @param applicationContext the ApplicationContext object to be used by this object
* @throws ApplicationContextException in case of context initialization errors
* @throws BeansException if thrown by application context methods
* @see BeanInitializationException
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
mqService = applicationContext.getBean(MqService.class);
fileService = applicationContext.getBean(YzgFileService.class);
fileProcedure = applicationContext.getBean(YzgFileProcedure.class);
}
/** /**
* Invoked by a BeanFactory after it has set all bean properties supplied * Invoked by a BeanFactory after it has set all bean properties supplied
* (and satisfied BeanFactoryAware and ApplicationContextAware). * (and satisfied BeanFactoryAware and ApplicationContextAware).
......
package com.yanzuoguang.cloud.file; package com.yanzuoguang.cloud.file;
import com.yanzuoguang.cloud.CloudConfig;
import com.yanzuoguang.cloud.helper.HttpFileHelper; import com.yanzuoguang.cloud.helper.HttpFileHelper;
import com.yanzuoguang.cloud.vo.YzgFileUploadReqVo; import com.yanzuoguang.cloud.vo.YzgFileUploadReqVo;
import com.yanzuoguang.util.MediaHelper; import com.yanzuoguang.util.MediaHelper;
...@@ -10,7 +11,10 @@ import com.yanzuoguang.util.helper.DateHelper; ...@@ -10,7 +11,10 @@ import com.yanzuoguang.util.helper.DateHelper;
import com.yanzuoguang.util.helper.FileHelper; import com.yanzuoguang.util.helper.FileHelper;
import com.yanzuoguang.util.helper.StringHelper; import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.vo.file.*; import com.yanzuoguang.util.vo.file.*;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.*;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
...@@ -32,16 +36,37 @@ import java.util.List; ...@@ -32,16 +36,37 @@ import java.util.List;
* @author 颜佐光 * @author 颜佐光
*/ */
@Component @Component
public class YzgFileServiceImpl implements YzgFileService { public class YzgFileServiceImpl implements YzgFileService, ApplicationContextAware {
private CloudConfig cloudConfig;
@Autowired
private YzgFileConfig fileConfig; private YzgFileConfig fileConfig;
@Autowired
private YzgFileProcedure procedure; private YzgFileProcedure procedure;
private MemoryCache<Boolean> cacheRemoveTempFolder = new MemoryCache<>(10 * 60); private MemoryCache<Boolean> cacheRemoveTempFolder = new MemoryCache<>(10 * 60);
/**
* Set the ApplicationContext that this object runs in.
* Normally this call will be used to initialize the object.
* <p>Invoked after population of normal bean properties but before an init callback such
* as {@link InitializingBean#afterPropertiesSet()}
* or a custom init-method. Invoked after {@link ResourceLoaderAware#setResourceLoader},
* {@link ApplicationEventPublisherAware#setApplicationEventPublisher} and
* {@link MessageSourceAware}, if applicable.
*
* @param applicationContext the ApplicationContext object to be used by this object
* @throws ApplicationContextException in case of context initialization errors
* @throws BeansException if thrown by application context methods
* @see BeanInitializationException
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
cloudConfig = applicationContext.getBean(CloudConfig.class);
fileConfig = applicationContext.getBean(YzgFileConfig.class);
procedure = applicationContext.getBean(YzgFileProcedure.class);
}
/** /**
* 压缩文件 * 压缩文件
* *
...@@ -64,7 +89,7 @@ public class YzgFileServiceImpl implements YzgFileService { ...@@ -64,7 +89,7 @@ public class YzgFileServiceImpl implements YzgFileService {
// 获取临时文件路径 // 获取临时文件路径
String tempFolder = getTempFolder(folder); String tempFolder = getTempFolder(folder);
String serverFolder = String.format("%s/%s", fileConfig.getServerUrl(), tempFolder); String serverFolder = String.format("%s/%s", cloudConfig.getServerUrl(), tempFolder);
// 创建服务器路径 // 创建服务器路径
FileHelper.createDirectory(serverFolder); FileHelper.createDirectory(serverFolder);
// 删除目录 // 删除目录
......
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