前言

  系列文章:[传送门]

  项目需求:

     http://www.cnblogs.com/Alandre/p/3733249.html

     上一博客写的是基本调度,后来这只能用于,像每天定个时间 进行数据库备份。但是,远远不能在上次的需求上实现。所以需要实现spring4.0 整合 Quartz 实现动态任务调度。

正文

  spring4.0 整合 Quartz 实现任务调度。这真是期末项目的最后一篇,剩下到暑假吧。

   Quartz 介绍

    Quartz is a full-featured, open source job scheduling service that can be integrated with, or used along side virtually any Java application - from the smallest stand-alone application to the largest e-commerce system. Quartz can be used to create simple or complex schedules for executing tens, hundreds, or even tens-of-thousands of jobs; 
    Quartz框架是一个全功能、开源的任务调度服务,可以集成几乎任何的java应用程序—从小的单片机系统到大型的电子商务系统。Quartz可以执行上千上万的任务调度。
 
   核心概念
     Quartz核心的概念:scheduler任务调度、Job任务、Trigger触发器、JobDetail任务细节

回顾

  上次我们配置了

<!--Quartz-->

    <!-- 集成方式:JobDetailFactoryBean,并且任务类需要继承QuartzJobBean-->
<!-- 定义jobDetail -->
<bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<!-- durability 表示任务完成之后是否依然保留到数据库,默认false -->
<property name="durability" value="true" />
<!-- 目标类 /wmuitp/src/test/SpringQuartz.java-->
<property name="jobClass" value="test.SpringQuartzTest"></property> <!-- 在这个例子中,jobDataAsMap没有用,此目标类中接受的参数 ,若参数为service,则可以在此进行参数配置,类似struts2 -->
<!--
<property name="jobDataAsMap">
<map>
<entry key="service"><value>simple is the beat</value></entry>
</map>
</property>
-->
</bean> <!-- 定义simpleTrigger触发器 -->
<!--
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="jobDetail"></property>
<property name="repeatCount">
<value>8</value>
</property>
<property name="repeatInterval">
<value>1000</value>
</property>
<property name="startDelay">
<value>4</value>
</property>
</bean>
--> <!-- 另一种触发器是CornTrigger -->
<bean id="cornTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="jobDetail"/>
<!-- 每个10秒触发 -->
<property name="cronExpression" value="0/10 * * * * ?"/>
</bean> <!-- 定义核心调度器 -->
<bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<ref bean="cornTrigger"/>
</property>
</bean>

  #spring实现quartz的方式,先看一下上面配置文件中定义的jobDetail。在Quartz 2.x版本中JobDetail已经是一个接口,Spring是通过将其转换为MethodInvokingJob或StatefulMethodInvokingJob类型来实现的。

  这是文档中的源码:

/**
* This implementation applies the passed-in job data map as bean property
* values, and delegates to <code>executeInternal</code> afterwards.
* @see #executeInternal
*/
public final void execute(JobExecutionContext context) throws JobExecutionException {
try {
// Reflectively adapting to differences between Quartz 1.x and Quartz 2.0...
Scheduler scheduler = (Scheduler) ReflectionUtils.invokeMethod(getSchedulerMethod, context);
Map mergedJobDataMap = (Map) ReflectionUtils.invokeMethod(getMergedJobDataMapMethod, context); BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValues(scheduler.getContext());
pvs.addPropertyValues(mergedJobDataMap);
bw.setPropertyValues(pvs, true);
}
catch (SchedulerException ex) {
throw new JobExecutionException(ex);
}
executeInternal(context);
} /**
* Execute the actual job. The job data map will already have been
* applied as bean property values by execute. The contract is
* exactly the same as for the standard Quartz execute method.
* @see #execute
*/
protected abstract void executeInternal(JobExecutionContext context) throws JobExecutionException;

  MethodInvokingJobDetailFactoryBean中的源码:

public void afterPropertiesSet() throws ClassNotFoundException, NoSuchMethodException {
prepare(); // Use specific name if given, else fall back to bean name.
String name = (this.name != null ? this.name : this.beanName); // Consider the concurrent flag to choose between stateful and stateless job.
Class jobClass = (this.concurrent ? MethodInvokingJob.class : StatefulMethodInvokingJob.class); // Build JobDetail instance.
if (jobDetailImplClass != null) {
// Using Quartz 2.0 JobDetailImpl class...
this.jobDetail = (JobDetail) BeanUtils.instantiate(jobDetailImplClass);
BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this.jobDetail);
bw.setPropertyValue("name", name);
bw.setPropertyValue("group", this.group);
bw.setPropertyValue("jobClass", jobClass);
bw.setPropertyValue("durability", true);
((JobDataMap) bw.getPropertyValue("jobDataMap")).put("methodInvoker", this);
}
else {
// Using Quartz 1.x JobDetail class...
this.jobDetail = new JobDetail(name, this.group, jobClass);
this.jobDetail.setVolatility(true);
this.jobDetail.setDurability(true);
this.jobDetail.getJobDataMap().put("methodInvoker", this);
} // Register job listener names.
if (this.jobListenerNames != null) {
for (String jobListenerName : this.jobListenerNames) {
if (jobDetailImplClass != null) {
throw new IllegalStateException("Non-global JobListeners not supported on Quartz 2 - " +
"manually register a Matcher against the Quartz ListenerManager instead");
}
this.jobDetail.addJobListener(jobListenerName);
}
} postProcessJobDetail(this.jobDetail);
}

  #既然知道了其所以然,我们就可以真正实战了。

实战

  听我慢慢道来

减少spring的配置文件

  为了实现一个定时任务,spring的配置代码太多了。动态配置需要们手动来搞。这里我们只需要这要配置即可:

    <!-- quartz配置  动态配置所以我们将 Factory 作为一个service一样的接口 QuartzJobFactory.java-->
<!-- 调度工厂 -->
<bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
</bean>

Job实现类

  在这里我把它看作工厂类:

package test;

import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException; @DisallowConcurrentExecution
public class QuartzJobFactoryImpl implements Job
{ @Override
public void execute(JobExecutionContext context) throws JobExecutionException
{
System.out.println("任务成功运行");
ScheduleJob scheduleJob = (ScheduleJob)context.getMergedJobDataMap().get("scheduleJob");
System.out.println("任务名称 = [" + scheduleJob.getJobName() + "]");
}
}

任务对应实体类

package test;

public class ScheduleJob
{
/** 任务id **/
private String jobId; /** 任务名称 **/
private String jobName; /** 任务分组 **/
private String jobGroup; /** 任务状态 0禁用 1启用 2删除**/
private String jobStatus; /** 任务运行时间表达式 **/
private String cronExpression; /** 任务描述 **/
private String desc; public String getJobId()
{
return jobId;
} public void setJobId(String jobId)
{
this.jobId = jobId;
} public String getJobName()
{
return jobName;
} public void setJobName(String jobName)
{
this.jobName = jobName;
} public String getJobGroup()
{
return jobGroup;
} public void setJobGroup(String jobGroup)
{
this.jobGroup = jobGroup;
} public String getJobStatus()
{
return jobStatus;
} public void setJobStatus(String jobStatus)
{
this.jobStatus = jobStatus;
} public String getCronExpression()
{
return cronExpression;
} public void setCronExpression(String cronExpression)
{
this.cronExpression = cronExpression;
} public String getDesc()
{
return desc;
} public void setDesc(String desc)
{
this.desc = desc;
} }

下面我们就来测试下:

Controller 测试代码:

  @RequestMapping(value = "/quartz")
public ModelAndView quartz() throws SchedulerException
{ //schedulerFactoryBean 由spring创建注入
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println(ctx);
Scheduler scheduler = (Scheduler)ctx.getBean("schedulerFactoryBean"); System.out.println(scheduler);
//这里获取任务信息数据
List<ScheduleJob> jobList = new ArrayList<ScheduleJob>(); for (int i = 0; i < 3; i++) {
ScheduleJob job = new ScheduleJob();
job.setJobId("10001" + i);
job.setJobName("JobName_" + i);
job.setJobGroup("dataWork");
job.setJobStatus("1");
job.setCronExpression("0/5 * * * * ?");
job.setDesc("数据导入任务");
jobList.add(job);
} for (ScheduleJob job : jobList) { TriggerKey triggerKey = TriggerKey.triggerKey(job.getJobName(), job.getJobGroup()); //获取trigger,即在spring配置文件中定义的 bean id="myTrigger"
CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey); //不存在,创建一个
if (null == trigger) {
JobDetail jobDetail = JobBuilder.newJob(QuartzJobFactoryImpl.class)
.withIdentity(job.getJobName(), job.getJobGroup()).build();
jobDetail.getJobDataMap().put("scheduleJob", job); //表达式调度构建器
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job
.getCronExpression()); //按新的cronExpression表达式构建一个新的trigger
trigger = TriggerBuilder.newTrigger().withIdentity(job.getJobName(), job.getJobGroup()).withSchedule(scheduleBuilder).build();
scheduler.scheduleJob(jobDetail, trigger);
} else {
// Trigger已存在,那么更新相应的定时设置
//表达式调度构建器
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job
.getCronExpression()); //按新的cronExpression表达式重新构建trigger
trigger = trigger.getTriggerBuilder().withIdentity(triggerKey)
.withSchedule(scheduleBuilder).build(); //按新的trigger重新设置job执行
scheduler.rescheduleJob(triggerKey, trigger);
}
} ModelAndView mav = new ModelAndView(AdminWebConstant.ADMIN_LOGIN_VIEW);
return mav;
}

#后面这块应该会进一步整理。到时候 会出个更详细的。期待吧

测试结果:

总结

  spring quartz

  

感谢及资源共享

    

    http://url.cn/RzETYu 加入我的群

    

    路上走来一步一个脚印,希望大家和我一起。

    感谢读者!很喜欢你们给我的支持。如果支持,点个赞。

    知识来源: 《spring in action》 quartz api

项目ITP(六) spring4.0 整合 Quartz 实现动态任务调度的更多相关文章

  1. 项目ITP(五) spring4.0 整合 Quartz 实现任务调度

    前言 系列文章:[传送门] 项目需求: 二维码推送到一体机上,给学生签到扫描用.然后需要的是 上课前20分钟 ,幸好在帮带我的学长做 p2p 的时候,接触过.自然 quartz 是首选.所以我就配置了 ...

  2. SpringBoot2.0整合Quartz实现动态设置定时任务时间

    一.    引入依赖 <!-- 引入quartz依赖 --> <dependency> <groupId>org.springframework.boot</ ...

  3. [CXF REST标准实战系列] 二、Spring4.0 整合 CXF3.0,实现测试接口(转)

    转自:[CXF REST标准实战系列] 二.Spring4.0 整合 CXF3.0,实现测试接口 文章Points: 1.介绍RESTful架构风格 2.Spring配置CXF 3.三层初设计,实现W ...

  4. [CXF REST标准实战系列] 二、Spring4.0 整合 CXF3.0,实现测试接口

    Writer:BYSocket(泥沙砖瓦浆木匠) 微博:BYSocket 豆瓣:BYSocket Reprint it anywhere u want. 文章Points: 1.介绍RESTful架构 ...

  5. Spring整合Quartz实现动态定时器

    一.版本说明 spring3.1以下的版本必须使用quartz1.x系列,3.1以上的版本才支持quartz 2.x,不然会出错. 原因:spring对于quartz的支持实现,org.springf ...

  6. Spring整合Quartz实现动态定时器,相关api,定时器添加,删除,修改

    一.版本说明 spring3.1以下的版本必须使用quartz1.x系列,3.1以上的版本才支持quartz 2.x,不然会出错. 原因:spring对于quartz的支持实现,org.springf ...

  7. spring4.0整合mongodb3.0.4项目实践(用户验证)

    我们的项目用到了spring框架和mongdb数据库,随着mongodb升级到3.0已有半年时间,我们也开始随之升级,但是3.0的用户验证有所更改,导致原来的很多配置无法再用. 经过几天的尝试后,终于 ...

  8. springboot学习入门简易版六---springboot2.0整合全局捕获异常及log4j日志(12-13)

    使用Aop实现 1创建异常请求 在原有项目基础上,jspController中创建一个可能发生异常的请求: /** * 全局捕获异常测试 * @param i * @return */ @Reques ...

  9. Spring4.0整合Hibernate3 .6

    转载自:http://greatwqs.iteye.com/blog/1044271 6.5  Spring整合Hibernate 时至今日,可能极少有J2EE应用会直接以JDBC方式进行持久层访问. ...

随机推荐

  1. 《MySQL必知必会》官方提供的数据库和表

    数据用于配合<MySQL必知必会>(MySQL Crash Course)这本书使用,配套SQL文件也可在Ben Forta网站下载. Ben Forta网址:http://forta.c ...

  2. 使用 Chrome 浏览器插件 Web Scraper 10分钟轻松实现网页数据的爬取

    web scraper 下载:Web-Scraper_v0.2.0.10 使用 Chrome 浏览器插件 Web Scraper 可以轻松实现网页数据的爬取,不写代码,鼠标操作,点哪爬哪,还不用考虑爬 ...

  3. String类笔记

    首先要知道,String类的核心是一个数组 我们所写的字符串序列都会放到这个char数组中,且前面有final修饰,所以只能赋值一次. 所以String创建的是不可变字符串序列,不可修改.如果要对其进 ...

  4. linux从0开始----01

    1.VMware 虚拟机安装与卸载 推荐安装较高版本,11.x以后的.本课程安装12.x版本,需要序列号. 2.在vmware中安装centos客户机.初学者选择典型安装也可以. 1.vware文件菜 ...

  5. centos firewall使用笔记

    Centos7.x firewalld配置详解推荐文章文章地址:https://blog.csdn.net/jsonxiang/article/details/87873493 一.firewalld ...

  6. lwip协议栈学习---udp

    书籍:<嵌入式网络那些事-lwip协议> udp协议的优点: 1)基于IP协议,无连接的用户数据报协议,适用于传送大批量数据, 2)实时性比较高,适用于嵌入式网络 发送函数:udp_sen ...

  7. JS入门经典第四章总结

    charAt():该函数有一个参数,即选择哪一个位置上的参数.返回值就是该位置上的字符. charCodeAt():该函数有一个参数,即选择哪一个位置上的参数.返回值是该位置字符在Unicode字符集 ...

  8. linux配置gitlab步骤

    1.安装git命令 yum install -y git 2.查看安装git的版本 git --version 3.创建用于保存项目的文件夹 mkdir 项目文件夹 4.切换目录到项目文件夹 cd 项 ...

  9. 从NoSQL到NewSQL,谈交易型分布式数据库建设要点

    在上一篇文章<从架构特点到功能缺陷,重新认识分析型分布式数据库>中,我们完成了对不同"分布式数据库"的横向分析,本文Ivan将讲述拆解的第二部分,会结合NoSQL与Ne ...

  10. 【设计经验】3、ISE中烧录QSPI Flash以及配置mcs文件的加载速度与传输位宽

    一.软件与硬件平台 软件平台: 操作系统:Windows 7 64-bit 开发套件:ISE14.7 硬件平台: FPGA型号:XC6SLX45-CSG324 QSPI Flash型号:W25Q128 ...