spring-quartz配置

1.spring2.5版本
2.加载jar包quartz-all-1.6.1.jar
3.配置定时quartz.xml文件:放到项目WEB-INF/
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<!-- Quartz本地Schduler -->
<bean id="localQuartzScheduler"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean"
lazy-init="false">
<!-- Triggers集成 -->
<property name="triggers">
<list>
<ref bean="cronTrigger" />
</list>
</property>
<!-- Quartz配置 -->
<property name="quartzProperties">
<props>
<prop key="org.quartz.threadPool.threadCount">5</prop>
</props>
</property>
<!-- 启动时延期3秒开始任务 -->
<property name="startupDelay" value="3" />
</bean>

<!-- Cron式Trigger定义 -->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="cronJobDetail" />
<!-- 每天的晚上23:59:59开始执行活跃用户日志的生成 -->
<property name="cronExpression" value="59 59 23 ? * *" />
</bean>

<bean id="cronJobDetail"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="cronJob" />
<property name="targetMethod" value="execute" />
<!-- 同一任务在前一次执行未完成而Trigger时间又到时是否并发开始新的执行, 默认为true. -->
<property name="concurrent" value="true" />
</bean>

<bean id="cronJob" class="com.moshou.util.date.ActiveUserTimer" />

</beans>
4.在web.xml中加载quartz.xml文件
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext*.xml,/WEB-INF/area_path.xml,/WEB-INF/quartz.xml
</param-value>
</context-param>

5.
测试类:
import org.springframework.stereotype.Component;

import com.moshou.comm.cache.firstlevelcache.ActiveUserCache;

/**
* @author admin
* @see 测试用例
*
*/
@Component
public class ActiveUserTimer{

public void execute()
{
System.out.println("开始生成活跃用户日志");
ActiveUserCache.createActUserLog();//此处可以随便执行些什么以便测试
}
}
6.
<!-- Cron式Trigger定义 -->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="cronJobDetail" />
<!-- 每天的晚上23:59:59开始执行活跃用户日志的生成 -->
<property name="cronExpression" value="59 59 23 ? * *" />
</bean>
说明:<property name="cronExpression" value="59 59 23 ? * *" />的日期格式,可以根据自己的需求定义.(搜索:Cron式Trigger定义,可找到对应的格式说明.)