一、基本框架
任务调度器是很多应用系统的幕后功臣,spring的任务调度器是基于对著名的quartz的封装。其基本框架是:
二、加入支持
主要是quartz-xxx.jar包,及相关的包
三、编写第一个程序
需要特别这此编写的代码并不多,只需要写工作(job)类,trigger和scheduler都由spring封装,配置一下即可。
举一个job类的例
package org.examples.mini.task;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class MyJob extends QuartzJobBean {
private MyService service;
public void setService(MyService service) {
this.service = service;
}
public MyService getService() {
return service;
}
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
service.doSomthing("做点什么事……");
}
}
四、配置,
applicationContext-scheduler.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:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
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.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"
default-lazy-init="true">
<description>Spring任务调度器 </description>
<bean id="myJob" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass">
<value>org.examples.mini.task.MyJob</value>
</property>
<property name="jobDataAsMap">
<map>
<entry key="service">
<ref bean="service"/>
</entry>
</map>
</property>
</bean>
<bean id="service" class="org.examples.mini.task.MyServiceImpl"></bean>
<!-- 简单任务触发器 -->
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail">
<ref bean="myJob" />
</property>
<property name="startDelay">
<value>50000</value>
</property>
<property name="repeatInterval">
<value>10000</value>
</property>
</bean>
<bean id="cronTrigger1" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="myJob" />
</property>
<property name="cronExpression">
<value>1 * * * * ? 2010</value><!-- 0 3 17 * * ? 2010 -->
</property>
</bean>
<bean id="cronTrigger2" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="myJob" />
</property>
<property name="cronExpression">
<value>2 * * * * ? 2010</value>
</property>
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<!-- <ref bean="simpleTrigger" />
<ref bean="cronTrigger1" />-->
<ref bean="cronTrigger2" />
</list>
</property>
</bean>
</beans>
五、启动web应用,即可实现真实的任务调度
六、表示式
CronException完整配置格式为: [秒] [分] [小时] [日] [月] [周] [年]
可参考网络资源:http://blog.csdn.net/feiyun72/article/details/6921366
- 大小: 11 KB
分享到:
相关推荐
这是本人初步学习Quartz时候记录的一些笔记,只是Quartz的简单入门!
Spring Boot 学习笔记,包括底层实现原理及代码实战,非常齐全,助你快速打通 Spring Boot 的各个环节。 1. Spring boot helloworld 2. Spring boot 返回 json 数据 3. Spring boot 使用其他 json 转换框架 4. ...
代表:Dubbo(Java)、Orleans(.Net)等特点:和语言绑定紧密代表:SpringCloud等现状:适合混合式开发(例如借助SteeltoeOSS可以让ASP.NetCore与Spring Cloud集成),正值当年代表:ServiceMesh(服务网格)=>例如...
* 实现任务调度 * 普通类调用 Bean * 使用模板引擎 * 集成 JSP * 集成 servlet * 集成 Fliter 和 Linstener * 拦截器 HandlerInterceptor * 系统启动任务 CommandLineRunner * 集成 Junit 单元测试 * 读取系统环境...
内容: Spring资源管理 ...定时调度 AOP切面编程-代理功能的加强 Spring与JMS消息组件 Spring与WebService Spring与Redis数据库 JDBC操作模板 Spring事务管理 SpringDataJPA SpringMVC SpringSecurity
Spring调度 org.springframework.scheduling包源码阅读笔记,学习如何用好Java执行器(高级并发对象)-Spring Framework 3.2.10
controller 控制器 dao 数据访问层 service 服务层 #启动项目说明: 要安装mysql,创建数据库,导入doc目录下的sql文件 启动redis服务。 启动项目,访问。 swagger2访问地址 项目介绍 项目提供了异步调度的demo 项目...
本套项目实战课程有极高的含金量,采用当下最主流的微服务分布式架构,前后端分离技术,涵盖技术栈有:Vue、axios、Springboot、RESTful、mybatis、tkmapper、Swagger、任务调度Quartz/Elastic-Job、阿里云OSS、...
动静分离,负载均衡多线程与初步单点登录与社交登录RabbitMQ消息静脉Nacos服务注册,发现,配置中心分散事务与Seata秒杀系统设计定时任务与分布式调度前哨服务容错Sleuth&Zipkin进行追踪实现一整合的
word源码java 这个是用来记录自己平常的一些练习的笔记。 练习1 基于tesseract开源项目的文字识别和字体集训练 练习2 比较根据两个excel表格的路径,比较两个excel表格有...Quartz单机版的任务调度系统 练习15 S