<!-- 配置调度程序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. Linux bash常用快捷键

    移动光标 ctrl-a 光标移动到行首 ctrl-e 光标移动到行尾 ctrl+xx 在行首和光标位置直接切换 ctrl-b 光标左移一位 ctrl-f 光标右移一位 alt-b 光标左移一词 alt ...

  2. 洛谷 P2279 [HNOI2003]消防局的设立 (树形dp or 贪心)

    一看到这道题就知道是树形dp 之前做过类似的题,只不过保护的范围是1 所以简单很多. 这道题保护的范围是2,就复杂了很多. 我就开始列状态,然后发现竟然有5种 然后我就开始列方程. 但是我考虑的时候是 ...

  3. 6款 jQuery Lightbox图片查看触控插件

    偶然间在网上看到的几个图片预览的插件,挺好用的,顺手整理下来. 1:Zoomify – jQuery缩放效果lightbox插件 地址:http://www.dowebok.com/214.html ...

  4. 移植u-boot-2014.4到S5PV210/TQ210(完整)

    本文很多其它的是教会大家怎样学习 1.1   概述 1.2   u-boot配置过程分析 1.3   u-boot编译过程分析 1.4   SPL 1.5   加入自己的单板 1.6   移植u-bo ...

  5. 微信公众号开发将war包导入新浪sae出现错误

    JAVA_Error: Error for /wechat.do java.lang.NoSuchFieldError: INSTANCE at org.apache.http.impl.io.Def ...

  6. python之路-------字符串与正則表達式

    1.1.#####去掉字符串中的转义符string.strip() print "hello\tworld\n" >>> word="\thello w ...

  7. ural 1989(树状数组+多项式hash)

    题意:给出一个字符串.有两种操作,一个是p a b,问字符串从位置a到位置b的子串是否是一个回文子串.还有一个操作 c a b,把字符串位置a的字符替换为b. 题解:由于字符串长度为1e5且问的次数也 ...

  8. String methods

    A method is similar to a function – it takes arguments and returns a value – but the syntax is diffe ...

  9. C语言基础-第三章

    C语句和数据输入/输出(函数) 1.printf();输出函数 2.getch();输入函数 3.scanf();格式输入 4.puts();字符串输出 5.gets();字符串输入

  10. PostgreSQL Replication之第三章 理解即时恢复(3)

    3.3 做基础备份 在上一节中,您已经看到,启用归档只需要几行命令,并提供了极大的灵活性.在本节,我们将看到如何创建一个所谓的基础备份,稍后这可以使用XLOG.一个基本备份是一个最初的数据的拷贝. [ ...