精进Quartz—Spring和Quartz集成详解(三)

欢迎关注我的公众号: Java编程技术乐园。分享技术,一起精进Quartz!

做一个积极的人

编码、改bug、提升自己

我有一个乐园,面向编程,春暖花开!

文章目录

    • 一、Jar包依赖
    • 二、任务类Job
    • 三、配置文件 spring_quartz.xml和运行结果
      • 3.1、RAM存储方式的xml配置文件
        • 3.1.1、配置文件
        • 3.1.2、运行结果
      • 3.2、JDBC存储方式的xml配置文件
        • 3.2.1、Spring配置文件
        • 3.2.2 、quartz.properties
        • 3.2.3、HelloWorldJob.java没有改动,HWTest.java修改如下
        • 3.2.4、输出的结果
    • 四、本文总结
    • Quartz专栏系列

Spring是一个很优秀的框架,它无缝的集成了Quartz,简单方便的让企业级应用更好的使用Quartz进行任务的调度。下面就对Spring集成Quartz进行简单的介绍和示例讲解!和上一节 Quartz学习——2、简单入门示例Demo
的流程相似,介绍Spring和Quartz集成存储方式使用的是RAM方式和JDBC方式!
注:在企业级开发过程中,正常情况下不会使用RAM方式进行任务的存储,都是使用JDBC方式。方便学习,这两种方式都进行介绍!
本篇中的代码下载地址: spring_quartz demo

一、Jar包依赖

使用Maven进行Jar包的管理,使用的jar包如下:


    <dependency>
		 <groupId>org.quartz-schedulergroupId>
		 <artifactId>quartzartifactId>
		 <version>2.2.1version>
	dependency>
	<dependency>
        <groupId>org.quartz-schedulergroupId>
        <artifactId>quartz-jobsartifactId>
        <version>2.2.1version>
    dependency>
    
		<dependency>
			<groupId>org.springframeworkgroupId>
			<artifactId>spring-contextartifactId>
			<version>4.0.5.RELEASEversion>
		dependency>
		<dependency>
			<groupId>org.springframeworkgroupId>
			<artifactId>spring-context-supportartifactId>
			<version>4.0.5.RELEASEversion>
		dependency>
		<dependency>
			<groupId>org.springframeworkgroupId>
			<artifactId>spring-jdbcartifactId>
			<version>4.0.5.RELEASEversion>
		dependency>
		<dependency>
			<groupId>org.springframeworkgroupId>
			<artifactId>spring-ormartifactId>
			<version>4.0.5.RELEASEversion>
		dependency>
		<dependency>
			<groupId>org.springframeworkgroupId>
			<artifactId>spring-txartifactId>
			<version>4.0.5.RELEASEversion>
		dependency>
		<dependency>
			<groupId>org.springframeworkgroupId>
			<artifactId>spring-aspectsartifactId>
			<version>3.2.3.RELEASEversion>
		dependency>
		<dependency>
			<groupId>org.springframeworkgroupId>
			<artifactId>spring-testartifactId>
			<version>4.0.5.RELEASEversion>
		dependency>
		<dependency>
			<groupId>org.springframeworkgroupId>
			<artifactId>spring-oxmartifactId>
			<version>4.0.5.RELEASEversion>
		dependency>
    
    
	<dependency>
		<groupId>org.slf4jgroupId>
		<artifactId>slf4j-apiartifactId>
		<version>1.7.5version>
	dependency>
	<dependency>
		<groupId>org.slf4jgroupId>
		<artifactId>jcl-over-slf4jartifactId>
		<version>1.7.5version>
	dependency>
	<dependency>
		<groupId>ch.qos.logbackgroupId>
		<artifactId>logback-classicartifactId>
		<version>1.0.13version>
	dependency>
    
	<dependency>
		<groupId>mysqlgroupId>
		<artifactId>mysql-connector-javaartifactId>
		<version>5.1.31version>
	dependency>

二、任务类Job

使用 HelloWorldJob 作为任务类!

package com.dufy.spring.quartz.chapter01.job;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Spring 整合 Quartz 的  Hello World!
 * @author dufy
 * @date 2017.02.26
 *
 */
public class HelloWorldJob implements Job{

	private Logger log = LoggerFactory.getLogger(this.getClass());
	
	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		log.info("This is a first spring combine quartz !");
		log.info("Welcome to Spring_Quartz World!"+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) );
		log.info("Let's begin ! \n \n");
	}

}

HWTest测试类:

package com.dufy.spring.quartz.chapter01.job;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class HWTest {

	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("spring_quartz.xml");

		
	}
}

三、配置文件 spring_quartz.xml和运行结果

3.1、RAM存储方式的xml配置文件

3.1.1、配置文件




	
	
	
		
		
		
		
		
		
		
		
		
		
	
	
	
	
	
	
		
		
		
		
		
		
		
		
	
	
	
	
	
		
			
				
				
			
		
		
	
	

3.1.2、运行结果

下图为Cron的Trigger运行结果,Simple的Trigger运行结果,可以自行进行查看:
精进Quartz—Spring和Quartz集成详解(三)_第1张图片

3.2、JDBC存储方式的xml配置文件

3.2.1、Spring配置文件


<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	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-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">


	
	
	<bean name="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="applicationContextSchedulerContextKey" value="applicationContextKey"/>
        <property name="configLocation" value="classpath:quartz.properties"/>	
	bean>
	
beans>

3.2.2 、quartz.properties

# Default Properties file for use by StdSchedulerFactory
# to create a Quartz Scheduler Instance, if a different
# properties file is not explicitly specified.
#

#============================================================================
# Configure Main Scheduler Properties
#============================================================================
org.quartz.scheduler.instanceName: dufy_test
org.quartz.scheduler.instanceId = AUTO

org.quartz.scheduler.rmi.export: false
org.quartz.scheduler.rmi.proxy: false
org.quartz.scheduler.wrapJobExecutionInUserTransaction: false
#============================================================================
# Configure ThreadPool
#============================================================================
org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount: 2
org.quartz.threadPool.threadPriority: 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread: true

org.quartz.jobStore.misfireThreshold: 60000
#============================================================================
# Configure JobStore
#============================================================================
 
#default config
#org.quartz.jobStore.class: org.quartz.simpl.RAMJobStore
#持久化配置
org.quartz.jobStore.class:org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass:org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.useProperties:true

#============================================================================
#havent cluster spring
#============================================================================
org.quartz.jobStore.isClustered = false  

#数据库表前缀
org.quartz.jobStore.tablePrefix:qrtz_
org.quartz.jobStore.dataSource:qzDS

#============================================================================
# Configure Datasources
#============================================================================
#JDBC驱动
org.quartz.dataSource.qzDS.driver:com.mysql.jdbc.Driver
org.quartz.dataSource.qzDS.URL:jdbc:mysql://localhost:3306/quartz_test
org.quartz.dataSource.qzDS.user:root
org.quartz.dataSource.qzDS.password:root
org.quartz.dataSource.qzDS.maxConnection:10

3.2.3、HelloWorldJob.java没有改动,HWTest.java修改如下

package com.dufy.spring.quartz.chapter01.job;

import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdScheduler;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class HWTest {
	
	 private static Scheduler scheduler;
	 
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("spring_quartz.xml");

		 scheduler = (StdScheduler)ac.getBean("scheduler");
		 
		 startSchedule();
	}
	
	public static void startSchedule() {
		try {
			// 1、创建一个JobDetail实例,指定Quartz
			JobDetail jobDetail = JobBuilder.newJob(HelloWorldJob.class)
			// 任务执行类
					.withIdentity("job1_1", "jGroup1")
					// 任务名,任务组
					.build();
			
			// 触发器类型
			//SimpleScheduleBuilder builder = SimpleScheduleBuilder
					// 设置执行次数
				    //.repeatSecondlyForTotalCount(5);
		
			CronScheduleBuilder builder = CronScheduleBuilder.cronSchedule("0/2 * * * * ?");
			// 2、创建Trigger
			Trigger trigger = TriggerBuilder.newTrigger()
					.withIdentity("trigger1_1", "tGroup1").startNow()
					.withSchedule(builder)
					.build();
			
			// 3、创建Scheduler
		    scheduler = StdSchedulerFactory.getDefaultScheduler();
			scheduler.start();
			// 4、调度执行
			scheduler.scheduleJob(jobDetail, trigger);
			
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
	}

}

使用的Cron类型的触发进行存储,运行期间可以在数据库查看到指定的任务已经保存到数据库,并看到控制台打印的信息!

3.2.4、输出的结果

精进Quartz—Spring和Quartz集成详解(三)_第2张图片

四、本文总结

本文在之前的基础上整合了Spring和Quartz,让们对Quartz有更加深入的了解和认识!

Quartz专栏系列

1.精进Quartz——Quartz大致介绍(一)
2.精进Quartz——Quartz简单入门Demo(二)
3.精进Quartz——Spring和Quartz集成详解
4.精进Quartz——SSMM(Spring+SpringMVC+Mybatis+Mysql)和Quartz集成详解(四)
5.精进Quartz源码——JobStore保存JonDetail和Trigger源码分析(一)
6.精进Quartz源码——scheduler.start()启动源码分析(二)
7.精进Quartz源码——QuartzSchedulerThread.run() 源码分析(三)
8.精进Quartz源码——Quartz调度器的Misfire处理规则(四)


谢谢你的阅读,如果您觉得这篇博文对你有帮助,请点赞或者喜欢,让更多的人看到!祝你每天开心愉快!


不管做什么,只要坚持下去就会看到不一样!在路上,不卑不亢!

博客首页 : http://blog.csdn.net/u010648555

愿你我在人生的路上能都变成最好的自己,能够成为一个独挡一面的人

© 每天都在变得更好的阿飞云

你可能感兴趣的:(Quartz学习,Quartz专栏)