码迷,mamicode.com
首页 > 编程语言 > 详细

spring整合quartz demo

时间:2015-08-20 13:29:40      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:

pom.xml:


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.jin.quartz</groupId>
  <artifactId>myapp-demo</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>myapp-demo Maven Webapp</name>
  <url>http://maven.apache.org</url>
   <properties>  
        <springframework.version>3.0.5.RELEASE</springframework.version>  
    </properties>  
  
    <dependencies>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-context</artifactId>  
            <version>${springframework.version}</version>  
        </dependency>  
  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-context-support</artifactId>  
            <version>${springframework.version}</version>  
        </dependency>  
  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-tx</artifactId>  
            <version>${springframework.version}</version>  
        </dependency>  
  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-web</artifactId>  
            <version>${springframework.version}</version>  
        </dependency>  
  
        <dependency>  
            <groupId>org.quartz-scheduler</groupId>  
            <artifactId>quartz</artifactId>  
            <version>1.8.5</version>  
        </dependency>  
    </dependencies>  
  
    <build>  
        <finalName>${project.artifactId}</finalName>  
        <plugins>  
            <plugin>  
                <groupId>org.mortbay.jetty</groupId>  
                <artifactId>jetty-maven-plugin</artifactId>  
                <version>7.5.4.v20111024</version>  
                <configuration>  
                    <scanIntervalSeconds>10</scanIntervalSeconds>  
                    <webApp>  
                        <contextPath>/${project.artifactId}</contextPath>  
                    </webApp>  
                </configuration>  
            </plugin>  
        </plugins>  
    </build>  
</project>
整合:


第一种方式:

任务类继承QuartzJobBean。感觉不常用。


第二种方式:

第二种方式的第一种配置:

在配置文件中配置任务类和要执行的方法。

任务类:


/**
 * Copyright 2013-2015
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */	  

package com.jin.quartz;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 任务类,
 * <p/>
 * 
 * @author <a href="mailto:978954852@qq.com">jinxf</a>
 * @version  Date: 2015年8月20日 上午11:00:39
 * @serial 1.0
 * @since 2015年8月20日 上午11:00:39
 */

public class QuartzTest {   
	public void Show(){
		Calendar cal=Calendar.getInstance();
		long date=cal.getTime().getTime();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
		String result=sdf.format(date);
		System.out.println(result);
		System.out.println("Spring Quartz Test");
	}
	
	public static void main(String args[]){
		new QuartzTest().Show();
	}
}
applicationContext.xml:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">  
<beans>       
	<!-- 要调用的工作类 -->
	<bean id="MyJob" class="com.jin.quartz.QuartzTest"></bean>
	<!-- 定义调用对象和调用对象的方法 --> 
	<bean id="JobWork" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
	<!-- 调用的类 -->
	<property name="targetObject">
		<ref bean="MyJob"/>
	</property>
	<!-- 调用类中的方法 -->
	<property name="targetMethod">
		<value>Show</value>
	</property>
	</bean>
	
	<!-- 定义触发时间 -->
	<bean id="JobTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail">
			<ref bean="JobWork"/>
		</property>
		<!-- cron表达式 -->
		<property name="cronExpression">
			<value>10,15,20,25,30,35,40,45,50,55 * * * * ?</value>
		</property>
	</bean>
	
	<bean id="JobSchedule" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				<ref bean="JobTrigger"/>
			</list>
		</property>
	</bean>
	
</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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Grade</display-name>
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
  
    <!-- Spring配置文件 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>
	
	<!-- Spring监听器 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
</web-app>
测试:



/**
 * Copyright 2013-2015
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */	  

package com.jin.quartz;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 测试
 * <p/>
 * 
 * @author <a href="mailto:978954852@qq.com">jinxf</a>
 * @version  Date: 2015年8月20日 上午11:14:54
 * @serial 1.0
 * @since 2015年8月20日 上午11:14:54
 */

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("Spring Quartz test start...");
		ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
//		如果配置文件中将startQuertz bean的lazy-init设置为false 则不用实例化
		System.out.println("Spring Quartz test end...");
	}
}

结果:





第二种配置方式:

使用JobDetailBean,任务类必须实现Job接口

applicationContext.xml:

<bean id="myjob" class="org.springframework.scheduling.quartz.JobDetailBean">     
     <property name="name" value="exampleJob"></property>     
     <property name="jobClass" value="com.jin.QuartzTest"></property>    
     <property name="jobDataAsMap">  
<map>  
    <entry key="service"><value>simple is the beat</value></entry>  
</map>  
</property>  
    </bean>





补上一点东西

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 *











spring整合quartz demo

标签:

原文地址:http://my.oschina.net/u/2265860/blog/494976

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!