最新 Spring 4.2.2 集成 Quartz Scheduler 2.2.2 任务调度示例

本文将演示如何通过 Spring 使用 Quartz Scheduler 进行任务调度。Spring 为简化 Quartz 的操作提供了相关支持类。
本文示例使用的相关工具如下:
  • Spring 4.2.2 (发布于 2015 年 10 月)
  • Quartz Scheduler 2.2.2 (发布于 2015 年 10 月)
  • Maven 3
  • JDK 1.7
  • Eclipse Luna Service Release 1 (4.4.1)

步骤 1:新建 Maven 项目

新建maven项目.png
这一步如果不知道怎么做可以参考博客《 使用 Eclipse 的 Maven 2 插件开发一个 JEE 项目》。

步骤 2:第三方依赖包的引入

Maven 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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>settle</groupId>
  <artifactId>spring-quartz</artifactId>
  <version>1.0.0</version>
  <packaging>jar</packaging>
  <name>spring-quartz</name>
  <url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<springframework.version>4.2.2.RELEASE</springframework.version>
		<quartz.version>2.2.2</quartz.version>
	</properties>

	<dependencies>
		<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <!-- Transaction dependency is required with Quartz integration -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${springframework.version}</version>
        </dependency>
         
        <!-- Quartz framework -->
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>${quartz.version}</version>
        </dependency>
	</dependencies>
</project>

步骤 3:Quartz Scheduler 配置作业

有两种方式在 Spring 中使用 Quartz 来配置一个作业。
A:使用 MethodInvokingJobDetailFactoryBean
这种方式在你想要调用特定 bean 的一个方法的时候很是方便,比另一种方法要简单的多。
    <!-- For times when you just need to invoke a method on a specific object -->
    <bean id="simpleJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="myBean" />
        <property name="targetMethod" value="printMessage" />
    </bean>

以上作业配置简单调用了 myBeanprintMessage 方法, myBean 是一个简单的 POJO。
B:使用 JobDetailFactoryBean
如果你需要更高级的设置,需要给作业传递数据,想更加灵活的话就使用这种方式。
    <!-- For times when you need more complex processing, passing data to the scheduled job -->
    <bean name="firstComplexJobDetail"    class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
        <property name="job
  • 18
    点赞
  • 67
    收藏
    觉得还不错? 一键收藏
  • 14
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值