quartz(二):quartz和spring的整合

Spring也提供了对Quartz的支持,它对Quzrtz的核心类进行了封装,使开发人员更便捷地实现任务调度。另外,整合Spring和Quartz,可以使用声明的方式配置计划任务,大大简化了操作步骤,而且也降低了代码耦合。

下面我们通过Spring框架来重构【http://blog.csdn.net/wx5040257/article/details/78780400】提出的问题。主要通过3步来完成。

1.使用JobDetaiIBean创建Quartz工作

在Quartz框架中,开发人员使用JobDetail创建任务对象,通过JobExecutionContext从JobDataMap中读取任务数据。Spring框架为了简化操作,提供了QuartzjobBean类,它实现了Quartz中的org.quartz.Job接口。该接口中的executelntemal()方法,定义了在预定的时刻到来时应该执行的计划任务。因此,我们需要从该类中派生子类来创建任务。然后通过扩展QuartzjobBean来创建Remindjob,并实现它的executelnternal()方法。

2.使用SimpleTriggerBean调度定时任务

接下来,需要创建触发器来定义时间规则。类似于Quartz中的SimpleTrigger,在Spring中可以使用SimpleTriggerBean来完成同样的功能。只需要在Spring的Bean配置文件中配置一个SimpleTriggerBean对象,并添加对JobDetail对象的引用。

3.使用SchedulerFactoryBean注册JobDetail和Trigger

最后,就是要配置调度器。在Spring中的Bean配置文件中可以配置一个SchedulerFactoryBean实例,可以在这个工厂Bean中指定多个触发器。最后将触发器添加到SchedulerFactoryBean中。

完整案例

==============TaskEntity.java================

package com.obtk.entitys;

public class TaskEntity {
	private String dateStr;
	private String task;
	
	public TaskEntity() {
		
	}
	public TaskEntity(String dateStr, String task) {
		super();
		this.dateStr = dateStr;
		this.task = task;
	}
	public String getDateStr() {
		return dateStr;
	}
	public void setDateStr(String dateStr) {
		this.dateStr = dateStr;
	}
	public String getTask() {
		return task;
	}
	public void setTask(String task) {
		this.task = task;
	}
	public void showInfo(){
		System.out.println(this.dateStr+"===>"+this.task);
	}
}
================TaskDao.java=====================

package com.obtk.dao;

import java.util.ArrayList;
import java.util.List;

import com.obtk.entitys.TaskEntity;

public class TaskDao {
	public List<TaskEntity> queryAllTask(){
		List<TaskEntity> taskList=new ArrayList<TaskEntity>();
		TaskEntity task1=new TaskEntity("2018-03-15 08:00:00", "起床执行打假任务");
		TaskEntity task2=new TaskEntity("2018-03-15 20:30:00", "到打假办公室315进行研讨");
		taskList.add(task1);
		taskList.add(task2);
		return taskList;
	}
	
}
==================SpringDaJiaJob.java====================

package com.obtk.jobs;

import java.util.List;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

import com.obtk.dao.TaskDao;
import com.obtk.entitys.TaskEntity;

public class SpringDaJiaJob extends QuartzJobBean{
	private String userName;
	
	private TaskDao taskDao;
	
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public void setTaskDao(TaskDao taskDao) {
		this.taskDao = taskDao;
	}
	
	protected void executeInternal(JobExecutionContext arg0)
			throws JobExecutionException {
		List<TaskEntity> taskList=taskDao.queryAllTask();
		System.out.println("请注意,这是"+userName+"发的消息==>");
		for(TaskEntity task : taskList){
			//输出打假任务通知的内容
			task.showInfo();
		}
	}
}

spring配置如下

<?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="taskDao" class="com.obtk.dao.TaskDao"></bean>
	<!-- job -->
	<bean id="remindJob" class="org.springframework.scheduling.quartz.JobDetailBean">
		<property name="jobClass" value="com.obtk.jobs.SpringDaJiaJob"></property>
		<property name="jobDataAsMap">
			<map>
				<entry key="taskDao" value-ref="taskDao"></entry>
				<entry key="userName" value="孙主任"></entry>
			</map>
		</property>
	</bean>
	<!-- 触发器 -->
	<bean id="myTrig" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
		<property name="jobDetail" ref="remindJob"></property>
		<property name="startDelay" value="1000"></property>
		<property name="repeatInterval" value="3000"></property>
		<property name="repeatCount" value="4"></property>
	</bean> 
	<!-- cron触发器 -->
	<bean id="cronTrig" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail" ref="remindJob"></property>
		<property name="cronExpression" value="5/5 * * 13 * ? 2017"></property>
	</bean>
	<!-- 调度器 -->
	<bean id="mySched" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				<!-- <ref bean="myTrig"/> -->
				<ref bean="cronTrig"/>
			</list>
		</property>
		<property name="waitForJobsToCompleteOnShutdown" value="true"></property>
	</bean>
</beans>


测试类

package com.obtk.test;


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


public class TestSpring2 {
	public static void main(String[] args) {
		ApplicationContext context=
			new ClassPathXmlApplicationContext("ApplicationContext.xml");
		//这条语句后任务就开始执行了
		Scheduler mySched=(Scheduler)context.getBean("mySched");
		try {
			Thread.sleep(5000);
			//下面是强行停止
			mySched.shutdown();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用。Quartz可以用来创建简单或为运行十个,百个,甚至是好几万个Jobs这样复杂的程序。Jobs可以做成标准的Java组件或 EJBs。 Quartz的优势: 1、Quartz是一个任务调度框架(库),它几乎可以集成到任何应用系统中。 2、Quartz是非常灵活的,它让您能够以最“自然”的方式来编写您的项目的代码,实现您所期望的行为 3、Quartz是非常轻量级的,只需要非常少的配置 —— 它实际上可以被跳出框架来使用,如果你的需求是一些相对基本的简单的需求的话。 4、Quartz具有容错机制,并且可以在重启服务的时候持久化(”记忆”)你的定时任务,你的任务也不会丢失。 5、可以通过Quartz,封装成自己的分布式任务调度,实现强大的功能,成为自己的产品。6、有很多的互联网公司也都在使用Quartz。比如美团 Spring是一个很优秀的框架,它无缝的集成了Quartz,简单方便的让企业级应用更好的使用Quartz进行任务的调度。   课程说明:在我们的日常开发中,各种大型系统的开发少不了任务调度,简单的单机任务调度已经满足不了我们的系统需求,复杂的任务会让程序猿头疼, 所以急需一套专门的框架帮助我们去管理定时任务,并且可以在多台机器去执行我们的任务,还要可以管理我们的分布式定时任务。本课程从Quartz框架讲起,由浅到深,从使用到结构分析,再到源码分析,深入解析QuartzSpring+Quartz,并且会讲解相关原理, 让大家充分的理解这个框架和框架的设计思想。由于互联网的复杂性,为了满足我们特定的需求,需要对Spring+Quartz进行次开发,整个次开发过程都会进行讲解。Spring被用在了越来越多的项目中, Quartz也被公认为是比较好用的定时器设置工具,学完这个课程后,不仅仅可以熟练掌握分布式定时任务,还可以深入理解大型框架的设计思想。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

御前两把刀刀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值