Quartz(09) quartz spring web 项目的整合(终极版)
2016-08-07 21:03
531 查看
上一节(Quartz(08) quartz spring web 项目的整合(方法二))
这一章我们将采用最简单的一种方式整合quartz spring web. 达到的效果是,我们只需要编写自己的job类,关于job,trigger 的配置信息都存放到数据库.(注:我们公司的的项目就是这么配置的,非常方便)
源码下载地址
1.编写我们自定义的job类Q1,Q2
配置quartz.properties 文件(并无特殊之处)
配置applicationContext.xml 文件
那么我们的关于job和trigger的信息到哪里去了? 去了数据库,配置如下:
这样在启动web项目的时候,Quartz就可以正常工作了.
这一章我们将采用最简单的一种方式整合quartz spring web. 达到的效果是,我们只需要编写自己的job类,关于job,trigger 的配置信息都存放到数据库.(注:我们公司的的项目就是这么配置的,非常方便)
源码下载地址
1.编写我们自定义的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("************************"); } }
配置quartz.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
配置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第三种方式,完全基于数据库,无需配置自定义的job类--> <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <!-- 以下配置实现了job的持久化 (JobStoreTX)--> <!-- 事先在数据库里面配置好quartz的信息,然后quartz自动去读取,并实例化 --> <!-- 运行资源文件myQuartzTest.sql文件,就配置好了相关的信息三张表 --> <property name="configLocation" value="classpath:quartz.properties" /> <property name="applicationContextSchedulerContextKey" value="applicationContextKey" /> <property name="autoStartup" value="true" /> </bean> </beans>
那么我们的关于job和trigger的信息到哪里去了? 去了数据库,配置如下:
DELETE from qrtz_cron_triggers; DELETE from qrtz_triggers; DELETE from qrtz_fired_triggers; DELETE from qrtz_job_details; COMMIT; INSERT INTO `qrtz_job_details` VALUES ('scheduler', 'myJobDetail1', 'DEFAULT', NULL, 'com.quartz.job.Q1', '1', '0', '0', '0', null); INSERT INTO `qrtz_job_details` VALUES ('scheduler', 'myJobDetail2', 'DEFAULT', NULL, 'com.quartz.job.Q2', '1', '0', '0', '0', null); INSERT INTO `qrtz_triggers` VALUES ('scheduler', 'my_job1', 'my_group1', 'myJobDetail1', 'DEFAULT', NULL, 0, 0, 0, 'ACQUIRED', 'CRON', 1470241748000, 0, NULL, 0, null); INSERT INTO `qrtz_triggers` VALUES ('scheduler', 'my_job1', 'my_group2', 'myJobDetail2', 'DEFAULT', NULL, 0, 0, 0, 'ACQUIRED', 'CRON', 1470241748000, 0, NULL, 0, null); INSERT INTO `qrtz_cron_triggers` VALUES ('scheduler', 'my_job1', 'my_group1', '0/5 * * * * ?', 'Asia/Shanghai'); INSERT INTO `qrtz_cron_triggers` VALUES ('scheduler', 'my_job1', 'my_group2', '0/5 * * * * ?', 'Asia/Shanghai'); COMMIT;
这样在启动web项目的时候,Quartz就可以正常工作了.
相关文章推荐
- Quartz(07) quartz spring web 项目的整合(方法一)
- Quartz(08) quartz spring web 项目的整合(方法二)
- web 项目 任务调度 Spring整合Quartz(JobDetailBean方式)
- Quartz整合web项目(不基于spring)
- web项目中加入struts2、spring的支持,并整合两者
- 项目ITP(七) javaWeb 整合 Quartz 实现动态调度 并且 持久化
- Axis2在Web项目中整合Spring
- quartz在web项目中的应用(不使用spring)
- Web项目中使用Spring 3.x + Quartz 2.x实现作业调度详解
- Axis2在Web项目中整合Spring
- Spring+SpringMVC+Mybatis整合系列(二)Eclipse新建Maven web项目
- Quartz(06) quartz整合到web项目中
- JAVA_WEB项目之Struts2、Spring和Hibernate三大框架的整合
- webservice之cxf实现[web项目中基于maven与spring整合]
- Web项目中使用Spring整合CXF发布Web Services
- 项目ITP(七) javaWeb 整合 Quartz 实现动态调度 并且 持久化
- CXF3.0.4与spring整合开发Webservice功能的web项目
- Web项目开发中 Spring整合Junit单元测试
- Spring 整合javaWeb项目导入的lib包
- Java Web项目整合spring和Hibernate时报错!