Quartz(08) quartz spring web 项目的整合(方法二)
2016-08-07 20:19
302 查看
上一章(Quartz(07) quartz spring web 项目的整合(方法一))中使用了MethodInvokingJobDetailFactoryBean 来配置我们自定义的job,缺点是这样的job不能持久化.本文我们采用JobDetailFactoryBean 来注册job
.源码下载地址
applicationContext.xml 文件如下:
实现了com.quartz.job接口的Q1,Q2
quarty.properties 的配置如下
这样就是配置好了,启动web项目,发现数据库中已经对我们的任务进行了持久化. 注意点,1.使用JobDetailFactoryBean 注册的job必须实现了com.quartz.job接口
下一节:Quartz(09) quartz spring web 项目的整合(终极版)
.源码下载地址
applicationContext.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: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-3.2.xsd"> <!-- 配置Quartz的两种方式 --> <!-- 1.使用 MethodInvokingJobDetailFactoryBean 来注册jobDetail --> <!-- 2.使用JobDetailFactoryBean 来注册jobDetail --> <!-- 二者之间的区别.使用方法1,自定义的job类是普通的类不需实现job接口,而且targetMethod必须是无参的方法.使用方便,但是job无法持久化(JobStoreTX) --> <!-- 二者之间的区别.使用方法2,自定义的job类需实现job接口 使用复杂些但是job可以持久化(JobStoreTX)--> <!-- 本例采用第2种方式 ,quartz06使用使用第一种方式配置.实现job持久化的配置在quartz08中实现--> <!-- com.quartz.job.Q1,com.quartz.job.Q2 都实现了Job接口 --> <bean id="myJobDetail1" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"> <property name="jobClass" value="com.quartz.job.Q1"></property> <property name="durability" value="true"/> </bean> <bean id="myTrigger1" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="name" value="my_job1"/> <property name="group" value="my_group1"/> <property name="jobDetail"> <ref bean="myJobDetail1"/> </property> <property name="cronExpression"> <value>0/5 * * * * ?</value> </property> </bean> <bean id="myJobDetail2" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"> <property name="jobClass" value="com.quartz.job.Q2"/> <property name="durability" value="true"/> </bean> <bean id="myTrigger2" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="name" value="my_job1"/> <property name="group" value="my_group2"/> <property name="jobDetail"> <ref bean="myJobDetail2"/> </property> <property name="cronExpression"> <value>0/5 * * * * ?</value> </property> </bean> <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="myTrigger1"/> <ref bean="myTrigger2"/> </list> </property> </bean> </beans>
实现了com.quartz.job接口的Q1,Q2
package com.quartz.job; import java.io.Serializable; import java.text.SimpleDateFormat; import java.util.Date; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class Q1 implements Serializable, Job { private static final long serialVersionUID = 6890216263057956690L; public void execute(JobExecutionContext arg0) throws JobExecutionException { System.out.println("------------------------"); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd hh:mm:ss"); System.out.println(sdf.format(new Date())); System.out.println("------------------------"); } }
package com.quartz.job; import java.io.Serializable; import java.text.SimpleDateFormat; import java.util.Date; import org.quartz.Job; import org.quartz.JobExecutionContext; public class Q2 implements Serializable, Job { private static final long serialVersionUID = 5004730246347558783L; public void execute(JobExecutionContext arg0) { System.out.println("************************"); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd hh:mm:ss"); System.out.println(sdf.format(new Date())); System.out.println("************************"); } }
quarty.properties 的配置如下
# Default Properties file for use by StdSchedulerFactory # to create a Quartz Scheduler Instance, if a different # properties file is not explicitly specified. # org.quartz.scheduler.instanceName=DefaultQuartzScheduler org.quartz.scheduler.rmi.export=false org.quartz.scheduler.rmi.proxy=false org.quartz.scheduler.wrapJobExecutionInUserTransaction=false org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool org.quartz.threadPool.threadCount=10 org.quartz.threadPool.threadPriority=5 org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread=true #jobStoreTX TEST org.quartz.jobStore.misfireThreshold=60000 # TX method org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX #jdbc Delegate org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate #table prefix org.quartz.jobStore.tablePrefix=qrtz_ # org.quartz.jobStore.dataSource=myDS org.quartz.dataSource.myDS.driver=com.mysql.jdbc.Driver org.quartz.dataSource.myDS.URL=jdbc:mysql://localhost:3306/test org.quartz.dataSource.myDS.user=root org.quartz.dataSource.myDS.password=123456 org.quartz.dataSource.myDS.maxConnections=10
这样就是配置好了,启动web项目,发现数据库中已经对我们的任务进行了持久化. 注意点,1.使用JobDetailFactoryBean 注册的job必须实现了com.quartz.job接口
下一节:Quartz(09) quartz spring web 项目的整合(终极版)
相关文章推荐
- Quartz(07) quartz spring web 项目的整合(方法一)
- Quartz整合web项目(不基于spring)
- web 项目 任务调度 Spring整合Quartz(JobDetailBean方式)
- Quartz(09) quartz spring web 项目的整合(终极版)
- java Web 项目定时器 Spring之OpenSymphony的Quartz。
- Web项目中加载Spring配置的常用方法
- spring项目的 context root 修改之后,导致 WebApplicationContext 初始化两次的解决方法
- Axis2在Web项目中整合Spring
- JAVA EE项目常用知识 之 整合了spring 项目中使用log4j的一种方法
- 对于web项目单独使用struts,不和Spring整合的情况。
- spring整合quartz时提示表或视图不存在错误解决方法
- Java Web项目整合spring和Hibernate时报错!
- Web项目中使用Spring 3.x + Quartz 2.x实现作业调度详解
- JAVA_WEB项目之如何解决Struts2和Spring整合时出现中文数据提交时出现乱码
- spring和hibernate整合开发web项目的创建及所需的jar包的添加
- web项目取得spring配置的数据源的简单实现方法
- 项目ITP(七) javaWeb 整合 Quartz 实现动态调度 并且 持久化
- 将普通Maven Spring项目转换成Web项目的方法
- javaWeb项目SpringMVC3.2.1与Mybatis3.0.4整合实例(Mybaits-spring配置文件的几种方式)之一SqlSessionDaoSupport方式
- Axis2在Web项目中整合Spring