一、到官网下载quartz相应的压缩包,maven引用如下:
<!-- 添加quartz定时任务包 -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.3</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.2.3</version>
</dependency>
二、将下载的压缩包中目录下docs/dbTables/tables_oracle.sql执行一下,需要根据你所使用的数据库来决定,找对应文件名就对了。
三、在spring中编写quartz调度类,我这只是测试,所以最简单化,代码如下:
3.1 QuartzService接口一枚
package com.zht.quartz;
import java.io.Serializable;
public interface QuartzService extends Serializable{
public String execute(String s);
}
3.2 QuartzServiceImpl实现类一枚
package com.zht.quartz.impl;
import org.springframework.stereotype.Service;
import com.zht.quartz.QuartzService;
@Service("jobService")
public class QuartzServiceImpl implements QuartzService{
/**
*
*/
private static final long serialVersionUID = -4132042294131430814L;
@Override
public String execute(String s) {
return "this is test";
}
}
3.3 quartz调度入口SpringIntegrationQuartz类
package com.zht.quartz.impl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
import com.zht.common.SpringApplicationContext;
import com.zht.quartz.QuartzService;
public class SpringIntegrationQuartz extends QuartzJobBean{
private static final Log LOG = LogFactory.getLog(SpringIntegrationQuartz.class);
@Override
protected void executeInternal(JobExecutionContext arg0)
throws JobExecutionException {
LOG.info("===== start quartz job =====");
QuartzService service = (QuartzService) SpringApplicationContext.getBeanById("jobService");
String result = service.execute("");
System.out.println(result);
}
}
四、在spring项目中添加quartz-service.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"
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.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<!-- 多个配置文件加载,最后load在一起时,一定都要加上ignore-unresolvable="true" -->
<context:property-placeholder location="classpath:quartz.properties" ignore-unresolvable="true"/>
<context:component-scan base-package="com.zht.quartz..*" />
<!-- 配置方式一 使用 JobDetailFactoryBean 如果你需要更高级的设置,需要给作业传递数据,想更加灵活的话就使用这种方式 -->
<bean name="firstComplexJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<description>job详细信息</description>
<!-- 指向job运行的类,也是作为job运行的入口, 关联到一个继承自 QuartzJobBean 的类,它实现了 Quartz 作业接口。
调用到这个作业的时候,它的 executeInternal 将被执行-->
<property name="jobClass" value="com.zht.quartz.impl.SpringIntegrationQuartz" />
<!--jobDataMap允许我们向job传递数据 -->
<property name="jobDataMap">
<map>
<!-- 这里传递的是一个bean,也可以在jobClass中进行入参的封装
<entry key="anotherBean" value-ref="anotherBean