Quartz Spring 定时任务

项目结构:



MyJob就是我们要定时执行的任务:

/**
 * 定时执行的任务
 * @author hulujie
 *
 */
public class MyJob   {
	
	public void executeJob() {
		System.out.println("executeJob执行啦。。。");
	}
}


quartz-context.xml的相关配置:

<?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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.2.xsd">

	<bean name="quartzScheduler"
		class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				<ref bean="myJobbtrigger"/>
			</list>
		</property>
	</bean>
	
	
	<bean id="myJob" class="com.wizard.timer.MyJob"></bean>
	<bean id="myJobDetail"
		class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject" ref="myJob" />
   		 <property name="targetMethod" value="executeJob" />
	</bean>

    <bean id="myJobbtrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="myJobDetail"/>
        <property name="cronExpression" value="0/5 * * * * ? "/> <!-- 每5秒执行一次 -->
    </bean>
    
</beans>    

servlet-context.xml配置:

<?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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<!-- 写在了web.xml里面 不然会触发两次
	第一次:web容器启动的时候,读取servlet-context.xml文件时,会加载一次。 
	第二次:Spring本身会加载servlet-context.xml一次。 

		<import resource="quartz-context.xml" />
	 --> 

	<bean id="jspViewResolver" 
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views" />
		<property name="suffix" value=".jsp" />
	</bean>

	<mvc:annotation-driven />

</beans>

web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>springmvcQuartz</display-name>
  
  	<listener>
		<listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>
	
	<context-param>
    <param-name>contextConfigLocation </param-name>
    <param-value>/WEB-INF/conf/servlet-context.xml,/WEB-INF/conf/quartz-context.xml</param-value> 
	</context-param>
	
	<servlet>
		<servlet-name>appServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/conf/servlet-context.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>appServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
 
</web-app>

cronExpression表达式一般分为7部分


7 个字段并区别顺序 0-59 0-59 小时 0-23 月内日期 1-31 1-12 或者 JAN-DEC 周内日期 1-7 或者 SUN-SAT ( 可选字段 ) 留空或者 1970-2099 并且通过特殊字符表示特殊意义 具体为下

    斜线(/)字符表示增量值例如在秒字段中"5/15"代表从第5秒开始15秒一次


    问号(?)字符和字母L字符只有在月内日期和周内日期字段中可用问号表示这个字段不包含具体值所以如果指定月内日期可以在周内日期字段中插入"?"表示周内日期值无关紧要这里有个很蛋疼的设定,无关Quartz,而是Spring集成Quartz后,它自己加的一个约束,那就是:日期(1-31)和星期(SUN-SAT)两者,必须有一个是问号(?),系统在启动的时候,Spring会检查表达式,如果不符合它的规则,就会抛异常。所以在使用的时候这个地方一定要注意,而这个在Linux上执行Cron是没有这个限制的。


    字母L字符是last的缩写放在月内日期字段中表示安排在当月最后一天执行在周内日期字段中如果"L"单独存在,就等于"7"否则代表当月内周内日期的最后一个实例所以"0L"表示安排在当月的最后一个星期日执行


    字母(W)字符把执行安排在最靠近指定值的工作日"1W"放在月内日期字段中表示把执行安排在当月的第一个工作日内


    井号(#)字符为给定月份指定具体的工作日实例"MON#2"放在周内日期字段中表示把任务安排在当月的第二个星期一


    星号(*)字符是通配字符,表示该字段可以接受任何可能的值表达式例子。


     例子:
     "0 0 08 * * ?"  每天上午 8 点触发
     "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 点到下午 2:59 期间的每 1 分钟触发
     "0 0/5 14 * * ?"  在每天下午 2 点到下午 2:55 期间的每 5 分钟触发
     "0 0/5 14,18 * * ?"  在每天下午 2 点到 2:55 期间和下午 6 点到 6:55 期间的每 5 分钟触发
     "0 0-5 14 * * ?"  在每天下午 2 点到下午 2:05 期间的每 1 分钟触发
     "0 10,44 14 ? 3 WED"  每年三月的星期三的下午 2:10 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 2009-2019" 2009 年至 2019 年的每月的最后一个星期五上午 10:15 触发
     "0 15 10 ? * 6#3"  每月的第三个星期五上午 10:15 触发


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值