实战-定时任务实现quartz
web.xml应用程序入口配置
<!-- 应用程序Spring上下文配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:applicationContext*.xml,
classpath*:applicationContext-SysManage.xml,
</param-value>
</context-param>
<!-- spring上下文加载监听器 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 声明 Spring MVC DispatcherServlet -->
<servlet>
<servlet-name>springDispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
applicationContext.xml spring配置文件配置
<context:annotation-config /> <!-- 下面这句话表示,让spring自动去扫描以***开头的包的组件,然后自动完成组装 --> <context:component-scan base-package="***" /> <!-- 配置定时任务 --> <!-- 定义调度工厂Bean,并配置触发器 --> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref local="createAutoSendParamsTableTask" /> </list> </property> </bean> <!-- 设置触发器属性,触发时间 --> <bean id="createAutoSendParamsTableTask" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"> <ref bean="AutoSendParamsTableQuartz" /> </property> <property name="cronExpression"> <span style="white-space:pre"> </span><!-- 每日00:10:00 开始,每分钟执行 --> <value>0 * 10 * * ?</value> </property> </bean> <!-- targetObject要执行的目标类,targetMethod要执行的方法 --> <!-- AutoTask类上加@Component注解即可 --> <bean id="AutoSendParamsTableQuartz" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject"> <ref bean="autoTask" /> </property> <property name="targetMethod"> <value>run</value> </property> <property name="concurrent" value="false"/> </bean>
AutoTask类
@Component
public class AutoTask {
public void run(){
System.out.println("task........"+new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒").format(new Date()));
}
}
输出
要注意quartz版本与spring版本兼容的问题
quartz CronExpression表达式的一些参考写法
0 0 12 * * ?---------------在每天中午12:00触发
0 15 10 ? * *---------------每天上午10:15 触发
0 15 10 * * ?---------------每天上午10:15 触发
0 15 10 * * ? *---------------每天上午10:15 触发
0 15 10 * * ? 2005---------------在2005年中的每天上午10:15 触发
0 * 14 * * ?---------------每天在下午2:00至2:59之间每分钟触发一次
0 0/5 14 * * ?---------------每天在下午2:00至2:59之间每5分钟触发一次
0 0/5 14,18 * * ?---------------每天在下午2:00至2:59和6:00至6:59之间的每5分钟触发一次
0 0-5 14 * * ?---------------每天在下午2:00至2:05之间每分钟触发一次
0 10,44 14 ? 3 WED---------------每三月份的星期三在下午2:00和2:44时触发
0 15 10 ? * MON-FRI---------------从星期一至星期五的每天上午10:15触发
0 15 10 15 * ?---------------在每个月的每15天的上午10:15触发
0 15 10 L * ?---------------在每个月的最后一天的上午10:15触发
0 15 10 ? * 6L---------------在每个月的最后一个星期五的上午10:15触发
0 15 10 ? * 6L 2002-2005---------------在2002, 2003, 2004 and 2005年的每个月的最后一个星期五的上午10:15触发
0 15 10 ? * 6#3---------------在每个月的第三个星期五的上午10:15触发
0 0 12 1/5 * ?---------------从每月的第一天起每过5天的中午12:00时触发
0 11 11 11 11 ?---------------在每个11月11日的上午11:11时触发.

浙公网安备 33010602011771号