`
liuye
  • 浏览: 53990 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Spring中时间任务调度Quarta

    博客分类:
  • java
阅读更多

今天五四青年节,闲着无聊,分享一下Spring中的Quartz时间任务调度机制。
当然使用Quartz的前提是在Spring框架中的。
第一,向工程中添加quartz-all-1.6.0.jar,在spring-framework-2.*中的lib\quarta\目录下就有,可能版本有点差异,但并影响Quartz的使用
第二,新建你的 EmailReportJob.java 文件,代码如下:
package com.inqgen.hxy.common;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class EmailReportJob extends QuartzJobBean {

private static int i=1;

@Override
protected void executeInternal(JobExecutionContext context)
throws JobExecutionException {
System.out.println(new java.util.Date());
System.out.println((i++)+imageDatabase);
}
private String imageDatabase;

public void setImageDatabase(String imageDatabase){
this.imageDatabase = imageDatabase;
}
}
这个类EmailReportJob需得继承org.springframework.scheduling.quartz.QuartzJobBean,并实现其executeInternal方法,若你有Servic层的话,也可以将String imageDatabase换成你所需要的Service,其注入将会在applicationContext-config.xml中配置~
第三,配置applicationContext-config.xml
1.自己所写的EmailReportJob.java类,是注入到org.springframework.scheduling.quartz.JobDetailBean类中的jobClass属性中,map的配置方式及上步骤中所提到的(若有Servic层的话)Service层的注入方式,
但写法可能就为<entry key="imageDatabase" ref="Servic" />而这个Servic必须要受Spring容器管理;若有多个Map的话,要在com.inqgen.hxy.common.EmailReportJob中写入其的setter方法
<bean id="reportJob" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="com.inqgen.hxy.common.EmailReportJob" />
<property name="jobDataAsMap">
<map>
<entry key="imageDatabase" value="imageDatabase" />
</map>
</property>
</bean>
所配置的JobDetailBean,本人管它叫【工作任务】。
2.配置TriggerBean在这里我们叫他【时间触发器】
<bean id="simpleReportTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail">
<ref bean="reportJob" />
</property>
<property name="startDelay">
<value>1000</value>
</property>
<property name="repeatInterval">
<value>2000</value>
</property>
</bean>
startDelay是触发前等待的时间(为毫秒ms),repeatInterval是触发的间隔时间jobDetail,是将我们配置的【工作任务】添加到【时间触发器】中,上述的写法的意思是,每隔2秒触发一次,再延缓1秒才执行
SimpleTriggerBean是一种简单的间隔触发执行代码的一个时间触发器,也可以写成CronTriggerBean,如下
------------------------------------------------
<bean id="checkImagesTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="reportJob" />
<property name="cronExpression" value="0/5 * * * * ?" />
</bean>
jobDetail的配置和上面的一样,cronExpression就有所不同了,上述的写法的意思是,每隔5秒触发一次
3.配置【调度工厂】
<bean id="scheduler"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref local="simpleReportTrigger" />
<ref local="checkImagesTrigger" />
</list>
</property>
<property name="quartzProperties">
<props>
<prop key="org.quartz.threadPool.threadCount">5</prop>
</props>
</property>
</bean>
把2步骤中所配置的【时间触发器】注入到triggers中;
quartzProperties是启动的线程数。
OK!This end!~
大功告成!
-------------------------------------------------
下面是介绍cronExpression的一些含义~
Expression
Meaning
"0 0 12 * * ?"
Fire at 12pm (noon) every day
"0 15 10 ? * *"
Fire at 10:15am every day
"0 15 10 * * ?"
Fire at 10:15am every day
"0 15 10 * * ? *"
Fire at 10:15am every day
"0 15 10 * * ? 2005"
Fire at 10:15am every day during the year 2005
"0 * 14 * * ?"
Fire every minute starting at 2pm and ending at 2:59pm, every day
"0 0/5 14 * * ?"
Fire every 5 minutes starting at 2pm and ending at 2:55pm, every day
"0 0/5 14,18 * * ?"
Fire every 5 minutes starting at 2pm and ending at 2:55pm, AND fire every 5 minutes starting at 6pm and ending at 6:55pm, every day
"0 0-5 14 * * ?"
Fire every minute starting at 2pm and ending at 2:05pm, every day
"0 10,44 14 ? 3 WED"
Fire at 2:10pm and at 2:44pm every Wednesday in the month of March.
"0 15 10 ? * MON-FRI"
Fire at 10:15am every Monday, Tuesday, Wednesday, Thursday and Friday
"0 15 10 15 * ?"
Fire at 10:15am on the 15th day of every month
"0 15 10 L * ?"
Fire at 10:15am on the last day of every month
"0 15 10 ? * 6L"
Fire at 10:15am on the last Friday of every month
"0 15 10 ? * 6L"
Fire at 10:15am on the last Friday of every month
"0 15 10 ? * 6L 2002-2005"
Fire at 10:15am on every last friday of every month during the years 2002, 2003, 2004 and 2005
"0 15 10 ? * 6#3"
Fire at 10:15am on the third Friday of every month

分享到:
评论
1 楼 wingware 2008-11-14  
我的文章,转载到这里了~
呵呵~

相关推荐

Global site tag (gtag.js) - Google Analytics