<!-- 配置调度程序quartz ,其中配置JobDetail有两种方式--><!-- 使用MethodInvokingJobDetailFactoryBean,任务类可以不实现Job接口,通过targetMethod指定调用方法--><!-- 定义目标bean和bean中的方法 --><bean id="SpringQtzJob" class="com.sky.JobSchedule.Job.JobTest"/><bean id="SpringQtzJobMethod" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"><property name="targetObject"><ref bean="SpringQtzJob"/></property><property name="targetMethod">  <!-- 要执行的方法名称 --><value>helloSky</value></property><property name="arguments" value="11"/><property name="concurrent " value="false"></property >     <!--非并发--></bean><!--  调度触发器  --><bean id="CronTriggerBean" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"><property name="jobDetail" ref="SpringQtzJobMethod"></property><property name="cronExpression" value="0/5 * * * * ?"></property></bean><!--  调度工厂  --><bean id="SpringJobSchedulerFactoryBean" lazy-init="true" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"><property name="triggers"><list><ref bean="CronTriggerBean"/></list></property></bean>

  

public class JobTest {String name;public JobTest() {System.out.println("Hello, Quartz! ----------------------");}public void helloSky(int age) {SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");System.out.println("Hello, " + age + " sky !" + formatter.format(new Date()));}public String getName() {return name;}public void setName(String name) {this.name = name;}
}

  

    public static void main(String[] args) throws SchedulerException {ApplicationContext context = new ClassPathXmlApplicationContext("classpath:quartz-config.xml");}

  

另外一种方式:任务类必须继承QuartzJobBean或者实现Job方法。

<bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"><property name="jobClass" value="com.mc.bsframe.job.TestJob"></property><property name="durability" value="true"></property></bean><bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean"><property name="jobDetail" ref="jobDetail" /><property name="startDelay" value="3000" /><property name="repeatInterval" value="2000" /></bean><!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 --><bean id="DefaultQuartzScheduler" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"><!-- 管理trigger --><property name="triggers"><list><ref bean="simpleTrigger" /></list></property>      <property name="configLocation" value="classpath:quartz.properties" />
</bean>

只持久化jobDetail

<bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"><property name="jobClass" value="com.sky.JobSchedule.Job.HelloQuartzJob"></property><property name="durability" value="true"></property></bean><bean id="DefaultQuartzScheduler" lazy-init="true" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"><property name="jobDetails"><list><ref bean="jobDetail" /></list></property><property name="configLocation" value="classpath:quartz.properties" /></bean>

  

两种方法的说明

使用QuartzJobBean,需要继承。而使用MethodInvokeJobDetailFactoryBean则需要指定targetObject(任务实例)和targetMethod(实例中要执行的方法)

后者优点是无侵入,业务逻辑简单,一目了然,缺点是无法持久化(目前还不太清楚这点!)

从我使用的经验来说,我更推荐的第二种,其中一个很重要的原因就是因为定时任务中注入相关Service的时候,后者可以直接注入,而前者还需要进行Schedular的替换修改。

上述配置的SchedulerFactoryBean的Id需要注意的是:

假若需要进行可视化调度管理的话,不可缺的是获取所有的jobDetail等等,这时候需要注意的是,获取过程中,实例化Scheduler时,instanceName会根据quartz.properties来进行获取,没有的话默认“QuartzSchelduer”,会获取数据库中SCHED_NAME一致的数据。

自定义的名字,可以直接保持配置的id和instanceName一致。

或者显示的调用SchedulerFactoryBean:

StdSchedulerFactory sf = new StdSchedulerFactory();
Properties props = new Properties();
props.put("org.quartz.scheduler.instanceName", "你定义的名字");
props.put("org.quartz.threadPool.threadCount", "10");#必填
sf.initialize(props);
scheduler = sf.getScheduler();
System.out.println(scheduler.getSchedulerName());
scheduler.shutdown();

  

添加监听器:在spring-quartz中,监听器:http://www.cnblogs.com/skyLogin/p/6928431.html

<?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.xsd"><bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"><property name="jobClass" value="com.sky.JobSchedule.Job.HelloQuartzJob"></property><property name="durability" value="true"></property></bean><bean id="jobDetail2" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"><property name="jobClass" value="com.sky.JobSchedule.Job.JobCron"></property><property name="name" value="test2"></property><property name="durability" value="true"></property><property name="jobDataAsMap"><map><entry key="name" value="sky"></entry></map></property></bean><bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean"><property name="jobDetail" ref="jobDetail" /><property name="repeatInterval" value="2000" /></bean><bean id="jobCountListener" class="com.sky.JobSchedule.Listener.JobCountListener" /><!--id--><bean id="DefaultQuartzScheduler" lazy-init="false"  autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"><!--<property name="autoStartup" value="false"></property>--><property name="jobDetails"><list><ref bean="jobDetail" /><ref bean="jobDetail2" /></list></property><!-- 管理trigger --><property name="triggers"><list><ref bean="simpleTrigger" /></list></property><property name="configLocation" value="classpath:quartz.properties" /><property name="globalJobListeners" ref="jobCountListener"></property></bean>
</beans>

  

quartz启动流程

当服务器启动时,就会装载相关的bean。SchedulerFactoryBean实现了InitializingBean接口,因此在初始化bean的时候,会执行afterPropertiesSet方法,该方法将会调用SchedulerFactory(DirectSchedulerFactory 或者 StdSchedulerFactory,通常用StdSchedulerFactory)创建Scheduler。SchedulerFactory在创建quartzScheduler的过程中,将会读取配置参数,初始化各个组件

  1. ThreadPool:一般是使用SimpleThreadPool,SimpleThreadPool创建了一定数量的WorkerThread实例来使得Job能够在线程中进行处理。WorkerThread是定义在SimpleThreadPool类中的内部类,它实质上就是一个线程。在SimpleThreadPool中有三个list:workers-存放池中所有的线程引用,availWorkers-存放所有空闲的线程,busyWorkers-存放所有工作中的线程;

  2. JobStore:分为存储在内存的RAMJobStore和存储在数据库的JobStoreSupport(包括JobStoreTX和JobStoreCMT两种实现,JobStoreCMT是依赖于容器来进行事务的管理,而JobStoreTX是自己管理事务),若要使用集群要使用JobStoreSupport的方式;

  3. QuartzSchedulerThread:

SchedulerFactoryBean还实现了SmartLifeCycle接口,因此初始化完成后,会执行start()方法,该方法将主要会执行以下的几个动作:

  1. 创建ClusterManager线程并启动线程:该线程用来进行集群故障检测和处理,将在下文详细讨论;
  2. 创建MisfireHandler线程并启动线程:该线程用来进行misfire任务的处理,将在下文详细讨论;
  3. 置QuartzSchedulerThread的paused=false,调度线程才真正开始调度;

转载于:https://www.cnblogs.com/skyLogin/p/6899156.html

spring配置 quartz-config.xml相关推荐

  1. Spring配置Quartz实现定时任务

    Spring引入Quartz实现定时任务从头开始: 一.引入jar包 spring3.1以下的版本必须使用quartz1.x系列,3.1以上的版本才支持quartz 2.x,不然会出错. <p& ...

  2. 实践讲解Spring配置中心config(图+文,本地文件方式)

    1 缘起 微服务的学习过程中,发现了许多服务的配置是相同的,并且项目稳定运行期间不会轻易变更, 于是,自己开始做实验,将这些相同的配置提取出来放在配置中心, 各个服务需要时,通过这个配置中心获取,Sp ...

  3. Spring配置JPA的xml路径的问题

    在Spring的配置文件中配置JPA的路径为/META-INF/persistence.xml,如下 <bean id="entityManagerFactory" clas ...

  4. 使用JDBCTemplate实现与Spring结合,方法公用 ——Spring配置(applicationContext.xml)

    <?xml version="1.0" encoding="UTF-8"?> <beansxmlns="http://www.spr ...

  5. 在Spring中使用JDBCJobStore配置Quartz

    我将开始一些有关Quartz Scheduler内部,提示和技巧的系列文章,这是第0章-如何配置持久性作业存储. 在Quartz中,您基本上可以在将作业和触发器存储在内存中以及在关系数据库中进行选择( ...

  6. java spring配置类_spring 配置 Java配置类装配bean

    https://www.cnblogs.com/chenbenbuyi/p/8457700.html 自动化装配的确有很大的便利性,但是却并不能适用在所有的应用场景,比如需要装配的组件类不是由自己的应 ...

  7. 校园商铺平台项目(3)- spring 配置

    1 添加目录 2 添加依赖 <properties><project.build.sourceEncoding>UTF-8</project.build.sourceEn ...

  8. spring配置bean

    第一章 spring框架构成 第二章 spring容器 第三章 spring配置bean 文章目录 spring配置bean 使用xml配置 使用构造器创建 依赖注入方式 setter方法注入 构造方 ...

  9. Quartz-Spring集成Quartz通过XML配置的方式

    文章目录 概述 Maven依赖 步骤 1 创建JobDteail 2 创建Trigger 3 创建Scheduler 示例-MethodInvokingJobDetailFactoryBean 示例- ...

  10. Spring Cache抽象-基于XML的配置声明(基于EhCache的配置)

    概述 完整示例 pomxml增加依赖 数据库表数据Oracle 实体类 服务层 ehcache的配置文件 Spring-EhCache配置文件 单元测试 日志输出 日志分析 示例源码 概述 首先请阅读 ...

最新文章

  1. LinearAlgebra_1
  2. Android 百度地图 SDK v3.0.0 (二) 定位与结合方向传感器
  3. python命令解析_python解析命令行
  4. mysql基础(3)-高级查询
  5. 人智化转型 华为云微认证带你实力进阶
  6. oracle log.xml分析,Oracle 11g Alert Log日志位置及参数
  7. java web开发需要学习哪些知识_java web开发需要学习哪些知识?
  8. php 非常简单的导入sql文件
  9. WinCE偶尔不能正常启动(内存清理)
  10. 【绝迹篇】RSA加密算法(私钥加签公钥验签)
  11. php 简易教学管理系统
  12. JAVA MONGODB 查询时间段
  13. 计算机自带录像视频文件代码,Windows自带录屏如何录制视频文件?
  14. 微服务4——服务的限流、熔断(Sentinel-三ti no)sca-comsumersca-provider
  15. creo1复制粘贴指令
  16. icon图标 地址栏 收藏夹显示 代码
  17. html自定义指针,如何自定义鼠标指针 怎样在wpf中自定义鼠标指针
  18. 看电影哪款蓝牙耳机降噪效果最好?性价比降噪蓝牙耳机推荐
  19. PHP开发宝典-PHP基础
  20. 在OpenGL中创建一个球体动画,使球体在窗口内做自由落体运动,并在撞击地面后能够返回原来高度

热门文章

  1. Linux环境中MySQL主从同步--添加新的从库
  2. 写给我--过去,现在,未来
  3. make it clear how to use const in C++
  4. 一步安装openssh的脚本
  5. iPhone平台下的游戏开发
  6. PHP包管理器PEAR 中爆多个缺陷可发动供应链攻击,已潜伏15年
  7. 多个蓝牙缺陷可使攻击者假冒合法设备
  8. Repo Jacking:依赖关系仓库劫持漏洞,影响谷歌GitHub等7万多个开源项目的供应链...
  9. 流拍后,Cerberus 银行木马源代码在黑市免费公开
  10. spring-boot-maven-plugin多模块install问题解决办法