quartzJob 例子
KpiOfPoorQualityJob.java
package com.eastcom_sw.inas.workorder.quartzJob.kpi; import net.sf.json.JSONObject; import org.apache.commons.lang3.StringUtils; import org.jfree.util.Log; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com..utils.DateUtil; import com..utils.service.quartz.QuartzService; import com..workorder.rest.service.KpiOfPoorQualityCellService; import com..workorder.rest.service.WorkorderService; @Service("kpiOfPoorQualityJob") public class KpiOfPoorQualityJob { private static String className = "com..workorder.quartzJob.kpi.KpiOfPoorQualityInvokingJob"; @Autowired private QuartzService quartzService; @Autowired private KpiOfPoorQualityCellService kpiOfPoorQualityCellService; public void execute(String jobName, String jobParames) { String status = "0"; try { String task_name = quartzService.fetchTaskNameByClass(className); System.out.println("####################:" + task_name); String desc = ""; String executeTime = DateUtil.getCurrentDatetime(); long startTime = System.currentTimeMillis(); try { String type = "type_areaIndex,type_areaAppIndex,type_areaNeIndex"; if (StringUtils.isNotBlank(jobParames) && !"null".equals(jobParames)) { JSONObject json = JSONObject.fromObject(jobParames.toLowerCase()); type = json.get("type".toLowerCase()).toString(); } kpiOfPoorQualityCellService.getKpiOfPoorQualityCell(type); desc = "实时数据保存任务执行正常"; } catch (Exception e) { e.printStackTrace(); status = "1"; desc = "实时数据保存任务执行异常"; } long endTime = System.currentTimeMillis(); long between = endTime - startTime; long s = between / 1000; String lastTime = String.valueOf(s);// 获取任务执行花费的时间 quartzService.saveQuartzLog(task_name, lastTime, executeTime, status, desc); } catch (Exception e) { e.printStackTrace(); } Log.info("--------task start--------"); } }
package com.eastcom_sw.inas.workorder.quartzJob.kpi; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.web.context.WebApplicationContext; import com..utils.quartzInit.WebAppicationContextBean; public class KpiOfPoorQualityInvokingJob implements Job { @SuppressWarnings("rawtypes") @Override public void execute(JobExecutionContext context) throws JobExecutionException { WebAppicationContextBean wacb = new WebAppicationContextBean(); WebApplicationContext wac = wacb.getWebAppicationContext(); KpiOfPoorQualityJob kpiOfPoorQualityJob = (KpiOfPoorQualityJob) wac.getBean("kpiOfPoorQualityJob"); String jobName = context.getMergedJobDataMap().getString("targetJobName"); Object jobP = context.getMergedJobDataMap().get("jobParames"); String jobParames = null; if (jobP != null) { jobParames = String.valueOf(jobP); } kpiOfPoorQualityJob.execute(jobName, jobParames); } }