Jelajahi Sumber

代码修改

LAPTOP-KB9HII50\70708 2 tahun lalu
induk
melakukan
c5c98551bb

+ 0 - 11
svr/svr-basic/pom.xml

@ -47,17 +47,6 @@
            <version>2.2.6</version>
        </dependency>
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.2.3</version>
        </dependency>
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz-jobs</artifactId>
            <version>2.2.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>

+ 10 - 4
svr/svr-basic/src/main/java/com/yihu/jw/basic/agadmin/controller/quota/TjQuotaController.java

@ -2,7 +2,6 @@ package com.yihu.jw.basic.agadmin.controller.quota;
import com.fasterxml.jackson.core.type.TypeReference;
import com.yihu.jw.basic.dict.service.SystemDictEntryService;
import com.yihu.jw.basic.quota.controller.JobController;
import com.yihu.jw.basic.quota.service.*;
import com.yihu.jw.basic.resource.service.RsResourceQuotaService;
import com.yihu.jw.basic.util.TemPath;
@ -43,8 +42,7 @@ import java.util.stream.Collectors;
@Api( tags = {"网关-统计"})
public class TjQuotaController extends EnvelopRestEndpoint {
    @Resource
    private JobController jobController;
    @Resource
    private SystemDictEntryService dictEntryService;
    @Resource
@ -928,7 +926,15 @@ public class TjQuotaController extends EnvelopRestEndpoint {
    @PostMapping("removeQuota")
    public Object removeQuota(Long tjQuotaId) {
        try {
            return jobController.removeQuota(tjQuotaId);
            String url = quotaUrl + "/api/v1.0/job/removeQuota";
            List<NameValuePair> params = new ArrayList<>();
            params.add(new BasicNameValuePair("id", tjQuotaId+""));
            String infoResponse = httpClientUtil.post(url,params,"UTF-8");
            if("true".equals(infoResponse)){
                return success("执行成功");
            }
            return Envelop.getError("执行失败");
        } catch (Exception e) {
            e.printStackTrace();
            return failed(ERR_SYSTEM_DES);

+ 0 - 85
svr/svr-basic/src/main/java/com/yihu/jw/basic/quota/controller/JobController.java

@ -1,85 +0,0 @@
package com.yihu.jw.basic.quota.controller;
import com.yihu.ehr.constants.ApiVersion;
import com.yihu.ehr.constants.ServiceApi;
import com.yihu.jw.basic.quota.service.JobService;
import com.yihu.jw.restmodel.web.Envelop;
import com.yihu.jw.restmodel.web.endpoint.EnvelopRestEndpoint;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
 * 指标任务控制
 *
 * @author janseny
 */
@RestController
@RequestMapping(ApiVersion.Version1_0)
@Api(description = "指标任务控制")
public class JobController extends EnvelopRestEndpoint {
    @Autowired
    private JobService jobService;
    /**
     * 初始执行指标
     */
    @ApiOperation(value = "初始执行指标")
    @RequestMapping(value = ServiceApi.TJ.FirstExecuteQuota, method = RequestMethod.POST)
    public Envelop firstExecuteQuota(
            @ApiParam(name = "id", value = "指标ID", required = true)
            @RequestParam(value = "id", required = true) Long id) {
        try {
            jobService.executeJob(id, "1", null, null);
            return success("执行成功");
        } catch (Exception e) {
            e.printStackTrace();
            return failed("执行失败");
        }
    }
    /**
     * 指标执行
     */
    @ApiOperation(value = "执行指标")
    @RequestMapping(value = ServiceApi.TJ.TjQuotaExecute, method = RequestMethod.POST)
    public Envelop executeQuota(
            @ApiParam(name = "id", value = "指标ID", required = true)
            @RequestParam(value = "id", required = true) Long id,
            @ApiParam(name = "startDate", value = "起始日期")
            @RequestParam(value = "startDate", required = false) String startDate,
            @ApiParam(name = "endDate", value = "截止日期")
            @RequestParam(value = "endDate", required = false) String endDate) {
        try {
            jobService.executeJob(id, "2", startDate, endDate);
            return success("执行成功");
        } catch (Exception e) {
            e.printStackTrace();
            return failed("执行失败");
        }
    }
    @ApiOperation(value = "停止指标")
    @RequestMapping(value = ServiceApi.TJ.TjQuotaRemove, method = RequestMethod.POST)
    public Envelop removeQuota(
            @ApiParam(name = "id", value = "指标ID", required = true)
            @RequestParam(value = "id", required = true) Long id) {
        try {
            jobService.removeJob(id);
            return success("执行成功");
        } catch (Exception e) {
            e.printStackTrace();
            return failed("执行失败");
        }
    }
}

+ 0 - 109
svr/svr-basic/src/main/java/com/yihu/jw/basic/quota/service/JobService.java

@ -1,109 +0,0 @@
package com.yihu.jw.basic.quota.service;
import com.yihu.jw.basic.es.EsResultExtract;
import com.yihu.jw.basic.quota.dao.XTjQuotaRepository;
import com.yihu.jw.basic.quota.model.QuotaVo;
import com.yihu.jw.basic.quota.util.QuartzHelper;
import com.yihu.jw.entity.ehr.quota.TjQuota;
import org.quartz.ObjectAlreadyExistsException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.HashMap;
import java.util.Map;
/**
 * @author chenweida
 */
@Service
public class JobService {
    @Autowired
    private QuartzHelper quartzHelper;
    @Autowired
    private XTjQuotaRepository quotaDao;
    @Autowired
    private EsResultExtract esResultExtract;
    /**
     * 启动指标任务
     *
     * @param id          指标ID
     * @param executeFlag 执行动作标识,1:初始执行(全量统计),2:立即执行、周期执行(增量统计)
     * @param startDate   抽取数据起始日期。初始执行时为NULL;立即执行时需要传值;周期执行时也为NULL,如果是基础指标周期执行,后续默认赋值昨天开始。
     * @param endDate     抽取数据截止日期。初始执行时无NULL;立即执行时需要传值;周期执行时也为NULL,如果是基础指标周期执行,后续默认赋值昨天截止。
     * @throws Exception
     */
    public void executeJob(Long id, String executeFlag, String startDate, String endDate) throws Exception {
        TjQuota tjQuota = quotaDao.getOne(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);
            }
        }
    }
    /**
     * 停止指标任务
     *
     * @param id 指标ID
     * @throws Exception
     */
    public void removeJob(Long id) throws Exception {
        TjQuota tjQuota = quotaDao.getOne(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);
        }
    }
}

+ 0 - 110
svr/svr-basic/src/main/java/com/yihu/jw/basic/quota/util/QuartzHelper.java

@ -1,110 +0,0 @@
package com.yihu.jw.basic.quota.util;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.Map;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
@Component("quartzHelper")
public class QuartzHelper {
    @Autowired
    private SchedulerFactoryBean schedulerFactoryBean;
    private Scheduler scheduler = null;
    @PostConstruct
    public void init() {
        try {
            scheduler = schedulerFactoryBean.getScheduler();
            scheduler.start();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
    public void addJob(Class jobClass, String cronString, String jobKey,
                       Map<String, Object> params) throws Exception {
        if (!CronExpression.isValidExpression(cronString)) {
            throw new Exception("cronExpression is not a valid Expression");
        }
        try {
            JobDetail job = JobBuilder.newJob(jobClass)
                    .withIdentity("job-id:" + jobKey, "job-group:" + jobKey)
                    .build();
            JobDataMap jobDataMap = job.getJobDataMap();
            jobDataMap.putAll(params);
            CronTrigger trigger = TriggerBuilder
                    .newTrigger()
                    .withIdentity("trigger-name:" + jobKey,
                            "trigger-group:" + jobKey)
                    .withSchedule(CronScheduleBuilder.cronSchedule(cronString))
                    .build();
            scheduler.scheduleJob(job, trigger);
            scheduler.start();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
    public void removeJob(String jobKeyString) throws Exception {
        TriggerKey triggerKey = new TriggerKey("trigger-name:" + jobKeyString,
                "trigger-group:" + jobKeyString);
        JobKey jobName = new JobKey("job-group:" + jobKeyString, "job-id:"
                + jobKeyString);
        scheduler.pauseTrigger(triggerKey);// 停止触发器
        scheduler.unscheduleJob(triggerKey);// 移除触发器
        scheduler.deleteJob(jobName);// 删除任务
    }
    public boolean isExistJob(String jobKey) throws SchedulerException {
        JobKey jk = new JobKey("job-id:" + jobKey, "job-group:" + jobKey);
        if (scheduler.checkExists(jk)) {
            return true;
        } else {
            return false;
        }
    }
    /**
     * 立即执行任务。
     *
     * @param jobClass
     * @param id
     * @param params
     * @throws Exception
     */
    public void startNow(Class jobClass, String id, Map<String, Object> params) throws Exception {
        startAt(new Date(), jobClass, id, params);
    }
    /**
     * 在指定时间点执行。
     *
     * @param time
     * @param jobClass
     * @param id
     * @param params
     * @throws Exception
     */
    public void startAt(Date time, Class jobClass, String id, Map<String, Object> params) throws Exception {
        JobDetail job = JobBuilder.newJob(jobClass).
                withIdentity("job-id:" + id, "job-group:" + id)
                .build();
        JobDataMap jobDataMap = job.getJobDataMap();
        if(null != params) jobDataMap.putAll(params);
        SimpleTrigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger-id:" + id, "group-group:" + id)
                .startAt(time)
                .withSchedule(simpleSchedule().withIntervalInSeconds(10).withRepeatCount(0).withMisfireHandlingInstructionFireNow())
                .build();
        scheduler.scheduleJob(job, trigger);
        scheduler.start();
    }
}