quartz使用案例

时间:2022-05-25 12:07:54
@Service
public class QuartzServiceImpl extends
BaseServiceImpl<JobDetails, String, QuartzTaskVO> implements
QuartzService { private JobDetailsJpaDao jobDetailsJpaDao; public JobDetailsJpaDao getJobDetailsJpaDao() {
return jobDetailsJpaDao;
} @Autowired
public QuartzServiceImpl(JobDetailsJpaDao jobDetailsJpaDao,
BaseJdbcDao baseJdbcDao) {
super((BaseJpaRepository<JobDetails, String>) jobDetailsJpaDao,
baseJdbcDao);
this.jobDetailsJpaDao = jobDetailsJpaDao;
} @Autowired
private Scheduler scheduler; @ServiceDesc("查询当前活动的任务列表")
@Override
public Map<String, Object> queryActiveTaskMap(JobDetailsQueryParam param) {
SQLQueryUtil util = new SQLQueryUtil();
util.addTable("QRTZ_TRIGGERS");
util.addColumn("SCHED_NAME", "schedule_name");
util.addColumn("JOB_NAME", "job_name");
util.addColumn("JOB_GROUP", "job_group");
util.addColumn("TRIGGER_NAME", "trigger_name");
util.addColumn("TRIGGER_GROUP", "trigger_group");
util.addColumn("PREV_FIRE_TIME", "prev_fire_time");
util.addColumn("NEXT_FIRE_TIME", "next_fire_time");
util.addColumn("START_TIME", "start_time");
util.addColumn("END_TIME", "end_time");
util.addColumn("TRIGGER_STATE", "trigger_state"); return super.queryDataMapNotExtend(util, QuartzTaskVO.class, param);
} @Override
@ServiceDesc("启动任务")
public void startTask(String jobId, String triggerId) throws AppException {
TaskId id = new TaskId(jobId, triggerId);
try {
scheduler.scheduleJob(id.getJobDetails().getJobDetail(), id
.getTriggers().getTrigger());
} catch (SchedulerException e) {
throw new AppException(ResultCode.Failure, e.getMessage());
} catch (ClassNotFoundException e) {
throw new AppException(ResultCode.Failure, e.getMessage());
}
} @Override
@ServiceDesc("暂停任务")
public void pauseTask(String... ids) throws AppException {
try {
for (String idStr : ids) {
scheduler.pauseJob(new TaskId(idStr).getJobKey());
}
} catch (SchedulerException e) {
throw new AppException(ResultCode.Failure, e.getMessage());
}
} @Override
@ServiceDesc("恢复任务")
public void resumeTask(String... ids) throws AppException {
try {
for (String idStr : ids) {
scheduler.resumeJob(new TaskId(idStr).getJobKey());
}
} catch (SchedulerException e) {
throw new AppException(ResultCode.Failure, e.getMessage());
}
} @Override
@ServiceDesc("删除任务")
public void deleteTask(String... ids) throws AppException {
try {
for (String idStr : ids) {
scheduler.deleteJob(new TaskId(idStr).getJobKey());
}
} catch (SchedulerException e) {
throw new AppException(ResultCode.Failure, e.getMessage());
}
} } class TaskId { /**
* id字符串格式: jobName:jobGroup;triggerName:triggerGroup
*/
public TaskId(String id) {
this.jobKey = new JobKey(id.split(";")[0].split(":")[0],
id.split(";")[0].split(":")[1]);
this.triggerKey = new TriggerKey(id.split(";")[1].split(":")[0],
id.split(";")[1].split(":")[1]);
} public TaskId(String jobId, String triggerId) {
JobDetailsService jobDetailsService = (JobDetailsService) SpringContextUtil
.getUtil().getBean(JobDetailsService.class);
TriggersService triggersService = (TriggersService) SpringContextUtil
.getUtil().getBean(TriggersService.class);
this.jobDetails = jobDetailsService.get(jobId);
this.triggers = triggersService.get(triggerId);
this.jobKey = new JobKey(jobDetails.getJobName(),
jobDetails.getJobGroup());
this.triggerKey = new TriggerKey(triggers.getTriggerName(),
triggers.getTriggerGroup());
} private JobDetails jobDetails; private Triggers triggers; private JobKey jobKey; private TriggerKey triggerKey; public JobDetails getJobDetails() {
return jobDetails;
} public void setJobDetails(JobDetails jobDetails) {
this.jobDetails = jobDetails;
} public Triggers getTriggers() {
return triggers;
} public void setTriggers(Triggers triggers) {
this.triggers = triggers;
} public JobKey getJobKey() {
return jobKey;
} public void setJobKey(JobKey jobKey) {
this.jobKey = jobKey;
} public TriggerKey getTriggerKey() {
return triggerKey;
} public void setTriggerKey(TriggerKey triggerKey) {
this.triggerKey = triggerKey;
} }