• 初涉Quartz


    1、首先需要导入包,必须导入的包如下:
          quartz-1.8.5.jar 
      commons-logging.jar 
      spring-core-3.0.5.RELEASE.jar 
      spring-beans-3.0.5.RELEASE.jar 
      spring-context-3.0.5.RELEASE.jar 
      spring-context-support-3.0.5.RELEASE.jar 
      spring-asm-3.0.5.RELEASE.jar 
      spring-expression-3.0.5.RELEASE.jar 
      spring.transaction-3.0.5.RELEASE.jar 
      spring-web-3.0.5.RELEASE.jar 

    2、编写java代码,如下:

    package cn.spring.basis;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class MyJob {
    	public void work() {
    		System.out.println(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
    	}
    }
    

    3、在applicationContext.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"
    	xmlns:p="http://www.springframework.org/schema/p"
    	xmlns:aop="http://www.springframework.org/schema/aop" 
    	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/aop 
    	http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    	http://www.springframework.org/schema/tx 
    	http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    	<!-- 使用MethodInvokingJobDetailFactoryBean,任务类可以不实现Job接口,通过targetMethod指定调用方法-->    
    	<!-- 定义目标bean和bean中的方法 -->
    	<bean id="SpringQtzJobMethod" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
    		<property name="targetObject">  
    			<ref bean="myJob"/>  
    		</property>  
    		<property name="targetMethod">  <!-- 要执行的方法名称 -->  
    			<value>work</value>  
    		</property>  
    	</bean>  
    	<!-- ======================== 调度触发器 ======================== -->  
    	<bean id="CronTriggerBean" class="org.springframework.scheduling.quartz.CronTriggerBean">  
    		<property name="jobDetail" ref="SpringQtzJobMethod"></property>  
    		<property name="cronExpression" value="0/5 * * * * ?"></property>  
    	</bean>  
    	<!-- ======================== 调度工厂 ======================== -->  
    	<bean id="SpringJobSchedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
    		<property name="triggers">  
    			<list>  
    				<ref bean="CronTriggerBean"/>  
    			</list>  
    		</property>  
    	</bean>    
    	
    	<bean id="myJob"
    		class="cn.spring.basis.MyJob"></bean>
    </beans>
    

    4、在web.xml文件中配置spring的监听器,如下:

    <listener>
    	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    

    启动tomcat服务器,可以在控制台窗口显示打印:

    在任务触发器中配置自动触发的时间,cronExpression属性的值中的含义如下:

    字段 允许值 允许的特殊字符 
    秒 0-59 , - * / 
    分 0-59 , - * / 
    小时 0-23 , - * / 
    日期 1-31 , - * ? / L W C 
    月份 1-12 或者 JAN-DEC , - * / 
    星期 1-7 或者 SUN-SAT , - * ? / L C # 
    年(可选) 留空, 1970-2099 , - * / 
    表达式意义 
    "0 0 12 * * ?" 每天中午12点触发 
    "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 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发 
    "0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发 
    每天早上6点 
    0 6 * * * 
    每两个小时 
    0 */2 * * * 
    晚上11点到早上8点之间每两个小时,早上八点 
    0 23-7/2,8 * * * 
    每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点 
    0 11 4 * 1-3 
    1月1日早上4点 
    0 4 1 1 * 

  • 相关阅读:
    自愿如此 四 内观
    自愿如此· 三 不做解释
    自愿如此·序言
    排序算法 (08.堆排序)
    2.2 ES6 解构赋值
    2.1 ES6 let 与 const
    ES6学习 (01. 内容概况)
    vue 技术栈进阶 (07. ajax 请求实战)
    vue技术栈进阶(06.状态持久化, 严格模式, 数据双向绑定问题)
    vue技术栈进阶(05. mutations, actions)
  • 原文地址:https://www.cnblogs.com/gyouxu/p/3988767.html
Copyright © 2020-2023  润新知