Quartz之所以这么强大,不仅在于其功能的完善,并且能够与绝大多数的框架无缝结合一起,其中就包括了我们今天所要谈论的Spring框架。如果我们的项目是maven项目,首先必须导入Spring相关的以及Quantz相关的依赖。Quartz的配置需要在Springmvc的配置文件中进行配置,如果不是Springmvc,则可以在Spring的配置文件(applicationContext.xml)中配置。
两种配置作业(Job)方式
- 第一种方式配置作业:优点:调用特定bean的一个方法的时候很适用。首先创建Job类:
@Component("myBean")
public class MyBean {
public void printMessage() {
// 打印当前的执行时间,格式为2017-01-01 10:00:00
Date date=new Date();
SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("MyBean.printMessage()"+sf.format(date));
}
}
在配置文件中配置:
<!-- 第一种方式配置作业:优点:调用特定bean的一个方法的时候很适用-->
<bean id="simpleJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="myBean"></property>
<property name="targetMethod" value="printMessage"></property>
</bean>
- 第二种方式配置作业:优点:支持传递参数。首先创建需要传递的实体对象:
@Component("anotherBean")
public class AnotherBean {
public void printAnotherMessage() {
System.out.println("AnotherMessage");
}
}
然后再创建Job工作类,并且引入上面创建的实体类,并创建Setter、Getter方法:
public class FirstSchedulerJob extends QuartzJobBean{
private AnotherBean anotherBean;
@Override
protected void executeInternal(JobExecutionContext arg0)
throws JobExecutionException {
// TODO Auto-generated method stub
Date date=new Date();
SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("FirstSchedulerJob"+sf.format(date));
this.anotherBean.printAnotherMessage();
}
public AnotherBean getAnotherBean() {
return anotherBean;
}
public void setAnotherBean(AnotherBean anotherBean) {
this.anotherBean = anotherBean;
}
}
配置文件中配置:
<!-- 第二种方式配置作业:优点:支持传递参数 -->
<bean id="firstComplexJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="com.quartz.springQuartz.FirstSchedulerJob"></property>
<property name="jobDataMap">
<map>
<entry key="anotherBean" value-ref="anotherBean"></entry>
</map>
</property>
<property name="durability" value="true"></property>
</bean>
两种方式配置Trigger
- SimpleTrigger配置:
<!-- 距离当前时间1秒之后执行,之后每隔两秒钟执行一次 -->
<bean id="mySimpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
<!-- 需要引入的Job类 -->
<property name="jobDetail" ref="simpleJobDetail"></property>
<!-- 开始时间 -->
<property name="startDelay" value="1000"></property>
<!-- 间隔 -->
<property name="repeatInterval" value="2000"></property>
</bean>
- CronTrigger配置:
<!-- 每隔5秒执行一次 -->
<bean id="myCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<!-- 需要引入的Job类 -->
<property name="jobDetail" ref="firstComplexJobDetail"></property>
<!-- cron表达式 -->
<property name="cronExpression" value="0/5 * * * * ? *"></property>
</bean>
配置Scheduler
最后,把先前配置Trigger加入scheduler配置中,Job可以加入也可以不加入,因为已经在Trigger中引用:
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="jobDetails">
<list>
<ref bean="simpleJobDetail"/>
<ref bean="firstComplexJobDetail"/>
</list>
</property>
<property name="triggers">
<list>
<ref bean="mySimpleTrigger"/>
<ref bean="myCronTrigger"/>
</list>
</property>
</bean>