Spring3.2.4集成quartz2.2.1定时任务(demo).

在JavaEE系统中,我们会经常用到定时任务,下面是我自己写的一个demo.

前面几篇quartz博客感觉看的还是难懂 于是重新整理下

源码地址:http://pan.baidu.com/s/1BXHv3

1.所需要的jar

Spring3.2.4集成quartz2.2.1定时任务(demo).

使用maven的pom.xml如下

  <dependencies>

<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency> <dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency> <dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.2</version>
</dependency> <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency> <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.21</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.2.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.2.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.2.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.2.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.2.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.3.2.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.2.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
</dependencies>

2.实体bean

package com.shanheyongmu.quartz;

import java.util.Date;

public class Study {
public void doStudy(){
System.out.println("It is"+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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
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-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- bean -->
<bean id="study" class="com.shanheyongmu.quartz" /> <!-- 定义调用对象和调用对象的方法 studyDetail也可以是jobTask -->
<bean id="studyDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 调用的类 -->
<property name="targetObject" ref="study"/>
<!-- 调用类中的方法 -->
<property name="targetMethod" value="doStudy" />
<!-- 是否允许任务并发执行。当值为false时 ,表示必须等到前一个线程处理完毕后才再启动一个新的线程-->
<property name="concurrent" value="false"/>
</bean> <!-- quartz-1.8以前的配置 -->
<!-- <bean id="myjobTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="studyDetail"/>
</property>
<property name="cronExpression"><value>0/1 * * * * ?</value></property>
</bean> --> <!-- quartz-2.x的配置 -->
<!-- 定义触发时间 -->
<bean id="myJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail">
<ref bean="studyDetail"/>
</property>
<property name="cronExpression">
<value>0/5 * * * * ?</value>
</property>
</bean> <!-- 总管理类 如果将lazy-init='false'那么容器 启动就会执行调度程序-->
<!-- 如果lazy-init='true',则需要实例化该bean才能执行调度程序-->
<bean name="startQuartz" lazy-init="false" autowire="no" class="org.springframework.scheuling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="myJobTrigger" />
</list>
</property>
<!-- <property name="autoStartup" value="true"/> -->
</bean> <!--下面是另外一种写法-->
<!-- <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<bean class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="cronExpression">
<value>0/5 * * * * ?</value>
</property>
<property name="jobDetail">
  <bean class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="scheduTask"></property>
<property name="targetMethod" value="doTask"></property>
是否允许任务并发执行。当值为false时,表示必须等到前一个线程处理完毕后才再启一个新的线程
<property name="concurrent" value="false"/>
  </bean>
</property>
</bean>
</list>
</property>
</bean> -->
</beans>

4.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value> <!-- maven下这样配置-->
 <param-value>classpath*:applicationContext.xml</param-value>
    </context-param>

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

http://cron.qqe2.com/  这个是在线quartz时间格式的网站

上一篇:JAVA中的Log4j


下一篇:unity3d插件Daikon Forge GUI 中文教程5-高级控件listbox和progress bar的使用