【过往系列文章】
在我们的任务类里面,我们需要使用到spring ioc 依赖注入的接口实现类
因为我们是Springmvc,我们需要在xml做一下配置,一般是spring-application.xml加入spring-quartz.xml
<import resource="spring-quartz.xml"/>
这个spring-quartz.xml我们新建一个,如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 动态任务组件 -->
<bean id="jobFactory" class="com.jy.task.config.JobFactory"></bean>
<bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" >
<property name="jobFactory" ref="jobFactory"></property>
</bean>
</beans>
代码结构如下
创建一个JobFactory的类
package com.jy.task.config;
import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.scheduling.quartz.AdaptableJobFactory;
public class JobFactory extends AdaptableJobFactory{
@Autowired
private AutowireCapableBeanFactory capableBeanFactory;
@Override
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
//调用父类的方法
Object jobInstance = super.createJobInstance(bundle);
//进行注入
capableBeanFactory.autowireBean(jobInstance);
return jobInstance;
}
}
创建ScheduleJobInit类,这个主要是启动的时候我们加载任务
package com.jy.task.config;
import javax.annotation.PostConstruct;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.jy.service.task.job.ScheduleService;
/**
* 定时任务初始化
*/
@Component
public class ScheduleJobInit {
/** 日志对象 */
private static final Logger LOG = LogManager.getLogger(ScheduleJobInit.class);
/** 定时任务service */
@Autowired
private ScheduleService scheduleJobService;
/**
* 项目启动时初始化
*/
@PostConstruct
public void init() {
LOG.info("定时任务初始化init");
try {
scheduleJobService.initScheduleJob();
} catch (Exception e) {
LOG.error("定时任务初始化异常",e);
}
LOG.info("定时任务初始化end");
}
}
iniScheduleJob方法参考
@Override
public void initScheduleJob() throws Exception {
//查找启用的任务
List<Schedule> scheduleJobList = dao.getRunList();
if (ObjectUtil.isNotEmpty(scheduleJobList)) {
for (Schedule scheduleJob : scheduleJobList) {
CronTrigger cronTrigger = ScheduleKit.getCronTrigger(scheduler, scheduleJob.getName(),scheduleJob.getGrouping());
if (cronTrigger == null) {
// 不存在,创建一个
ScheduleKit.createJob(scheduler, scheduleJob);
} else {
// 已存在,那么更新相应的定时设置
ScheduleKit.updateJob(scheduler, scheduleJob);
}
}
}
}
创建常量类JobConst
package com.jy.task.common;
public class JobConst {
private JobConst() {}
/** 任务调度的参数key */
public static final String JOB_PARAM_KEY = "scheduleJob";
/**
* 默认一次任务
*/
public static final String DEF_ONCE_CRON="0 0 10/1 * * ?";
public enum Type{
BEGIN(1,"运行"),EXCEPTION(2,"异常"),COMPLETE(3,"完成");
private Integer code;
private String dec;
private Type(int code,String dec){this.code = code;this.dec = dec;}
public int code() {return this.code;}
public String dec(){return this.dec;}
}
}
任务实体类Schedule
package com.jy.entity.task.job;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.ibatis.type.Alias;
import com.jy.entity.base.BaseEntity;
@Alias("Schedule")
public class Schedule extends BaseEntity{
private static final long serialVersionUID = 1L;
/** 任务id */
private String id;
/** 任务名称 */
private String name;
/** 任务分组 */
private String grouping;
/** 任务别名 */
private String aliasName;
/** 指定执行类 */
private String jobClass;
/** 任务状态 0停用 1启用 2删除 */
private Integer status;
/** 任务运行时间表达式 */
private String cron;
private List<Date> nextTimePoints=new ArrayList<>();
/** 任务描述 */
private String description;
/** 创建时间 */
private Date createTime;
/** 修改时间 */
private Date updateTime;
private String keyWord;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGrouping() {
return grouping;
}
public void setGrouping(String grouping) {
this.grouping = grouping;
}
public String getCron() {
return cron;
}
public void setCron(String cron) {
this.cron = cron;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getJobClass() {
return jobClass;
}
public void setJobClass(String jobClass) {
this.jobClass = jobClass;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public List<Date> getNextTimePoints() {
return nextTimePoints;
}
public void setNextTimePoints(List<Date> nextTimePoints) {
this.nextTimePoints = nextTimePoints;
}
public String getAliasName() {
return aliasName;
}
public void setAliasName(String aliasName) {
this.aliasName = aliasName;
}
public String getKeyWord() {
return keyWord;
}
public void setKeyWord(String keyWord) {
this.keyWord = keyWord;
}
}
这里我们写一个测试的job试试
创建JobTException类
package com.jy.task.job.test;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;
import com.jy.entity.task.job.Schedule;
import com.jy.entity.task.log.TaskLog;
import com.jy.service.task.log.TaskLogService;
import com.jy.task.common.JobConst;
import com.jy.task.common.ScheduleKit;
public class JobTException implements Job{
/* 日志对象 */
private static final Logger logger = LogManager.getLogger(JobTException.class);
@Autowired
private TaskLogService logService;
@Override
public void execute(JobExecutionContext context){
Schedule scheduleJob = (Schedule) context.getMergedJobDataMap().get(JobConst.JOB_PARAM_KEY);
// 获取日志
TaskLog log = ScheduleKit.getLog(scheduleJob);
logService.record(log.type(JobConst.Type.BEGIN));
try {
int abc=1/0;
System.out.println(abc);
logService.record(log.type(JobConst.Type.COMPLETE));
} catch (Exception e) {
logger.error(e.toString(),e);
logService.record(log.type(JobConst.Type.EXCEPTION).description(e.toString()));
}
}
}
这个类可以根据需要改成自己需要,这个只是为了测试而写,定时抛一个计算异常,
至于页面设计可以参考一下如下配套
列表版面设计参考
添加一个任务版面设计参考
然后这篇章这里结束
下一个篇幅会介绍一下我整理的工具类,敬请留意
版权声明:本文为hj7jay原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。