Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
The MIT License (MIT) * Copyright (c) 2019 铭飞科技
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package net.mingsoft.quartz.biz.impl;
import net.mingsoft.quartz.constant.Const;
import net.mingsoft.quartz.utils.ScheduleUtil;
import org.quartz.JobDataMap;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import net.mingsoft.base.biz.impl.BaseBizImpl;
import net.mingsoft.base.dao.IBaseDao;
import java.util.*;
import net.mingsoft.quartz.entity.JobEntity;
import net.mingsoft.quartz.biz.IJobBiz;
import net.mingsoft.quartz.dao.IJobDao;
import javax.annotation.PostConstruct;
/**
* 任务实体表管理持久化层
* @author 铭飞开源团队
* 创建日期:2019-11-21 10:09:26
* 历史修订:
*/
@Service("jobBizImpl")
public class JobBizImpl extends BaseBizImpl implements IJobBiz {
@Autowired
private IJobDao jobDao;
@Autowired
private Scheduler scheduler;
@Override
protected IBaseDao getDao() {
// TODO Auto-generated method stub
return jobDao;
}
/**
* 项目启动时,初始化定时器
* 主要是防止手动修改数据库导致未同步到定时任务处理(注:不能手动修改数据库ID和任务组名,否则会导致脏数据)
*/
@PostConstruct
public void init() throws SchedulerException
{
List jobList = queryAll();
for (JobEntity job : jobList)
{
updateSchedulerJob(job, job.getQjGroup());
}
}
/**
* 立即运行任务
*
* @param job 调度信息
*/
@Override
public void run(JobEntity job) throws SchedulerException
{
String jobId = job.getId();
String jobGroup = job.getQjGroup();
JobEntity properties = (JobEntity)getEntity(Integer.parseInt(job.getId()));
// 参数
JobDataMap dataMap = new JobDataMap();
dataMap.put(Const.TASK_PROPERTIES, properties);
scheduler.triggerJob(ScheduleUtil.getJobKey(jobId, jobGroup), dataMap);
}
@Override
public int saveEntity(JobEntity job) throws SchedulerException {
int i = super.saveEntity(job);
if(i>0){
ScheduleUtil.createScheduleJob(scheduler,job);
}
return i;
}
@Override
public void updateEntity(JobEntity job) throws SchedulerException {
JobEntity jobEntity = (JobEntity)getEntity(Integer.parseInt(job.getId()));
super.updateEntity(job);
updateSchedulerJob(job, jobEntity.getQjGroup());
}
@Override
public void createJob(JobEntity job) throws SchedulerException {
ScheduleUtil.createScheduleJob(scheduler,job);
}
/**
* 更新任务
*
* @param job 任务对象
* @param jobGroup 任务组名
*/
public void updateSchedulerJob(JobEntity job, String jobGroup) throws SchedulerException
{
String jobId = job.getId();
// 判断是否存在
JobKey jobKey = ScheduleUtil.getJobKey(jobId, jobGroup);
if (scheduler.checkExists(jobKey))
{
// 防止创建时存在数据问题 先移除,然后在执行创建操作
scheduler.deleteJob(jobKey);
}
ScheduleUtil.createScheduleJob(scheduler, job);
}
}