使用的版本Spring4.04+Quartz2.2.3,关于jar包自行下载。

详细需要以下几个步骤来完成:

1.  定义要执行的Job类

2.  定义quartz的配置文件applicationContext-quartz.xml

2.1 定义要调用的对象和对象的方法

2.2 在触发器中配置使用该方法的时间

2.3 在总管类中添加该任务

3.  定义执行的任务的时间配置文件

4. 在拦截器中添加要扫描的包

5. 将quartz配置文件添加到我们的spring容器的配置文件applicationContext.xml中

6. 启动tomcat

下面以具体实例来演示:

1. 定义要执行的Job类 TestJob.java

注意加上注解标记该类为Component组件,这样方便自动装配到spring容器中管理。

package com.crm.scheduler;

import java.text.SimpleDateFormat;
import java.util.Date; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; @Component("testJobComponent")
public class TestJob { //定义是否
@Value("${crmbi.cronExpression.isDoTestJob}")
private boolean isDoTestJob = false;
public void execute(){ if(!isDoTestJob){
return ;
} SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("我是定时任务类,现在的执行时间是" + sdf.format(new Date())); }
}

2. 定义quartz的配置文件

2.1 定义要调用的对象和对象的方法,在这里对象就是上面的新建的testJobComponnent。

    <!-- 定时器2:测试quartz类的使用方法 -->
<!-- 定义调用的对象及对象中的方法 -->
<bean id="defTestJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="testJobComponent" />
<property name="targetMethod" value="execute" />
<property name="concurrent" value="false" /> <!-- 是否支持并发 -->
</bean>

2.2 定义触发器并配置定时任务执行的时间

    <!-- 触发器:定义出发器执行的脚本的时间  -->
<bean id="triggerTestJob" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="defTestJob" />
<property name="cronExpression">
<value>${crmbi.cronExpression.testJob}</value>
</property>
</bean>

2.3 在总管理中添加该任务

    <!-- 总管理类,启动触发器的配置, 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
<bean id="startQuartz" lazy-init='false' autowire="no"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="triggerTestjob" />
</list>
</property>
<property name="autoStartup" value="true" />
<property name="startupDelay" value="30"/>
</bean>

最后展示下完成的配置文件applicationContext-quartz.xml如下,其中包含了2个定时任务类,triggerTestJob为我们新增的任务信息:

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util"
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
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> <!-- Quartz common config--> <!-- 总管理类,启动触发器的配置, 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
<bean id="startQuartz" lazy-init='false' autowire="no"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="runSyncHive2OracleJob" />
<ref bean="triggerTestjob" />
</list>
</property>
<property name="autoStartup" value="true" />
<property name="startupDelay" value="30"/>
</bean> <!-- 定时器1: 同步hive数据到oracle -->
<!-- 定义执行的对象及对象中的方法 -->
<bean id="defineSyncHive2OracleJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="syncHive2OracleComponent" />
<property name="targetMethod" value="execute" />
<property name="concurrent" value="false" /> <!-- 指是否并行执行 -->
</bean>
<!-- 定义触发器的时间 -->
<bean id="runSyncHive2OracleJob" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="defineSyncHive2OracleJob" />
<property name="cronExpression">
<value>${crmbi.cronExpression.syncHive2OracleJob}</value>
</property>
</bean> <!-- 定时器2:测试quartz类的使用方法 -->
<!-- 定义调用的对象及对象中的方法 -->
<bean id="defTestJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="testJobComponent" />
<property name="targetMethod" value="execute" />
<property name="concurrent" value="false" /> <!-- 是否支持并发 -->
</bean>
<!-- 触发器:定义出发器执行的脚本的时间 -->
<bean id="triggerTestJob" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="defTestJob" />
<property name="cronExpression">
<value>${crmbi.cronExpression.testJob}</value>
</property>
</bean> </beans>

3. 定义执行任务的时间配置文件config.properties.

注意这里的crmbi.cronExpression.isDotestJob用来控制是否执行任务的开关

# 3:00 every day
crmbi.cronExpression.syncHive2OracleJob=0 0 3 * * ?
crmbi.cronExpression.isDoSyncHive2Oracle=true # each 5 minuts
crmbi.cronExpression.testJob=0 0/5 * * * ?
crmbi.cronExpression.isDoTestJob=true

4. 在拦截器中添加要扫描的包,这里加入了com.crm.scheduler包和config.properties文件的扫描。

<!----这个applicationContext-dao.xml文件中---->
<context:component-scan base-package="com.crm.dao"/>
<context:component-scan base-package="com.crm.scheduler"/> <context:property-placeholder location="classpath:jdbc.properties" ignore-unresolvable="true"/>
<context:property-placeholder location="classpath:config.properties" ignore-unresolvable="true" />

5.将quartz的配置文件引入到spring容器中。

<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 数据访问层配置 -->
<import resource="classpath:spring/applicationContext-dao.xml" /> <!--服务层配置 -->
<import resource="classpath:spring/applicationContext-service.xml" /> <!-- 定时任务配置文件 -->
<import resource="classpath:spring/applicationContext-quartz.xml" /> </beans>

6. 最后启动tomcat, 等待一段时间后,可以看到控制台输出如下:

[BI-CONSOLE] 2016-08-11 18:03:44.939 INFO RequestMappingHandlerMapping.registerHandlerMethod(197) | Mapped "{[/user/showUser],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.crm.action.system.UserController.showUser(org.springframework.ui.Model,java.lang.String,java.lang.String)
[BI-CONSOLE] 2016-08-11 18:03:44.940 INFO RequestMappingHandlerMapping.registerHandlerMethod(197) | Mapped "{[/user/updateUserPwd],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.util.Map<java.lang.String, java.lang.Object> com.crm.action.system.UserController.updateUserPwd(java.lang.String,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
[BI-CONSOLE] 2016-08-11 18:03:44.940 INFO RequestMappingHandlerMapping.registerHandlerMethod(197) | Mapped "{[/user/userSave],methods=[POST],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.util.Map<java.lang.String, java.lang.Object> com.crm.action.system.UserController.saveUser(org.springframework.ui.Model,com.crm.entity.system.MUser)
[BI-CONSOLE] 2016-08-11 18:03:45.533 INFO SimpleUrlHandlerMapping.registerHandler(302) | Root mapping to handler of type [class org.springframework.web.servlet.mvc.ParameterizableViewController]
[BI-CONSOLE] 2016-08-11 18:03:45.540 INFO SimpleUrlHandlerMapping.registerHandler(315) | Mapped URL path [/**] onto handler 'org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler#0'
[BI-CONSOLE] 2016-08-11 18:03:45.581 INFO SimpleUrlHandlerMapping.registerHandler(315) | Mapped URL path [/resources/**] onto handler 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0'
[BI-CONSOLE] 2016-08-11 18:03:45.863 INFO DispatcherServlet.initServletBean(498) | FrameworkServlet 'crmbi': initialization completed in 1255 ms
八月 11, 2016 6:03:45 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-bio-8080"]
八月 11, 2016 6:03:45 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["ajp-bio-8009"]
八月 11, 2016 6:03:45 下午 org.apache.catalina.startup.Catalina start
信息: Server startup in 14949 ms
[BI-CONSOLE] 2016-08-11 18:04:14.583 INFO SchedulerFactoryBean.run(667) | Starting Quartz Scheduler now, after delay of 30 seconds
[BI-CONSOLE] 2016-08-11 18:04:14.584 INFO QuartzScheduler.start(575) | Scheduler startQuartz_$_NON_CLUSTERED started.
我是定时任务类,现在的执行时间是2016-08-11 18:05:00
我是定时任务类,现在的执行时间是2016-08-11 18:10:00

7. 附上项目的文档结构图如下:

定时任务-在spring中配置quartz的更多相关文章

  1. spring中配置quartz调用两次及项目日志log4j不能每天生成日志解决方法

    在quartz中配置了一个方法运行时会连续调用两次,是因为加载两次,只需在tomcat的server.xml中修改配置 <Host name="www.xx.cn" appB ...

  2. spring 中使用quartz实现定时任务

    一般开发系统,使用定时任务非常常见.当然也可以用Java实现.比如定时器.大致如下: 1: public static void main(String[] args) { 2: Timer time ...

  3. 浅谈Spring中的Quartz配置

    浅谈Spring中的Quartz配置 2009-06-26 14:04 樊凯 博客园 字号:T | T Quartz是一个强大的企业级任务调度框架,Spring中继承并简化了Quartz,下面就看看在 ...

  4. Spring中使用Quartz之MethodInvokingJobDetailFactoryBean配置任务

    Quartz是一个强大的企业级任务调度框架,Spring中继承并简化了Quartz. Spring中使用Quartz的3种方法(MethodInvokingJobDetailFactoryBean,i ...

  5. 在spring中实现quartz的动态调度(开始、暂停、停止等)

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/fantasic_van/article/details/74942062 需求: 需要在页面设定某个 ...

  6. Spring 中使用Quartz实现任务调度

    前言:Spring中使用Quartz 有两种方式,一种是继承特定的基类:org.springframework.scheduling.quartz.QuartzJobBean,另一种则不需要,(推荐使 ...

  7. 10 -- 深入使用Spring -- 5...2 在Spring中使用Quartz

    10.5.2 在Spring中使用Quartz Spring 的任务调度抽象层简化了任务调度,在Quartz基础上提供了更好的调度抽象.本系统使用Quartz框架来完成任务调度,创建Quartz的作业 ...

  8. 在Spring中配置SQL server 2000

    前言 Lz主要目的是在Spring中配置SQL server 2000数据库,但实现目的的过程中参差着许多SQL server 2000的知识,也包罗在本文记载下来!(Lz为什么要去搞sql serv ...

  9. spring中配置监听队列的MQ

    一.spring中配置监听队列的MQ相关信息注:${}是读取propertites文件的常量,这里忽略.绿色部分配置在接收和发送端都要配置.  <bean id="axx" ...

随机推荐

  1. XSD笔记

    XML Schema 是基于 XML 的 DTD 替代者. XML Schema 可描述 XML 文档的结构. XML Schema 语言也可作为 XSD(XML Schema Definition) ...

  2. Django admin coercing to Unicode: need string or buffer, tuple found

    见 http://stackoverflow.com/questions/29762306/django-admin-coercing-to-unicode-need-string-or-buffer ...

  3. PhyLab2.0设计分析阶段任务大纲(α)

    任务概述 由于接手软剑攻城队的PhyLab项目,省去了用户需求分析.团队编码规范.用户界面原型设计和后端逻辑设计的大部分环节,因此前期的主要任务落在了用户使用反馈.功能优化增改方向.用户体验优化以及源 ...

  4. 高性能JavaScript(您值得一看)

    众所周知浏览器是使用单进程处理UI更新和JavaScript运行等多个任务的,而同一时间只能有一个任务被执行,如此说来,JavaScript运行了多长时间就意味着用户得等待浏览器响应需要花多久时间. ...

  5. 八数码问题(紫薯P199)

    #include <algorithm> #include <cstdio> #include <cstring> #include <iostream> ...

  6. java编程思想-接口总结

    "确定接口是理想选择,因而应该总是选择接口而不是具体的类."这其实是一种诱饵.当然,对于创建类,几乎在任何时刻,都可以替代为创建一个接口和一个工厂. 许多人都掉进了这种诱惑的陷阱, ...

  7. Yocto开发笔记之《Tip-bitbake常用命令》(QQ交流群:519230208)

    开了一个交流群,欢迎爱好者和开发者一起交流,转载请注明出处. QQ群:519230208,为避免广告骚扰,申请时请注明 “开发者” 字样 =============================== ...

  8. UML的目标

    http://zhidao.baidu.com/link?url=ghQvzG70vSCSLyQcrHDTd7xt1aSWBR73lPIMxBCEPo1ktkq9cQ3EE9TXX1mZyHINkVA ...

  9. SVN Access to ‘/svn/Test/!svn/me’ forbidden,不能更新解决办法

    今天上班,使用公司配置的电脑进行项目的更新.SVN报如下错误, SVN Access to '/svn/Test/!svn/me' forbidden,不能更新解决办法 很有意思: 开始以为自己的SV ...

  10. 【经典】C++&RPG对战游戏

    博文背景: 还记大二上学期的时候看的这个C++&RPG游戏(博主大一下学期自学的php,涵盖oop内容),一个外校的同学他们大一学的C++,大二初期C++实训要求做一个程序填空,就是这个 RP ...