一、簡介及配置
- 項目環境
使用dubbo分佈式結構,創建一個定時任務的子工程 - 配置文件
spring-redis.xml文件
redis沒有密碼的配置,如果有密碼,在最後的bean中加入<constructor-arg name="password" value="12345"/>
spring-jobs.xml 定時任務配置<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!--jedis 連接池的相關配置--> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal"> <value>200</value> </property> <property name="maxIdle"> <value>50</value> </property> <property name="testOnBorrow" value="true"/> <property name="testOnReturn" value="true"/> </bean> <bean id="jedisPool" class="redis.clients.jedis.JedisPool"> <constructor-arg name="poolConfig" ref="jedisPoolConfig"/> <constructor-arg name="host" value="127.0.0.1"/> <constructor-arg name="port" value="6379" type="int"/> <constructor-arg name="timeout" value="30000" type="int"/> </bean> </beans>
裏面的配置需要自定義一個類和方法,詳請見下
pom.xml依賴<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!--開啓spring註解使用--> <context:annotation-config></context:annotation-config> <!--註冊自定義job--> <bean id="clearImgQuartz" class="com.sys.jobs.ClearImgQuartz"/> <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!--注入對象--> <property name="targetObject" ref="clearImgQuartz"/> <!--注入方法--> <property name="targetMethod" value="clearImg"/> </bean> <!--觸發器 指定時間--> <bean id="triggerFactoryBean" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <!--注入jobDetail--> <property name="jobDetail" ref="jobDetail"/> <!--觸發時間,基於cron表達式 ,每週一上午10點15分執行任務--> <property name="cronExpression" value="0 15 10 ? * MON"/> </bean> <!--調度工廠--> <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <!--注入觸發器--> <property name="triggers"> <list> <ref bean="triggerFactoryBean"/> </list> </property> </bean> </beans>
web.xml配置<dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> </dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz-jobs</artifactId> </dependency>
讀取spring資源文件<!-- 加載spring容器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:spring*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
二、定時任務類
-
ClearImgQuartz 類
/** * 定時清理七牛雲垃圾圖片 */ @Slf4j public class ClearImgQuartz { //根據redis清除七牛圖片 @Autowired private JedisPool jedisPool; /** * 刪除垃圾圖片 */ public void clearImg(){ //根據redis中保存的兩個set集合進行差值計算 //得到垃圾圖片的集合,然後刪除 Set<String> set = jedisPool.getResource(). sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES); log.info("垃圾圖片集合:"+set); if(set != null){ for (String picName : set){ //根據圖片名字刪除 QiNiuYun.deleteImg(picName); //刪除redis集合中的圖片名稱 jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES,picName); log.info("圖片刪除成功,刪除的圖片名稱是:"+picName); } } } }
-
QiNiuYun工具類
對圖片文件進行上傳及刪除/** * 七牛雲-圖片上傳工具類 * @author hp */ public class QiNiuYun { //1.定義靜態參數 public static String accessKey = "你的AK碼"; public static String secretKey = "你的SK碼"; public static String bucket = "空間名稱"; /* //文件上傳----測試 public static void uploadToQiNiuYun(String filePath, String fileName) { //構造一個帶指定 Region 對象的配置類 Configuration cfg = new Configuration(Zone.autoZone()); UploadManager uploadManager = new UploadManager(cfg); Auth auth = Auth.create(accessKey, secretKey); String upToken = auth.uploadToken(bucket); try { Response response = uploadManager.put(filePath, fileName, upToken); //解析上傳成功的結果 DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class); System.out.println(putRet.key); System.out.println(putRet.hash); } catch (QiniuException ex) { Response r = ex.response; System.err.println(r.toString()); try { System.err.println(r.bodyString()); } catch (QiniuException ex2) { //ignore } } }*/ //字節數組上傳 public static void uploadToQiNiuYun(byte[] bytes, String fileName) { //構造一個帶指定 Region 對象的配置類 Configuration cfg = new Configuration(Zone.autoZone()); UploadManager uploadManager = new UploadManager(cfg); Auth auth = Auth.create(accessKey, secretKey); String upToken = auth.uploadToken(bucket); String key = fileName; try { Response response = uploadManager.put(bytes, key, upToken); //解析上傳成功的結果 DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class); System.out.println(putRet.key); System.out.println(putRet.hash); } catch (QiniuException ex) { Response r = ex.response; System.err.println(r.toString()); try { System.err.println(r.bodyString()); } catch (QiniuException ex2) { //ignore } } } /** * 刪除圖片 * @param fileName */ public static void deleteImg(String fileName){ Configuration cfg = new Configuration(Zone.zone2()); Auth auth = Auth.create(accessKey, secretKey); String key = fileName; BucketManager bucketManager = new BucketManager(auth, cfg); try { bucketManager.delete(bucket, key); } catch (QiniuException ex) { //如果遇到異常,說明刪除失敗 System.err.println(ex.code()); System.err.println(ex.response.toString()); } } }
-
RedisConstant類
存放redis常量參數/** * redis數據庫常量參數 * @author hp */ public class RedisConstant { //套餐的所有圖片名稱 public static final String SETMEAL_PIC_RESOURCES = "setmealPicResources"; //套餐圖片保存到數據庫的所有圖片名稱 public static final String SETMEAL_PIC_DB_RESOURCES = "setmealPicDbResources"; }
三、向redis數據庫添加兩個set集合
- 在圖片上傳時,向數據庫添加數據
@Autowired private JedisPool jedisPool; /** * 圖片文件上傳 * @param imgFile * @return */ @RequestMapping("/upload") public Result upload(@RequestParam("imgFile")MultipartFile imgFile){ log.info("imgFile:"+ imgFile); //原始文件名 String fileName = imgFile.getOriginalFilename(); //找到xxx.jpg的點 int index = fileName.lastIndexOf("."); //獲取.jpg String extention = fileName.substring(index - 1); //使用UUID修改原始名字 String newFileName = UUID.randomUUID().toString() + extention; log.info("修改後的文件名稱:" + newFileName); //圖片上傳到七牛雲 try { QiNiuYun.uploadToQiNiuYun(imgFile.getBytes(),newFileName); log.info("上傳成功,圖片放入redis中------"); //TODO 在圖片上傳時,把圖片名稱放入redis數據庫中存儲 jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,newFileName); } catch (IOException e) { e.printStackTrace(); return new Result(false,"圖片上傳失敗!"); } return new Result(true,"圖片上傳成功!",newFileName); }
- 在添加數據時,向數據庫添加數據
@Autowired private JedisPool jedisPool; /** * 添加setmeal對象和中間表數據 * @param setmeal * @param itemgroupIds */ @Override public void add(Setmeal setmeal, Integer[] itemgroupIds) { setmealDao.insert(setmeal); Integer setmealId = setmeal.getId(); setMealAndItemGroup(setmealId,itemgroupIds); //TODO 在執行添加操作時,把圖片名稱存入redis數據庫中 jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg()); }