spring整合quartz demo

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:[email protected]">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:[email protected]">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 *

时间: 2024-08-30 17:29:37

spring整合quartz demo的相关文章

Spring整合Quartz实现定时任务调度

一. 核心类 1. Job: 表示一个工作, 具体的业务处理都在这里. 2. JobDetail: 表示一个具体的可执行的调度程序. 3. Trigger: 用于调度参数的配置(什么时候去调用Job). 4. Scheduler: 表示一个调度容器, 容器中有一个线程池, 用来并行调度执行每个作业, 一个调度容器中可以注册多个JobDetail和Trigger. 二. 整合spring 1. 代码结构图: 2. applicationContext.xml <?xml version="1

spring整合quartz并持久化

spring整合quartz有两种方式: 一.常见是使用配置文件,将定时任务保存到内存中 简单示例: <!-- 短信催还提醒任务调度 --> <bean id="overdueRecall" class="com.sursen.souba.ddlibserve.quartz.OverdueRecallTimerTask" /> <!--定义定时执行overdueRecallTimerTask 这个bean中的overdueRecall(

Spring整合Quartz定时发送邮件

功能描述:刚开始接触Quartz,试着用Quartz整合spring实现每隔一分钟发送一封邮件连续发送10次 核心jar: 邮件发送:commons-email-1.2.jar mail.jar(必须的) quartz:quartz-all-1.8.3.jar quartz-all-1.8.3/lib/下所有jar spring:spring-context-support.ajr(必须的) 只贴出核心代码: Email发送:使用apache commons-email跟mail package

Spring quartz Job不能依赖注入,Spring整合quartz Job任务不能注入

Spring quartz Job不能依赖注入,Spring整合quartz Job任务不能注入 Spring4整合quartz2.2.3中Job任务使用@Autowired不能注入 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ©Copyright 蕃薯耀 2017年9月6日 http://ww

使用Spring整合Quartz轻松完成定时任务

一.背景 上次我们介绍了如何使用Spring Task进行完成定时任务的编写,这次我们使用Spring整合Quartz的方式来再一次实现定时任务的开发,以下奉上开发步骤及注意事项等. 二.开发环境及必须jar包依赖 1.开发环境 Spring 4.2.6.RELEASE Maven 3.3.9 Jdk 1.7 Idea 15.04 2.必不可少jar包依赖 1 <dependency> 2 <groupId>org.springframework</groupId> 3

Spring整合Quartz定时任务 在集群、分布式系统中的应用(Mysql数据库环境)

Spring整合Quartz定时任务 在集群.分布式系统中的应用(Mysql数据库环境) 转载:http://www.cnblogs.com/jiafuwei/p/6145280.html 单个Quartz实例能给予你很好的Job调度能力,但它不能满足典型的企业需求,如可伸缩性.高可靠性满足.假如你需要故障转移的能力并能运行日益增多的 Job,Quartz集群势必成为你应用的一部分了.使用 Quartz 的集群能力可以更好的支持你的业务需求,并且即使是其中一台机器在最糟的时间崩溃了也能确保所有的

spring整合quartz时间任务调度框架

spring整合quartz框架 1.创建maven工程 2.导入jar包(pom.xml) <dependencies> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version> </dependency> <dependency&

java 框架-spring 整合 quartz 框架 service层 注入不了job 类

    1.  spring  + quartz  启动 停止 添加job 功能  一 maven添加quartz  的jar 二 代码区 applicationContext.xml  导入 quartz.xml   <import resource="classpath:spring/quartz.xml"/> <?xml version="1.0" encoding="UTF-8"?> <beans xmlns

Spring整合Quartz实现持久化、动态设定时间

一.spring整合 网上一搜有很多整合的方式,这里我采用了其中的一种(暂时还没有对其他的方法研究过). 对于spring的整合其中的任务,spring提供了几个类.接口(这些类都实现了Job接口): org.springframework.scheduling.quartz.QuartzJobBean org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean.MethodInvokingJob org.sp