spring配置 quartz-config.xml
<!-- 配置调度程序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的过程中,将会读取配置参数,初始化各个组件
ThreadPool:一般是使用SimpleThreadPool,SimpleThreadPool创建了一定数量的WorkerThread实例来使得Job能够在线程中进行处理。WorkerThread是定义在SimpleThreadPool类中的内部类,它实质上就是一个线程。在SimpleThreadPool中有三个list:workers-存放池中所有的线程引用,availWorkers-存放所有空闲的线程,busyWorkers-存放所有工作中的线程;
JobStore:分为存储在内存的RAMJobStore和存储在数据库的JobStoreSupport(包括JobStoreTX和JobStoreCMT两种实现,JobStoreCMT是依赖于容器来进行事务的管理,而JobStoreTX是自己管理事务),若要使用集群要使用JobStoreSupport的方式;
- QuartzSchedulerThread:
SchedulerFactoryBean还实现了SmartLifeCycle接口,因此初始化完成后,会执行start()方法,该方法将主要会执行以下的几个动作:
- 创建ClusterManager线程并启动线程:该线程用来进行集群故障检测和处理,将在下文详细讨论;
- 创建MisfireHandler线程并启动线程:该线程用来进行misfire任务的处理,将在下文详细讨论;
- 置QuartzSchedulerThread的paused=false,调度线程才真正开始调度;
spring配置 quartz-config.xml的更多相关文章
- 使用spring配置quartz定时器
quartz是石英钟的意思,所以用这个名字来做定时器的框架名称再适合不过.一年前做项目的时候有用过这个框架,当时没有整理,今天刚好新的商城系统也需要定时器.想要达到的效果是:每天的固定时间,比如凌晨3 ...
- Spring配置Quartz任务调度、及 ThreadPool 线程池
ONE.除了引入 Spring 相关的 jar 包,还要引入 Quartz 的 jar 包 <dependency> <groupId>org.springframework& ...
- 使用spring配置类代替xml配置文件注册bean类
spring配置类,即在类上加@Configuration注解,使用这种配置类来注册bean,效果与xml文件是完全一样的,只是创建springIOC容器的方式不同: //通过xml文件创建sprin ...
- 关于Spring的Quartz的xml配置的例子
<span style="font-size:16px"></span><h3><span style="font-family ...
- spring 配置 Java配置类装配bean
https://www.cnblogs.com/chenbenbuyi/p/8457700.html 自动化装配的确有很大的便利性,但是却并不能适用在所有的应用场景,比如需要装配的组件类不是由自己的应 ...
- 定时任务-在spring中配置quartz
使用的版本Spring4.04+Quartz2.2.3,关于jar包自行下载. 详细需要以下几个步骤来完成: 1. 定义要执行的Job类 2. 定义quartz的配置文件applicationCo ...
- spring配置中,properties文件以及xml文件配置问题
spring方便我们的项目快速搭建,功能强大,自然也会是体系复杂! 这里说下配置文件properties管理的问题. 一些不涉及到代码逻辑,仅仅只是配置数据,可以放在xxxx.properties文件 ...
- Spring中Quartz的配置
Quartz是一个强大的企业级任务调度框架,Spring中继承并简化了Quartz,下面就看看在Spring中怎样配置Quartz: 首先,来写一个测试被调度的类:(QuartzHelloWorldJ ...
- [代码]JAVA触发器,Spring的quartz配置
Spring的quartz中的配置代码,spring-quartz.xml: <?xml version="1.0" encoding="UTF-8"?& ...
- Spring中,applicationContext.xml 配置文件在web.xml中的配置详解
一.首先写一下代码结构. 二.再看web.xml中的配置情况. <?xml version="1.0" encoding="UTF-8"?> < ...
随机推荐
- [NOIP2012提高组]疫情控制
题目:洛谷P1084.codevs1218.Vijos P1783. 题目大意:有一棵n个节点的,根为1的带权树和m支军队.每支军队可以在一个点上停下,那么从1开始就不能经过这个点了.现在有m支军队已 ...
- yii框架原生代码
http://www.cnblogs.com/duanxz/p/3480254.htm
- python基础7(函数 Ⅱ)
1.python代码运行遇到函数时 从python解释器开始执行之后,就在内存中开辟了一个空间 每当遇到一个变量的时候,就把变量名和值之间的对应关系记录下来. 但是当遇到函数定义的时候解释器只是象征性 ...
- Linux经常使用命令(九) - cat
cat命令的用途是连接文件或标准输入并打印.这个命令经常使用来显示文件内容,或者将几个文件连接起来显示,或者从标准输入读取内容并显示,它常与重定向符号配合使用. 1. 命令格式: cat [选项] 文 ...
- HTML样式以及使用
HTML的样式包含: 1,标签{style ,link} 2.属性{rel="styleSheet"外部样式表,type="text/css",margin-l ...
- ubuntu,jdk安装成功后,点击eclipse,提示信息A Java RunTime Environment (JRE) or Java Development Kit (JDK)
A Java RunTime Environment (JRE) or Java Development Kit (JDK) must be available in order to run Ecl ...
- js 使用(不断更新...)
1.JS 对象(Object)和字符串(String)互转 var jsObj = {}; jsObj.testArray = [1, 2, 3, 4, 5]; jsObj.name = 'CSS3' ...
- Objects are mutable
We can change the state of an object by making an assignment to one of its attributes. For example, ...
- sql查询每个学生的最高成绩mysql语句
张三 语文 100 张三 数学 83 李四 语文 88 李四 数学 100 查询每个学生的最高成绩. select b.* from (select name,max(score) score fro ...
- HD-ACM算法专攻系列(1)——第几天?
题目描述: 源码: #include <cstdio> #include <ctime> int main() { int year, month, day; int sum; ...