Quartz+spring任务调度

本文介绍了如何在Spring项目中集成Quartz进行任务调度,包括从官网下载Quartz库,执行数据库脚本,编写QuartzService接口及其实现类,配置quartz-service.xml,设置quartz.properties文件,以及部署启动后在数据库中查看Quartz相关表的信息。
摘要由CSDN通过智能技术生成
一、到官网下载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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值