一、Quartz 调度器
1.使用JobDetail Bean
applicationContext.xml:
<bean id="exampleJob" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="com.atom.timer.JobBeanTimer" />
<property name="jobDataAsMap">
<map>
<entry key="timeout" value="5"/>
</map>
</property>
</bean>
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="exampleJob" />
<property name="cronExpression" value="0 54 14 ? * *" />
</bean>
<!-- <bean id="simpleReportTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="exampleJob" />
<property name="startDelay">
<value>1000</value>
</property>
<property name="repeatInterval">
<value>8000</value>
</property>
</bean> -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger"/>
</list>
</property>
</bean>
JobBeanTimer.java
public class JobBeanTimer extends QuartzJobBean
{
private int timeout;
protected void executeInternal(JobExecutionContext arg0)
throws JobExecutionException
{
System.out.println("==================" + timeout);
}
public void setTimeout(int timeout)
{
this.timeout = timeout;
}
}
2.使用MethodInvokingJobDetailFactoryBean
applicationContext.xml:
<bean id="quartzJob" class="com.atom.timer.QuartzJob"/>
<!-- 定义调用对象和调用对象的方法 -->
<bean id="jobtask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 调用的类 -->
<property name="targetObject">
<ref bean="quartzJob"/>
</property>
<!-- 调用类中的方法 -->
<property name="targetMethod">
<value>work</value>
</property>
</bean>
<!-- 定义触发时间 -->
<bean id="doTime" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="jobtask"/>
</property>
<!-- cron表达式 -->
<property name="cronExpression">
<value>10,15 * * * * ?</value>
</property>
</bean>
<!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
<bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="doTime"/>
</list>
</property>
</bean>
QuartzJob.java
public class QuartzJob
{
public void work()
{
System.out.println("Quartz的任务调度!!!");
}
}
二、使用JDK timer
1.使用 TimerTask
applicationContext.xml:
<bean id="reportTimerTask" class="com.atom.timer.SpringTimer" />
<bean id="scheduleReportTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
<property name="timerTask" ref="reportTimerTask" />
<property name="period">
<value>15000</value>
</property>
<property name="delay">
<value>10000</value>
</property>
</bean>
<bean class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="scheduledTimerTasks">
<list>
<ref bean="scheduleReportTask"/>
</list>
</property>
</bean>
SpringTimer.java
public class SpringTimer extends TimerTask
{
public void run()
{
System.out.println("God, I LOVE YOU!");
}
}
2.使用MethodInvokingTimerTaskFactoryBean
applicationContext.xml:
<bean id="MethodInvokeTimer" class="com.atom.timer.MethodInvokeTimer"/>
<bean id="methodInvokeTaskFactoryBean" class="org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean">
<property name="targetObject" ref="MethodInvokeTimer" />
<property name="targetMethod" value="doSomething" />
</bean>
<bean id="methodInvokeTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
<property name="timerTask" ref="methodInvokeTaskFactoryBean" />
<property name="period">
<value>15000</value>
</property>
<property name="delay">
<value>10000</value>
</property>
</bean>
<bean class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="scheduledTimerTasks">
<list>
<ref bean="methodInvokeTask"/>
</list>
</property>
</bean>
MethodInvokeTimer.java
public class MethodInvokeTimer
{
public void doSomething(){
System.out.println("Method invike timer test.");
}
}