Quartz Scheduler(2.2.1) - Integration with Spring

1. maven 依赖:

<properties>
<spring.version>3.2.3.RELEASE</spring.version>
<quartz.version>2.2.1</quartz.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>${quartz.version}</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>${quartz.version}</version>
</dependency> <dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.6</version>
<scope>provided</scope>
</dependency> <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
</dependency> </dependencies>

2. 定义任务类:

package com.huey.hello.quartz;

import java.util.Date;

import org.quartz.JobExecutionException;

import com.huey.hello.quartz.utils.DateUtils;

public class HelloJob {

    public void sayHello() throws JobExecutionException {
System.out.println("hello " + DateUtils.dateToStr(new Date()));
} }

3. Spring 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- 配置任务实例 -->
<bean id="helloJob" class="com.huey.hello.quartz.HelloJob" /> <!-- 配置 JobDetail -->
<bean id="myJobDetailFactory" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 指定任务 -->
<property name="targetObject" ref="helloJob" />
<!-- 指定执行的方法 -->
<property name="targetMethod" value="sayHello" />
</bean> <!-- 配置触发器 -->
<bean id="myTriggerFactory" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<!-- 指定 JobDetail -->
<property name="jobDetail" ref="myJobDetailFactory" />
<!-- 指定 cron 表达式 -->
<property name="cronExpression" value="0/5 * * * * ?" />
</bean> <!-- 配置 scheduler 工厂 -->
<bean id="schedulerFactory" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="myTriggerFactory"/>
</list>
</property>
<!-- 是否自动启动 -->
<property name="autoStartup" value="true" />
</bean> </beans>
上一篇:SQL 字符串分割表函数


下一篇:Codeforces 549C The Game Of Parity(博弈)