spring4.0+quartz配置定时任务
2016-05-10 16:22
507 查看
作用简介:
作用我就不说了。这个写给我自己看的,方便以后忘记了进行翻阅,这样的代码网上一搜一堆。
jar架包:
spring4.2.4系列包,quartz2.1.7,slf4j,commons-logging。
(1)web.xml配置文件
(2)quartz.xml配置文件
(4)执行结果。
执行第一个定时任务,时间是::Tue May 10 16:31:03 CST 2016
当前线程id:38
执行第二个定时任务,时间是:Tue May 10 16:31:03 CST 2016
当前线程id:39
执行第二个定时任务,时间是:Tue May 10 16:32:03 CST 2016
当前线程id:41
执行第一个定时任务,时间是::Tue May 10 16:32:03 CST 2016
当前线程id:40
结语:没什么含量的代码,仅仅是方便自己以后忘记了可以来看看。
作用我就不说了。这个写给我自己看的,方便以后忘记了进行翻阅,这样的代码网上一搜一堆。
jar架包:
spring4.2.4系列包,quartz2.1.7,slf4j,commons-logging。
(1)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_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>quartz1</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 加载xml文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:*quartz.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
(2)quartz.xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <!-- - Application context definition for JPetStore's business layer. - Contains bean references to the transaction manager and to the DAOs in - dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation"). --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <!-- 要执行的工作类 --> <bean id="quartzJob" class="com.test.quartz.JobClass"></bean><!-- 可以有多个,工作类可以继续添加 --> <!-- 调用工作类中的方法 --> <bean id="firstJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!-- 要调用的类 --> <property name="targetObject"> <ref bean="quartzJob"/> </property> <!-- 要调用类中的哪个方法 --> <property name="targetMethod"> <value>firstJob</value> </property> </bean> <!-- 定义上面这个方法的执行周期 --> <bean id="execTime1" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <!-- 调用firstJob --> <property name="jobDetail"> <ref bean="firstJob" /> </property> <!-- cron表达式 每分钟的03秒时执行 --> <property name="cronExpression"> <value>3 * * * * ? </value> </property> </bean> <!-- 启动所有的定时任务 --> <bean id="startAllJob" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="execTime1"/> <ref bean="execTime2"/> <!-- 此处继续添加要执行的定时任务 --> </list> </property> </bean> <!-- 第二个任务 --> <bean id="twoJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!-- 要调用的类 --> <property name="targetObject"> <ref bean="quartzJob"/> </property> <!-- 要调用类中的哪个方法 --> <property name="targetMethod"> <value>twoJob</value> </property> </bean> <bean id="execTime2" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <!-- 调用firstJob --> <property name="jobDetail"> <ref bean="twoJob" /> </property> <!-- cron表达式 --> <property name="cronExpression"> <value>3 * * * * ? </value> </property> </bean> </beans>(3)工作类JobClass.java
package com.test.quartz; import java.util.Date; public class JobClass { /** * 第一个定时任务 */ public void firstJob(){ System.out.println("执行第一个定时任务,时间是::"+new Date()); System.out.println("当前线程id:"+Thread.currentThread().getId()); } /** * 第二个定时任务 */ public void twoJob(){ System.out.println("执行第二个定时任务,时间是:"+new Date()); System.out.println("当前线程id:"+Thread.currentThread().getId()); } }
(4)执行结果。
执行第一个定时任务,时间是::Tue May 10 16:31:03 CST 2016
当前线程id:38
执行第二个定时任务,时间是:Tue May 10 16:31:03 CST 2016
当前线程id:39
执行第二个定时任务,时间是:Tue May 10 16:32:03 CST 2016
当前线程id:41
执行第一个定时任务,时间是::Tue May 10 16:32:03 CST 2016
当前线程id:40
结语:没什么含量的代码,仅仅是方便自己以后忘记了可以来看看。
相关文章推荐
- spring发布和接收定制的事件(spring事件传播)
- Java中实例变量和局部变量未初始化的情况
- eclipse中中文字体过小
- Maven+SpringMVC+Dubbo 简单的入门demo配置
- Spring基础知识(2)
- eclipse下如何打开ftl文件
- 软件可扩展性:来自星巴克的经验
- java 注释规则个人汇总
- java synchronized类锁详解
- SpringMVC上传文件进度显示
- Java版 中缀表达式转换为后缀表达式并求结果
- Spring MVC框架搭建及运行流程
- spring+velocity+javamail发送模版邮件
- struts2 中dispatcher、redirect和chain三种result type的使用区别
- No validator could be found for type: java.lang.Integer.错误
- java 数据装箱及练习-对一个字符串中的数字进行排序
- java类
- 《java入门第一季》之Arrays类前传(排序案例以二分查找注意的问题)
- 《java入门第一季》之Arrays类前传(排序案例以二分查找注意的问题)
- Maven构建springMVC+mybites