一.准备
1.spring3.x相关包
2.quartz1.8.5
具体需要的就是一些常用jar包,如果不知道,可以启动时报错的提示加入相关包即可。
二.建好一个相应的工程
具体省略
三.将org.springframework.context.support-3.0.0.RELEASE.jar和quartz-1.8.5.jar两个必须jar包放进lib中,其他例如日志包log4j,slf4j等根据需要添加
四.将quartz.properties文件放到src类路径下,具体文件可从quartz.jar解压后得到,或者从官方提供的例子程序中找
文件中相关的主要配置(只配置这些即可正常运行)
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
org.quartz.jobStore.misfireThreshold = 60000
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
五.建调度程序的applicationContext-quratz.xml文件,并配置applicationContext.xml在启动时加载
1.applicationContext-quratz.xml文件的内容
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-lazy-init="false">
<bean name="webserviceScheduler"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="testTrigger"/>
<ref bean="testCornTrigger"/>
</list>
</property>
<property name="configLocation" value="classpath:quartz.properties" />
<property name="autoStartup" value="true" />
<property name="schedulerName" value="webserviceScheduler" />
</bean>
<!-- 通过表达式定时触发器 -->
<bean id="testCornTrigger"
class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<bean class="frameworkx.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="testJob" /><!-- 引用要定时触发的目标类 -->
</property>
<property name="targetMethod">
<value>testCorn</value><!-- 引用要定时触发的目标类中的方法名 -->
</property>
<property name="concurrent">
<value>false</value><!-- 默认为true,一般设置成false,是否允许多个worker执行同一个job -->
</property>
</bean>
</property>
<property name="cronExpression">
<value>0 51 12 4 7 ? 2013</value><!-- 定时时间:秒,分,时,月中那一天,月,周中那一天,年 -->
</property>
</bean>
<!-- 简单的重复执行触发器 -->
<bean id="testTrigger"
class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail">
<bean class="frameworkx.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="testJob" /><!-- 引用要定时触发的目标类 -->
</property>
<property name="targetMethod">
<value>test</value><!-- 引用要定时触发的目标类中的方法名 -->
</property>
<property name="concurrent">
<value>false</value><!-- 默认为true,一般设置成false,避免数据存储问题 -->
</property>
</bean>
</property>
<property name="repeatInterval">
<!-- 多久重复执行一次,以毫秒为单位 -->
<value>60000</value>
</property>
</bean>
</beans>
2.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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 注解配置切面,代理类 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!-- 数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="oracle.jdbc.driver.OracleDriver" />
<property name="jdbcUrl" value="jdbc:oracle:thin:@192.168.111.231:1521:HWAJ" />
<property name="user" value="webservice" />
<property name="password" value="1" />
</bean>
<!-- 事务管理 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 事务切面 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="del*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="modify*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="edit*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="save*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<aop:config proxy-target-class="true">
<aop:pointcut id="allManageMethod"
expression="execution(* hw.webservice.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="allManageMethod" />
</aop:config>
<!--注入jdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- BaseDao -->
<bean id="baseDao" class="hw.webservice.base.impl.BaseDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<!-- BaseService -->
<bean id="baseService" class="hw.webservice.base.impl.BaseServiceImpl">
<property name="baseDao" ref="baseDao"></property>
</bean>
<bean class="hw.webservice.util.BeanUtil"></bean>
<!-- 任务调度器配置文件 -->
<import resource="classpath*:config/quartz/applicationContext-quratz.xml"/>
<!-- 任务调度器所需service配置文件 -->
<import resource="classpath*:config/spring_config/applicationContext-service-quartz.xml"/>
<!-- 任务调度器所需job配置文件 -->
<import resource="classpath*:config/spring_config/applicationContext-job-service.xml"/>
</beans>
3.applicationContext-job-service.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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="testJob" class="hw.webservice.job.TestJob"/>
<bean id="testDynamicJob" class="hw.webservice.job.TestDynamicJob"/>
</beans>
4.applicationContext-service-quartz.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:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd">
<bean class="frameworkx.springframework.scheduling.quartz.BeanInvokingJobDetailFactoryBean"></bean>
<bean class="frameworkx.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"></bean>
<!-- 动态quartz -->
<bean id="schedulerManageService" class="hw.webservice.quartz.SchedulerManageServiceImpl" />
</beans>
5.两个例子:TestJob和TestDynamicJob
TestJob.java 包含表达式触发器和simple触发器两种
package hw.webservice.job;
import hw.webservice.quartz.ISchedulerManageService;
import hw.webservice.util.BeanUtil;
import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class TestJob implements Serializable{
private static final long serialVersionUID = 1L;
private static ISchedulerManageService schedulerManageService;
static{
schedulerManageService = (ISchedulerManageService)BeanUtil.getBean("schedulerManageService");
}
public void test(){
System.out.println("测试触发器,1m执行一次");
// addJob();
}
public void testCorn() throws Exception{
System.out.println("表达式触发器");
addJob();
addSimpleJob();
}
public void addJob() throws Exception{
Map map = new HashMap();
map.put("key", "测试动态job");
Map JobMap = new HashMap();
JobMap.put("jobGroup", "testJobGroup");
JobMap.put("triggeGroup", "testTriggeGroup");
JobMap.putAll(map);
Date date = null;
try {
date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2013-07-04 12:52:00");
} catch (ParseException e) {
e.printStackTrace();
}
//开始考试任务
JobMap.put("job", "testJob");
JobMap.put("trigger", "tetsTrigger");
JobMap.put("cronExpression", schedulerManageService.getCronexpression_r(date));
JobMap.put("data", "测试");
schedulerManageService.addCronTrigger(JobMap, TestDynamicJob.class);
}
public void addSimpleJob() throws Exception{
Map map = new HashMap();
map.put("key", "测试动态simplejob");
Map JobMap = new HashMap();
JobMap.put("jobGroup", "testJobGroup");
JobMap.put("triggeGroup", "testTriggeGroup");
//开始考试任务
JobMap.put("job", "testJob");
JobMap.put("trigger", "tetsTrigger");
long time = System.currentTimeMillis();
Date date = new Date(time);
JobMap.put("startTime", date);
JobMap.put("endTime", new Date(time+60000));
JobMap.put("repeatCount", 10);
JobMap.put("repeatInterval", 5000);
JobMap.put("data", "测试simplejob");
JobMap.putAll(map);
schedulerManageService.addSimpleTrigger(JobMap, TestDynamicJob.class);
}
}
以上是spring集成quartz1.8.5的完整步骤,以上例子可以实现通过xml文件配置静态的simple触发器和表达式触发器。
弊端:通过xml配置的触发器,没有实现数据的持久哈,数据是保存在内存中的,当服务重启时添加的动态触发器就会丢失。
关于动态触发器的使用和触发器数据持久化,将在接下来的两篇博客中讲述。
相关推荐
可以找到使用Spring ApplicationContext特性时所需的全部类,JDNI 所需的全部类,instrumentation组件以及校验Validation 方面的相关类。 外部依赖spring-beans, (spring-aop)。 5. spring-context-support-4.1.1....
18.3. 使用Spring提供的辅助类实现EJB组件 19. JMS 19.1. 简介 19.2. 使用Spring JMS 19.2.1. JmsTemplate 19.2.2. 连接工厂 19.2.3. (消息)目的地管理 19.2.4. 消息侦听容器 19.2.5. 事务管理 19.3. 发送...
2. Spring 2.0 的新特性 2.1. 简介 2.2. 控制反转(IoC)容器 2.2.1. 更简单的XML配置 2.2.2. 新的bean作用域 2.2.3. 可扩展的XML编写 2.3. 面向切面编程(AOP) 2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的...
Spring Framework 开发参考手册 Rod Johnson Juergen Hoeller Alef Arendsen Colin Sampaleanu Rob Harrop ...18.3. 使用Spring提供的辅助类实现EJB组件 19. JMS 19.1. 简介 19.2. 使用Spring JMS ...
为自动检测的组件提供一个作用域 3.12.6. 用注解提供限定符元数据 3.13. 注册一个LoadTimeWeaver 4. 资源 4.1. 简介 4.2. Resource接口 4.3. 内置 Resource 实现 4.3.1. UrlResource 4.3.2. ClassPath...
为自动检测的组件提供一个作用域 3.12.6. 用注解提供限定符元数据 3.13. 注册一个LoadTimeWeaver 4. 资源 4.1. 简介 4.2. Resource接口 4.3. 内置 Resource 实现 4.3.1. UrlResource 4.3.2. ClassPath...
虽然可以通过属性文件(在属性文件中可以指定 JDBC 事务的数据源、全局作业和/或触发器侦听器、插件、线程池,以及更多)配置 Quartz,但它根本没有与应用程序服务器的上下文或引用集成在一起。结果就是作业不能访问...
(13) spring-mock.jar 这个jar文件包含Spring一整套mock类来辅助应用的测试。Spring测试套件使用了其中大量mock类,这样测试就更加简单。模拟 HttpServletRequest和HttpServletResponse类在Web应用单元测试是很方便...
除了spring.jar文件,Spring还包括有其它13个独立的jar包,各自包含着对应的Spring组件,用户可以根据自己的需要来选择组合自己的jar包,而不必引入整个spring.jar的所有类文件。 (1) spring-core.jar 这个jar文件...
在基于Spring Boot、Spring Cloud 分布微服务开发过程中,根据实际项目环境,需要选择、集成符合项目需求的各种组件和积累各种解决方案。基于这样的背景下,我开源了本示例项目,方便大家快速上手Spring Boot、...
Seam - 语境相关的组件[满江红20071230]............................................................................................................................ 1 Java EE 框架...........................
14.1 用Spring Portlet MVC开发一个简单的Portlet 544 14.1.1 问题 544 14.1.2 解决方案 545 14.1.3 工作原理 546 14.2 将Portlet请求映射到处理程序 553 14.2.1 问题 553 14.2.2 解决方案 553 14.2.3...
14.1 用Spring Portlet MVC开发一个简单的Portlet 544 14.1.1 问题 544 14.1.2 解决方案 545 14.1.3 工作原理 546 14.2 将Portlet请求映射到处理程序 553 14.2.1 问题 553 14.2.2 解决方案 553 14.2.3...
这个jar文件包含Spring一整套mock类来辅助应用的测试。Spring测试套件使用了其中大量mock类,这样测试就更加简单。模拟HttpServletRequest和HttpServletResponse类在Web应用单元测试是很方便的。
方便集成各种优秀框架 Spring不排斥各种优秀的开源框架,其内部提供了对各种优秀框架(如:Struts、Hibernate、MyBatis、Quartz等)的直接支持。 降低Java EE API的使用难度 Spring对Java EE开发中非常难用的一些...
提供对Mybatis-Plus,Quartz,ShardingJdbc,P6sy,Jndi等组件的集成方案。 提供 自定义数据源来源 方案(如全从数据库加载)。 提供项目启动后 动态增加移除数据源 方案。 提供Mybatis环境下的 纯读写分离 方案。 ...
提供对Mybatis-Plus,Quartz,ShardingJdbc,P6sy,Jndi等组件的集成方案。 提供 自定义数据源来源 方案(如全从数据库加载)。 提供项目启动后 动态增加移除数据源 方案。 提供Mybatis环境下的 纯读写分离 方案。 ...
Spring+SpringMVC+Mybatis框架集成公共模块,包括公共配置、MybatisGenerator扩展插件、通用BaseService、工具类等。 > zheng-admin 基于bootstrap实现的响应式Material Design风格的通用后台管理系统,`zheng`...
在基于Spring Boot,Spring Cloud的分布式微服务开发过程中,根据实际项目环境,需要选择,集成符合项目需求的各种组件和积累各种解决方案。基于这样的背景下,我开源了本示例项目,方便大家快速上手Spring Boot,...
已集成组件 springframework-4.1.6.RELEASE freemarker-2.3.22 druid-1.0.14 mybatis-3.3.0 activemq-5.7.0 quartz-2.2.1 slf4j-1.7.12 logback-1.1.3 guava-18.0 apache-commons-xxx 注意事项 目录结构可参考 ./...