spring quarter
【一】Spring quarter设置方法一:
(1)新建一个要跑批的类
package com.test.job
public class AutoJob{
public void doAutoSomething(){
}
}
(2)配置spring quarter.
<!--注入ben-->
<bean id="autoJob" class="com.test.job.AutoJob">
</bean>
<!--配置job详情及方法 -->
<bean id="autoJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="autoJob"/>
</property>
<property name="targetMethod">
<value>doAutoSomething</value>
</property>
</bean>
<!-- 配置job Trigger跑批时间-->
<bean id="autoTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="autoJobDetail"/>
</property>
<property name="cronExpression">
<value>0 10 5 * * ?</value>
</property>
</bean>
<!-- 引入要批量跑批的job-->
<bean id="myquartz" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref local="autoTrigger"/>
</list>
</property>
</bean>
【二】Spring quarter设置方法二
(1)新建一个要跑批的类
public class TestSpringJob implements Job{
@Override
public void execute(JobExecutionContext context) throws JobExecutionException{
}
}
(2)配置jobdetail
<bean id="testSpringJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="com.test.TestSpringJob"></property>
</bean>
(3)配置job触发器
<bean id="testSpringJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="testSpringJobDetail" />
<property name="cronExpression" value="0 10 5 * * ?" />
</bean>
(4)加入调度器中
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="testSpringJobTrigger" />
</list>
</property>
</bean>
【三】Spring quarter设置方法三
(1)新建一个父类
public abstract class ParentClassJob extends JobDetailFactoryBean implements Job,InitializingBean{
public ParentClassJob(){
this.setJobClass(this.getClass());
}
}
(2)新建一个要跑批的类
@Component("subClassJob")
public class subClassJob extends ParentClassJob{
public void execute(JobExecutionContext context){
}
}
(3)配置触发器
<bean id="subClassTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="subClassJob" />
<property name="cronExpression" value="0 10 5 * * ?" />
</bean>
(4)加载触发器到调度器中
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<!--
<ref bean="subClassTrigger" />
</list>
</property>
</bean>
【四】Spring 监听类名
public class MyTrigger {
/**
* 名称
*/
private String triggerName;
/**
* 启动时间
*/
private Date startTime;
/**
* 最后一次执行时间
*/
private Date lastFireTime;
/**
* 下一次执行时间
*/
private Date nextFireTime;
/**
* 状态
*/
private int state;
/**
* 时间配置表达式
*/
private String cronExpression;
public String getCronExpression() {
return cronExpression;
}
public void setCronExpression(String cronExpression) {
this.cronExpression = cronExpression;
}
public String getTriggerName() {
return triggerName;
}
public void setTriggerName(String triggerName) {
this.triggerName = triggerName;
}
public Date getStartTime() {
return startTime;
}
public void setStartTime(Date startTime) {
this.startTime = startTime;
}
public Date getLastFireTime() {
return lastFireTime;
}
public void setLastFireTime(Date lastFireTime) {
this.lastFireTime = lastFireTime;
}
public Date getNextFireTime() {
return nextFireTime;
}
public void setNextFireTime(Date nextFireTime) {
this.nextFireTime = nextFireTime;
}
public int getState() {
return state;
}
public String getStateDetail() {
String rst = null;
switch (this.state) {
case 0:
rst = "正常";
break;
case 1:
rst = "暂停中";
break;
case 2:
rst = "执行中";
break;
case 3:
rst = "异常";
break;
case 4:
rst = "阻塞中";
break;
default:
rst = "未知" + this.state;
break;
}
return rst;
}
public void setState(int state) {
this.state = state;
}
(5)获取监听相关信息
public List<MyTrigger> getTriggerList() throws Exception {
List<MyTrigger> result = new ArrayList<MyTrigger>();
Scheduler scheduler = this.schedulerFactoryBean.getScheduler();
for (String triggerGroupName : scheduler.getTriggerGroupNames()) {
for (String triggerName : scheduler.getTriggerNames(triggerGroupName)) {
Trigger trigger = scheduler.getTrigger(triggerName, triggerGroupName);
MyTrigger myTrigger = newMyTrigger();
if(trigger instanceof CronTriggerBean){
CronTriggerBean ctb = (CronTriggerBean)trigger;
myTrigger.setCronExpression(ctb.getCronExpression());
}
myTrigger.setTriggerName(trigger.getName());
myTrigger.setLastFireTime(trigger.getPreviousFireTime());
myTrigger.setNextFireTime(trigger.getNextFireTime());
myTrigger.setStartTime(trigger.getStartTime());
myTrigger.setState(scheduler.getTriggerState(triggerName, triggerGroupName));
result.add(myTrigger);
}
}
return result;
}
public void pauseTrigger(String triggerName) throws Exception {
Scheduler scheduler = this.schedulerFactoryBean.getScheduler();
scheduler.pauseTrigger(triggerName, null);
}
public void resumeTrigger(String triggerName) throws Exception {
Scheduler scheduler = this.schedulerFactoryBean.getScheduler();
scheduler.resumeTrigger(triggerName, null);
}
public void runTrigger(String triggerName) throws Exception {
Scheduler scheduler = this.schedulerFactoryBean.getScheduler();
String jobName = scheduler.getTrigger(triggerName, null).getJobName();
scheduler.triggerJob(jobName, null);
}