|  | @ -1,14 +1,15 @@
 | 
												
													
														
															|  | package com.yihu.quota.service.job;
 |  | package com.yihu.quota.service.job;
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  | import com.yihu.quota.contants.JobConstant;
 | 
												
													
														
															|  | import com.yihu.quota.dao.TjQuotaDao;
 |  | import com.yihu.quota.dao.TjQuotaDao;
 | 
												
													
														
															|  | import com.yihu.quota.model.TjQuota;
 |  | import com.yihu.quota.model.TjQuota;
 | 
												
													
														
															|  | import com.yihu.quota.util.QuartzHelper;
 |  | import com.yihu.quota.util.QuartzHelper;
 | 
												
													
														
															|  | import com.yihu.quota.vo.QuotaVo;
 |  | import com.yihu.quota.vo.QuotaVo;
 | 
												
													
														
															|  | 
 |  | import org.apache.commons.lang3.StringUtils;
 | 
												
													
														
															|  | import org.quartz.ObjectAlreadyExistsException;
 |  | import org.quartz.ObjectAlreadyExistsException;
 | 
												
													
														
															|  | import org.springframework.beans.BeanUtils;
 |  | import org.springframework.beans.BeanUtils;
 | 
												
													
														
															|  | import org.springframework.beans.factory.annotation.Autowired;
 |  | import org.springframework.beans.factory.annotation.Autowired;
 | 
												
													
														
															|  | import org.springframework.stereotype.Service;
 |  | import org.springframework.stereotype.Service;
 | 
												
													
														
															|  | import org.springframework.util.StringUtils;
 |  | 
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | import java.util.HashMap;
 |  | import java.util.HashMap;
 | 
												
													
														
															|  | import java.util.Map;
 |  | import java.util.Map;
 | 
												
											
												
													
														
															|  | @ -18,7 +19,6 @@ import java.util.Map;
 | 
												
													
														
															|  |  */
 |  |  */
 | 
												
													
														
															|  | @Service
 |  | @Service
 | 
												
													
														
															|  | public class JobService {
 |  | public class JobService {
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |     @Autowired
 |  |     @Autowired
 | 
												
													
														
															|  |     private QuartzHelper quartzHelper;
 |  |     private QuartzHelper quartzHelper;
 | 
												
													
														
															|  |     @Autowired
 |  |     @Autowired
 | 
												
											
												
													
														
															|  | @ -26,49 +26,55 @@ public class JobService {
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |     /**
 |  |     /**
 | 
												
													
														
															|  |      * 启动指标任务
 |  |      * 启动指标任务
 | 
												
													
														
															|  | 
 |  |      * 启动任务只关心任务是否手动(马上)执行还是任务执行(Cron),对于数据是否初始化(全量),或者增量,这个是具体Job判断的事情。
 | 
												
													
														
															|  | 
 |  |      * 任务只是执行时间有区别,并且任务不可能同时存在立即执行和周期执行两种行为,不需要增加任务名称来增加复杂度
 | 
												
													
														
															|  |      *
 |  |      *
 | 
												
													
														
															|  |      * @param id          指标ID
 |  |      * @param id          指标ID
 | 
												
													
														
															|  |      * @param executeFlag 执行动作标识,1:初始执行(全量统计),2:立即执行、周期执行(增量统计)
 |  | 
 | 
												
													
														
															|  | 
 |  |      * @param executeFlag 执行动作标识,0:全量执行,1:增量执行。
 | 
												
													
														
															|  |      * @param startDate   抽取数据起始日期。初始执行时为NULL;立即执行时需要传值;周期执行时也为NULL,如果是基础指标周期执行,后续默认赋值昨天开始。
 |  |      * @param startDate   抽取数据起始日期。初始执行时为NULL;立即执行时需要传值;周期执行时也为NULL,如果是基础指标周期执行,后续默认赋值昨天开始。
 | 
												
													
														
															|  |      * @param endDate     抽取数据截止日期。初始执行时无NULL;立即执行时需要传值;周期执行时也为NULL,如果是基础指标周期执行,后续默认赋值昨天截止。
 |  |      * @param endDate     抽取数据截止日期。初始执行时无NULL;立即执行时需要传值;周期执行时也为NULL,如果是基础指标周期执行,后续默认赋值昨天截止。
 | 
												
													
														
															|  |      * @throws Exception
 |  |      * @throws Exception
 | 
												
													
														
															|  |      */
 |  |      */
 | 
												
													
														
															|  |     public void executeJob(Integer id, String executeFlag, String startDate, String endDate) throws Exception {
 |  | 
 | 
												
													
														
															|  | 
 |  |     public void execute(Integer id, String executeFlag, String startDate, String endDate) throws Exception {
 | 
												
													
														
															|  |         TjQuota tjQuota = quotaDao.findOne(id);
 |  |         TjQuota tjQuota = quotaDao.findOne(id);
 | 
												
													
														
															|  |         if (tjQuota != null) {
 |  | 
 | 
												
													
														
															|  |             QuotaVo quotaVo = new QuotaVo();
 |  | 
 | 
												
													
														
															|  |             BeanUtils.copyProperties(tjQuota, quotaVo);
 |  | 
 | 
												
													
														
															|  |             Map<String, Object> params = new HashMap<>();
 |  | 
 | 
												
													
														
															|  |             params.put("quota", quotaVo);
 |  | 
 | 
												
													
														
															|  |             params.put("executeFlag", executeFlag);
 |  | 
 | 
												
													
														
															|  |             params.put("startTime", startDate);
 |  | 
 | 
												
													
														
															|  |             params.put("endTime", endDate);
 |  | 
 | 
												
													
														
															|  |             String quotaCode = quotaVo.getCode().replace("_", "");
 |  | 
 | 
												
													
														
															|  |             String quotaCodeImmediately = quotaCode + "immediately";
 |  | 
 | 
												
													
														
															|  |             boolean existJob = quartzHelper.isExistJob(quotaCode);
 |  | 
 | 
												
													
														
															|  |             boolean existJobImmediately = quartzHelper.isExistJob(quotaCodeImmediately);
 |  | 
 | 
												
													
														
															|  |             if (existJob && "0".equals(quotaVo.getJobStatus())) {
 |  | 
 | 
												
													
														
															|  |                 //周期执行jobKey
 |  | 
 | 
												
													
														
															|  |                 quartzHelper.removeJob(quotaCode);
 |  | 
 | 
												
													
														
															|  |             }
 |  | 
 | 
												
													
														
															|  |             if (existJobImmediately) {
 |  | 
 | 
												
													
														
															|  |                 //立即执行jobKey
 |  | 
 | 
												
													
														
															|  |                 quartzHelper.removeJob(quotaCodeImmediately);
 |  | 
 | 
												
													
														
															|  |             }
 |  | 
 | 
												
													
														
															|  |             //往quartz框架添加任务
 |  | 
 | 
												
													
														
															|  |             if ((!StringUtils.isEmpty(executeFlag) && executeFlag.equals("1")) || // 初始执行
 |  | 
 | 
												
													
														
															|  |                     (!StringUtils.isEmpty(tjQuota.getJobClazz()) && tjQuota.getExecType().equals("1"))) { // 立即执行
 |  | 
 | 
												
													
														
															|  |                 try {
 |  | 
 | 
												
													
														
															|  |                     quartzHelper.startNow(Class.forName(quotaVo.getJobClazz()), quotaCodeImmediately, params);
 |  | 
 | 
												
													
														
															|  |                 } catch (Exception e) {
 |  | 
 | 
												
													
														
															|  |                     throw new ObjectAlreadyExistsException(quotaCodeImmediately + "," + tjQuota.getName() + "指标正在执行!");
 |  | 
 | 
												
													
														
															|  |                 }
 |  | 
 | 
												
													
														
															|  |             } else {
 |  | 
 | 
												
													
														
															|  |                 //周期执行指标 更新指标执行状态:0未开启,1执行中
 |  | 
 | 
												
													
														
															|  |                 tjQuota.setJobStatus("1");
 |  | 
 | 
												
													
														
															|  |                 quotaDao.save(tjQuota);
 |  | 
 | 
												
													
														
															|  |                 quartzHelper.addJob(Class.forName(quotaVo.getJobClazz()), quotaVo.getCron(), quotaCode, params);
 |  | 
 | 
												
													
														
															|  | 
 |  |         if (tjQuota == null) {
 | 
												
													
														
															|  | 
 |  |             throw new Exception("数据采集任务,id:" + id);
 | 
												
													
														
															|  | 
 |  |         }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |         QuotaVo quotaVo = new QuotaVo();
 | 
												
													
														
															|  | 
 |  |         BeanUtils.copyProperties(tjQuota, quotaVo);
 | 
												
													
														
															|  | 
 |  |         Map<String, Object> params = new HashMap<>(4);
 | 
												
													
														
															|  | 
 |  |         params.put("quota", quotaVo);
 | 
												
													
														
															|  | 
 |  |         params.put("executeFlag", executeFlag);
 | 
												
													
														
															|  | 
 |  |         params.put("startTime", startDate);
 | 
												
													
														
															|  | 
 |  |         params.put("endTime", endDate);
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |         String jobId = quotaVo.getCode();
 | 
												
													
														
															|  | 
 |  |         boolean existJobId = quartzHelper.isExistJob(jobId);
 | 
												
													
														
															|  | 
 |  |         if (existJobId) {
 | 
												
													
														
															|  | 
 |  |             quartzHelper.removeJob(jobId);
 | 
												
													
														
															|  | 
 |  |         }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |         if (StringUtils.isEmpty(quotaVo.getJobClazz())) {
 | 
												
													
														
															|  | 
 |  |             throw new Exception("数据采集任务未配置执行类");
 | 
												
													
														
															|  | 
 |  |         }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |         //往quartz框架添加任务
 | 
												
													
														
															|  | 
 |  |         JobConstant.ExecWay execWay = JobConstant.ExecWay.fromStr(tjQuota.getExecType());
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |         // 初始执行或者立即执行
 | 
												
													
														
															|  | 
 |  |         if (JobConstant.ExecWay.Manual.equals(execWay)) {
 | 
												
													
														
															|  | 
 |  |             try {
 | 
												
													
														
															|  | 
 |  |                 quartzHelper.startNow(Class.forName(quotaVo.getJobClazz()), jobId, params);
 | 
												
													
														
															|  | 
 |  |             } catch (Exception e) {
 | 
												
													
														
															|  | 
 |  |                 throw new ObjectAlreadyExistsException(jobId + "," + tjQuota.getName() + "指标正在执行!");
 | 
												
													
														
															|  |             }
 |  |             }
 | 
												
													
														
															|  | 
 |  |         } else {
 | 
												
													
														
															|  | 
 |  |             //周期执行指标 更新指标执行状态:0未开启,1执行中
 | 
												
													
														
															|  | 
 |  |             tjQuota.setJobStatus("1");
 | 
												
													
														
															|  | 
 |  |             quotaDao.save(tjQuota);
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |             quartzHelper.addJob(Class.forName(quotaVo.getJobClazz()), quotaVo.getCron(), jobId, params);
 | 
												
													
														
															|  |         }
 |  |         }
 | 
												
													
														
															|  |     }
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
											
												
													
														
															|  | @ -78,28 +84,23 @@ public class JobService {
 | 
												
													
														
															|  |      * @param id 指标ID
 |  |      * @param id 指标ID
 | 
												
													
														
															|  |      * @throws Exception
 |  |      * @throws Exception
 | 
												
													
														
															|  |      */
 |  |      */
 | 
												
													
														
															|  |     public void removeJob(Integer id) throws Exception {
 |  | 
 | 
												
													
														
															|  | 
 |  |     public void stop(Integer id) throws Exception {
 | 
												
													
														
															|  |         TjQuota tjQuota = quotaDao.findOne(id);
 |  |         TjQuota tjQuota = quotaDao.findOne(id);
 | 
												
													
														
															|  |         if (tjQuota != null) {
 |  | 
 | 
												
													
														
															|  |             QuotaVo quotaVo = new QuotaVo();
 |  | 
 | 
												
													
														
															|  |             BeanUtils.copyProperties(tjQuota, quotaVo);
 |  | 
 | 
												
													
														
															|  |             String quotaCode = quotaVo.getCode().replace("_", "");
 |  | 
 | 
												
													
														
															|  |             String quotaCodeImmediately = quotaCode + "immediately";
 |  | 
 | 
												
													
														
															|  |             boolean existJob = quartzHelper.isExistJob(quotaCode);
 |  | 
 | 
												
													
														
															|  |             boolean existJobImmediately = quartzHelper.isExistJob(quotaCodeImmediately);
 |  | 
 | 
												
													
														
															|  |             if (existJob) {
 |  | 
 | 
												
													
														
															|  |                 //周期执行jobKey
 |  | 
 | 
												
													
														
															|  |                 quartzHelper.removeJob(quotaCode);
 |  | 
 | 
												
													
														
															|  |             }
 |  | 
 | 
												
													
														
															|  |             if (existJobImmediately) {
 |  | 
 | 
												
													
														
															|  |                 //立即执行jobKey
 |  | 
 | 
												
													
														
															|  |                 quartzHelper.removeJob(quotaCodeImmediately);
 |  | 
 | 
												
													
														
															|  |             }
 |  | 
 | 
												
													
														
															|  |             //周期执行指标 更新指标执行状态:0未开启,1执行中
 |  | 
 | 
												
													
														
															|  |             tjQuota.setJobStatus("0");
 |  | 
 | 
												
													
														
															|  |             quotaDao.save(tjQuota);
 |  | 
 | 
												
													
														
															|  | 
 |  |         if (tjQuota == null) {
 | 
												
													
														
															|  | 
 |  |             throw new Exception("数据采集任务,id:" + id);
 | 
												
													
														
															|  | 
 |  |         }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |         QuotaVo quotaVo = new QuotaVo();
 | 
												
													
														
															|  | 
 |  |         BeanUtils.copyProperties(tjQuota, quotaVo);
 | 
												
													
														
															|  | 
 |  |         String jobId = quotaVo.getCode();
 | 
												
													
														
															|  | 
 |  |         boolean existJobId = quartzHelper.isExistJob(jobId);
 | 
												
													
														
															|  | 
 |  |         if (existJobId) {
 | 
												
													
														
															|  | 
 |  |             quartzHelper.removeJob(jobId);
 | 
												
													
														
															|  |         }
 |  |         }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |         //周期执行指标 更新指标执行状态:0未开启,1执行中
 | 
												
													
														
															|  | 
 |  |         tjQuota.setJobStatus("0");
 | 
												
													
														
															|  | 
 |  |         quotaDao.save(tjQuota);
 | 
												
													
														
															|  |     }
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | }
 |  | }
 |