<!-- 配置调度程序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,调度线程才真正开始调度;

spring配置 quartz-config.xml的更多相关文章

  1. 使用spring配置quartz定时器

    quartz是石英钟的意思,所以用这个名字来做定时器的框架名称再适合不过.一年前做项目的时候有用过这个框架,当时没有整理,今天刚好新的商城系统也需要定时器.想要达到的效果是:每天的固定时间,比如凌晨3 ...

  2. Spring配置Quartz任务调度、及 ThreadPool 线程池

    ONE.除了引入 Spring 相关的 jar 包,还要引入 Quartz 的 jar 包 <dependency> <groupId>org.springframework& ...

  3. 使用spring配置类代替xml配置文件注册bean类

    spring配置类,即在类上加@Configuration注解,使用这种配置类来注册bean,效果与xml文件是完全一样的,只是创建springIOC容器的方式不同: //通过xml文件创建sprin ...

  4. 关于Spring的Quartz的xml配置的例子

    <span style="font-size:16px"></span><h3><span style="font-family ...

  5. spring 配置 Java配置类装配bean

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

  6. 定时任务-在spring中配置quartz

    使用的版本Spring4.04+Quartz2.2.3,关于jar包自行下载. 详细需要以下几个步骤来完成: 1.  定义要执行的Job类 2.  定义quartz的配置文件applicationCo ...

  7. spring配置中,properties文件以及xml文件配置问题

    spring方便我们的项目快速搭建,功能强大,自然也会是体系复杂! 这里说下配置文件properties管理的问题. 一些不涉及到代码逻辑,仅仅只是配置数据,可以放在xxxx.properties文件 ...

  8. Spring中Quartz的配置

    Quartz是一个强大的企业级任务调度框架,Spring中继承并简化了Quartz,下面就看看在Spring中怎样配置Quartz: 首先,来写一个测试被调度的类:(QuartzHelloWorldJ ...

  9. [代码]JAVA触发器,Spring的quartz配置

    Spring的quartz中的配置代码,spring-quartz.xml: <?xml version="1.0" encoding="UTF-8"?& ...

  10. Spring中,applicationContext.xml 配置文件在web.xml中的配置详解

    一.首先写一下代码结构. 二.再看web.xml中的配置情况. <?xml version="1.0" encoding="UTF-8"?> < ...

随机推荐

  1. Python学习第二天-编写购物车

    需求:1.启动程序后,让用户输入工资,然后打印商品列表         2.允许用户根据商品编号购买商品         3.用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒          ...

  2. asm下重定向数据文件小例(使用toad工具)

    原创作品,出自 "深蓝的blog" 博客,深蓝的blog:http://blog.csdn.net/huangyanlong/article/details/46863991 场景 ...

  3. mysql-面试题目1

    一.数据库的ACID 原子性(Atomicity):保证事务中的所有操作全部执行或全部不执行. 一致性(Consistency):保证数据库始终保持数据的一致性——事务操作之前和之后都是一致的. 隔离 ...

  4. PAT(B) 101-111-1-2014-03-01

    1.个位数统计: #include<stdio.h> #include<iostream> #include<string.h> #include<set&g ...

  5. zjnu(1181)——石子合并

    这道题算是最简单的区间dp了..非常久之前写的,搞懂原理了就1A. 传送门:problem_id=1181">http://acm.zjnu.edu.cn/CLanguage/show ...

  6. java匿名内部类的使用注意事项

    1.首先匿名内部类要继承自抽象基类或者实现基类接口 like this abstract class Seed{ int cnt; public Seed(int x){ cnt=x; } abstr ...

  7. sass05 数据类型,数据运算

    /*! 数字类型 */ $n1: 1.2; $n2: 12; $n3: 14px; p{ font-size: $n3; } /*! 字符串类型*/ $s1: container; $s2: 'con ...

  8. angularjs 模块化

    <!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="C ...

  9. 搭建Hadoop的全分布模式

    此教程仅供参考 注意:此文档目的是为了本人方便以后复习,不适合当教程,以免误导萌新... 1.安装三台Linux2.在每台机器上安装JDK3.配置每台机器的免密码登录 (*) 生成每台机器的公钥和私钥 ...

  10. Spring 注解拦截器使用详解

    Spring mvc拦截器 平时用到的拦截器通常都是xml的配置方式.今天就特地研究了一下注解方式的拦截器. 配置Spring环境这里就不做详细介绍.本文主要介绍在Spring下,基于注解方式的拦截器 ...