-- Create table
create table TBL_TASK
(
task_id NUMBER(10) default 0 not null,
task_name VARCHAR2(255) not null,
task_type CHAR(2) not null,
class_name VARCHAR2(300),
method VARCHAR2(100),
opr_id VARCHAR2(15) not null,
expression VARCHAR2(50) not null,
isspringbean NUMBER(2),
create_time CHAR(14),
update_time CHAR(14),
state CHAR(1),
remark VARCHAR2(200),
ext1 VARCHAR2(50)
)
tablespace USERS
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 64K
next 1M
minextents 1
maxextents unlimited
);
-- Add comments to the columns
comment on column TBL_TASK.task_id
is '主键';
comment on column TBL_TASK.task_name
is '任务名称';
comment on column TBL_TASK.task_type
is '任务类型';
comment on column TBL_TASK.class_name
is '实现类';
comment on column TBL_TASK.method
is '实现函数';
comment on column TBL_TASK.opr_id
is '操作员ID';
comment on column TBL_TASK.expression
is '调用周期';
comment on column TBL_TASK.isspringbean
is '是否SPRINGBEAN';
comment on column TBL_TASK.create_time
is '创建时间';
comment on column TBL_TASK.update_time
is '更新时间';
comment on column TBL_TASK.state
is '状态 0正常 1执行中 2执行完毕 3删除状态';
comment on column TBL_TASK.remark
is '备注';
comment on column TBL_TASK.ext1
is '扩展1';
-- Create/Recreate primary, unique and foreign key constraints
alter table TBL_TASK
add constraint PK_TBL_TASK primary key (TASK_ID)
using index
tablespace USERS
pctfree 10
initrans 2
maxtrans 255
storage
(
initial 64K
next 1M
minextents 1
maxextents unlimited
);
-- Create table
create table TBL_TASK_PARAM
(
task_id NUMBER(10) default 0 not null,
param_id NUMBER(10) default 0 not null,
param_name VARCHAR2(60) not null,
param_value NVARCHAR2(100),
create_time CHAR(14),
update_time CHAR(14),
state CHAR(1),
remark VARCHAR2(200),
ext1 CLOB,
ext2 VARCHAR2(50),
ext3 VARCHAR2(50)
)
tablespace USERS
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 64K
next 1M
minextents 1
maxextents unlimited
);
-- Add comments to the columns
comment on column TBL_TASK_PARAM.task_id
is '外键';
comment on column TBL_TASK_PARAM.param_id
is '主键';
comment on column TBL_TASK_PARAM.param_name
is '参数名';
comment on column TBL_TASK_PARAM.param_value
is '参数值';
comment on column TBL_TASK_PARAM.create_time
is '创建时间';
comment on column TBL_TASK_PARAM.update_time
is '更新时间';
comment on column TBL_TASK_PARAM.state
is '状态 0正常 1删除状态';
comment on column TBL_TASK_PARAM.remark
is '备注';
comment on column TBL_TASK_PARAM.ext1
is '扩展1';
comment on column TBL_TASK_PARAM.ext2
is '扩展2';
comment on column TBL_TASK_PARAM.ext3
is '扩展3';
-- Create/Recreate primary, unique and foreign key constraints
alter table TBL_TASK_PARAM
add constraint PK_TBL_TASK_PARAM primary key (PARAM_ID)
using index
tablespace USERS
pctfree 10
initrans 2
maxtrans 255
storage
(
initial 64K
next 1M
minextents 1
maxextents unlimited
);
<!-- 定时任务 -->
<bean id="schedulerManager" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"
<property name="triggers">
<list>
<ref bean="quartzManagerTrigger" />
</list>
</property>
</bean>
<!-- 主定时计划 -->
<bean id="quartzManagerTrigger"
class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="quartzManagerJobDetail" />
<!-- 延时1秒 执行任务 -->
<property name="startDelay" value="1000" />
<!-- 任务执行周期 6秒 -->
<property name="repeatInterval" value="20000" />
</bean>
<bean id="quartzManagerJobDetail"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="quartzManagerBean" />
<property name="targetMethod" value="reScheduleJob" />
<property name="concurrent" value="false" />
</bean>
<bean id="quartzManagerBean" class="com.jiuyu.batchServer.bussiness.reservedrpt.action.QuartzManager"
init-method="initMethod">
<property name="scheduler" ref="schedulerManager" />
<property name="taskService" ref="taskService"/>
</bean>
public class QuartzManager implements BeanFactoryAware {
@SuppressWarnings("unused")
private void reScheduleJob() throws Exception, ParseException {
// 通过查询数据库里计划任务来配置计划任务
LOG.info("reScheduleJob---->"+new Date());
Map paramMap = new HashMap();
List paramList = new ArrayList();
paramList.add("0");//待运行
paramList.add("2");//待删除
paramMap.put("paramList", paramList);
List<TaskVo> quartzList = taskService.selectTaskIsNotDelete(paramMap);//查询不为1和3的任务(已加入,已删除)
if (quartzList != null && quartzList.size() > 0) {
for (TaskVo tbcq1 : quartzList) {
configQuatrz(tbcq1);
}
}
}
public boolean configQuatrz(TaskVo tbcq) {
boolean result = false;
try {
// 运行时可通过动态注入的scheduler得到trigger
CronTriggerBean trigger = (CronTriggerBean) scheduler.getTrigger(
String.valueOf(tbcq.getTaskId()), Scheduler.DEFAULT_GROUP);
// 如果计划任务已存在则调用修改方法
if (trigger != null) {
change(tbcq, trigger);
} else {
// 如果计划任务不存在并且数据库里的任务状态为可用时,则创建计划任务
if (tbcq.getState().equals("0")) {
this.createCronTriggerBean(tbcq);
} else {
tbcq.setState("3");//已加入
taskService.updateTaskFile(tbcq);
}
}
result = true;
} catch (Exception e) {
result = false;
e.printStackTrace();
}
return result;
}
public void change(TaskVo tbcq, CronTriggerBean trigger)
throws Exception {
// 如果任务为可用
if (tbcq.getState().equals("0")) {
// 判断从DB中取得的任务时间和现在的quartz线程中的任务时间是否相等
// 如果相等,则表示用户并没有重新设定数据库中的任务时间,这种情况不需要重新rescheduleJob
if (!trigger.getCronExpression().equalsIgnoreCase(
tbcq.getExpression())) {
trigger.setCronExpression(tbcq.getExpression());
scheduler.rescheduleJob(String.valueOf(tbcq.getTaskId()),
Scheduler.DEFAULT_GROUP, trigger);
}
tbcq.setState("1");//已加入
taskService.updateTaskFile(tbcq);
LOG.info(new Date() + ": 更新" + String.valueOf(tbcq.getTaskId()) + "计划任务");
} else {//删除
// 不可用
scheduler.pauseTrigger(String.valueOf(tbcq.getTaskId()), trigger.getGroup());// 停止触发器
scheduler.unscheduleJob(String.valueOf(tbcq.getTaskId()), trigger.getGroup());// 移除触发器
scheduler.deleteJob(String.valueOf(tbcq.getTaskId()), trigger.getJobGroup());// 删除任务
tbcq.setState("3");
taskService.updateTaskFile(tbcq);
LOG.info(new Date() + ": 删除" + String.valueOf(tbcq.getTaskId()) + "计划任务");
}
}
/**
* 创建/添加计划任务
*
* @param tbcq
* 计划任务配置对象
* @throws Exception
*/
public void createCronTriggerBean(TaskVo tbcq) throws Exception {
// 新建一个基于Spring的管理Job类
MethodInvokingJobDetailFactoryBean mjdfb = new MethodInvokingJobDetailFactoryBean();
mjdfb.setName(String.valueOf(tbcq.getTaskId()));// 设置Job名称
// 如果定义的任务类为Spring的定义的Bean则调用 getBean方法
if ("1".equals(tbcq.getIsspringbean())) {
mjdfb.setTargetObject(beanFactory.getBean(tbcq.getClassName()));// 设置任务类
} else {
// 否则直接new对象
mjdfb.setTargetObject(Class.forName(tbcq.getClassName())
.newInstance());// 设置任务类
}
Object[] arguments = new Object[1];
arguments[0] = tbcq;
mjdfb.setArguments(arguments);
mjdfb.setTargetMethod(tbcq.getMethod());// 设置任务方法
//mjdfb.setConcurrent(tbcq.getConcurrent().equals("0") ? false : true); // 设置是否并发启动任务
mjdfb.setConcurrent(false);//不并发
mjdfb.afterPropertiesSet();// 将管理Job类提交到计划管理类
// 将Spring的管理Job类转为Quartz管理Job类
JobDetail jobDetail = new JobDetail();
jobDetail = (JobDetail) mjdfb.getObject();
jobDetail.setName(String.valueOf(tbcq.getTaskId()));
scheduler.addJob(jobDetail, true); // 将Job添加到管理类
// 新一个基于Spring的时间类
CronTriggerBean c = new CronTriggerBean();
c.setCronExpression(tbcq.getExpression());// 设置时间表达式
c.setName(String.valueOf(tbcq.getTaskId()));// 设置名称
c.setJobDetail(jobDetail);// 注入Job
c.setJobName(String.valueOf(tbcq.getTaskId()));// 设置Job名称
scheduler.scheduleJob(c);// 注入到管理类
scheduler.rescheduleJob(String.valueOf(tbcq.getTaskId()), Scheduler.DEFAULT_GROUP,
c);// 刷新管理类
tbcq.setState("1");//已加入
taskService.updateTaskFile(tbcq);
LOG.info(new Date() + ": 新建" + String.valueOf(tbcq.getTaskId()) + "计划任务");
}
public void setBeanFactory(BeanFactory factory) throws BeansException {
this.beanFactory = factory;
}
public BeanFactory getBeanFactory() {
return beanFactory;
}
}