1.使用spring4.1 与quartz2.1集成
a. Job实现类
package job;
import hello.MessagePrinter;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MyJob {
private MessagePrinter messagePrinter;
private void init(){
messagePrinter = (MessagePrinter)SpringContextHolder.getBean(MessagePrinter.class);
}
public void execute(){
System.out.println((new SimpleDateFormat("yyyy-MM-dd hh:mm:ss:SSS")).format(new Date()));
}
public void execute2(){
init();
messagePrinter.printMessage();
System.out.println((new SimpleDateFormat("yyyy-MM-dd hh:mm:ss:SSS")).format(new Date()));
}
}
b.applicationContext-quartz.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"
xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 定时任务Bean -->
<bean id="myJob" class="job.MyJob" />
<!-- 定义调用对象和调用对象的方法 -->
<bean id="myJobDetail"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 调用的类 -->
<property name="targetObject" ref="myJob" />
<!-- 调用类中的方法 -->
<property name="targetMethod" value="execute" />
<!-- 是否允许任务并发执行。当值为false时,表示必须等到前一个线程处理完毕后才再启一个新的线程 -->
<property name="concurrent" value="false" />
</bean>
<!-- 定义触发时间 -->
<!-- quartz-1.8以前的配置 -->
<!--
<bean id="myJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="testDetail" />
</property>
<property name="cronExpression">
<value>0/10 * * * * ?</value>
</property>
</bean>
-->
<!-- quartz-2.x后的配置 -->
<bean id="myJobTrigger"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail">
<ref bean="myJobDetail" />
</property>
<property name="cronExpression">
<value>0/10 * * * * ?</value>
</property>
</bean>
<!-- 总管理类:如果lazy-init='false',那么容器启动后就会执行调度程序 -->
<!-- 如果lazy-init='true',则需要实例化该bean才能执行调度程序 -->
<bean name="testQuartz" lazy-init="false" autowire="no"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="myJobTrigger" />
<ref bean="myJobTrigger2" />
</list>
</property>
<!-- <property name="autoStartup" value="true"/> -->
</bean>
<!-- 配置MyJob的第二个方法 -->
<!-- 定义调用对象和调用对象的方法 -->
<bean id="myJobDetail2"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 调用的类 -->
<property name="targetObject" ref="myJob" />
<!-- 调用类中的方法 -->
<property name="targetMethod" value="execute2" />
<!-- 是否允许任务并发执行。当值为false时,表示必须等到前一个线程处理完毕后才再启一个新的线程 -->
<property name="concurrent" value="false" />
</bean>
<bean id="myJobTrigger2"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail">
<ref bean="myJobDetail2" />
</property>
<property name="cronExpression">
<value>0/10 * * * * ?</value>
</property>
</bean>
</beans>
3.获取applicationContext实例:
import java.util.Map;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
* 实现 ApplicationContextAware 获取 ApplicationContext spring容器实例
* @author
*
*/
public class SpringContextHolder implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
SpringContextHolder.applicationContext=applicationContext;
}
public static ApplicationContext getApplicationContext(){
return applicationContext;
}
@SuppressWarnings("unchecked")
public static <T> T getBean(String name){
if(applicationContext==null)
return null;
return (T)applicationContext.getBean(name);
}
@SuppressWarnings("unchecked")
public static <T> T getBean(Class<T> clazz){
if(applicationContext==null)
return null;
@SuppressWarnings("rawtypes")
Map beanMaps = applicationContext.getBeansOfType(clazz);
if(!(beanMaps==null || beanMaps.isEmpty())){
return (T)beanMaps.values().iterator().next();
}
return null;
}
}
spring4 jar包下载:http://repo.springsource.org/libs-release-local/org/springframework/spring/4.1.6.RELEASE/
相关推荐
Spring集成quartz跑定时任务实例 自己写的例子并为实现job 有测试的主函数,请参考http://www.blogjava.net/baoyaer/articles/155645.html 博客信息 看此代码,项目里分为两大块,com文件夹下 为上面博客相关学习...
Quartzquartz定时任务跑批 包含Quartz Job Scheduling 学习的文档,Quartz_API的chm文档,demo(包含java例子以及集成spring的Quartz),直接能用
集成了分布式任务调度框架 Quartz ,任务存储于数据库。 使用SpringMVC作为路由控制, 集成 Swagger2 提供实时 RESTful API文档。 数据持久层集成 Mybatis 框架。 使用自定义注解 @TargetDataSource 实现了多数据源...
job(`分布式定时任务`)、swagger(`API接口管理测试`)、security(`基于RBAC的动态权限认证`)、SpringSession(`Session共享`)、Zookeeper(`结合AOP实现分布式锁`)、RabbitMQ(`消息队列`)、Kafka(`消息队列`)、...
基于quartz实现分布式任务调度(定时、API);引入camunda作为任务执行框架,支持工作流式任务编排、并发批量调度和参数传递。 简介 分布式、多租户的支持流程编排的任务调度应用 同时支持Redis分布式锁和Quartz...
包含Quartz Job Scheduling 学习的文档,Quartz_API的chm文档,demo(包含java例子以及集成spring的Quartz)
Elastic Elastic-Job-Lite为轻量级无中心化解决方案,使用罐基于quartz定时任务框架为基础的,因此已有quartz的大部分功能使用zookeeper做协调,调度中心,更轻量级完成任务的分片支持弹性扩容,可以水平扩展,当...
plus(快速操作Mybatis)、BeetlSQL(强大的ORM框架)、upload(本地文件上传和七牛云文件上传)、redis(缓存)、ehcache(缓存)、email(发送各种类型邮件)、task(基础定时任务)、quartz(动态管理定时任务)、xxl-job(分布式...
plus(快速操作Mybatis)、BeetlSQL(强大的ORM框架)、upload(本地文件上传和七牛云文件上传)、redis(缓存)、ehcache(缓存)、email(发送各种类型邮件)、task(基础定时任务)、quartz(动态管理定时任务)、xxl-job(分布式...
Example涵盖内容: 4种日志框架详解:Logback、Log4j、Log4j2和Slf4j,基于secure-ext-spring-boot-starter日志记录脱敏,spring-session基于redis存储session,集成MyBatis以及mybatis-plus3的应用实践,安全认证-...
plus(快速操作Mybatis)、BeetlSQL(强大的ORM框架)、upload(本地文件上传和七牛云文件上传)、redis(缓存)、ehcache(缓存)、email(发送各种类型邮件)、task(基础定时任务)、quartz(动态管理定时任务)、xxl-job(分布式...
比如:使用Spring框架集成quartz,只需要进行一些简单的配置就能定时执行任务了。但是随着企业的系统越来越多、逐步从单一应用慢慢演变为微服务集群。 在分布式集群系统中主要面临出如:任务的重复执行、没有统一定时...
BeetlSQL(强大的ORM框架),upload(本地文件上传和七牛云文件上传),redis(缓存),ehcache(缓存),email(发送各种类型邮件),任务(基础定时任务),Quartz(动态管理定时任务),xxl-job(分布式定时任务...
实现异步处理,定时任务,整合Quartz Job以及Spring Task 邮件管理功能, 整合spring-boot-starter-mail发送邮件等, 数据源:druid 用户管理,菜单管理,角色管理,代码生成 运行环境 jdk8+oracle+redis+...
项目信息:此项目是基于...整合了 quartz 定时任务框架 ,并进行了封装,只需在构建完定时任务Job类后,在 application-quartz.properties 配置文件中进行简单配置即可;实现了 reids 分布式锁 ,当项目部署集群时,
存储过程等),用Redis做中间缓存,缓存数据 实现异步处理,定时任务,整合Quartz Job以及Spring Task 邮件管理功能, 整合spring-boot-starter-mail发送邮件等, 数据源:druid 用户管理,菜单管理,角色管理,代码...