Quartz集成Spring基本使用(Web)
目录
2.2配置quartz(quartz.properties)2
2.3配置Spring下的quartz的任务(task.xml)2
2.4配置Spring(applicationContext.xml)2
2.5在web.xml中配置Spring(web.xml)2
3.4配置Spring,applicationContext.xml5
1.所需jar
1)日志管理
commons-logging-1.1.1.jar
log4j-1.2.16.jar
2)spring核心包
3)quartz的spring支持
org.springframework.context.support-3.0.5.RELEASE.jar
org.springframework.transaction-3.0.5.RELEASE.jar
4)quartz及他的log支持
quartz-1.8.4.jar
slf4j-api-1.6.1.jar
slf4j-log4j12-1.6.1.jar
2.集成步骤
2.1开发一个业务job(POJ)
2.2配置quartz(quartz.properties)
2.3配置Spring下的quartz的任务(task.xml)
2.4配置Spring(applicationContext.xml)
2.5在web.xml中配置Spring(web.xml)
3.基本使用源码
3.1开发一个业务job
/**
* @authoryoupp
* job
*/
publicclassPrintReport {
/**
* 调度方法
*/
publicvoid run() {
System.out.println("打印报表");
}
}
3.2配置quartz.properties
#============================================================================
# Configure Main SchedulerProperties
#============================================================================
org.quartz.scheduler.instanceName = EmailScheduler
org.quartz.scheduler.instanceId = AUTO
#============================================================================
# Configure ThreadPool \u914D\u7F6E\u6570\u636E\u5E93\u8FDE\u63A5\u6C60
#============================================================================
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 10
org.quartz.threadPool.threadPriority= 5
#============================================================================
# Configure JobStore \u914D\u7F6E\u505A\u4E1A\u5B58\u50A8\u65B9\u5F0F
#============================================================================
org.quartz.jobStore.class = org.quartz.impl.jdbcstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.useProperties = false
#\u914D\u7F6EdataSource\u540D
org.quartz.jobStore.dataSource = myDS
#\u8868\u524D\u7F00
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.isClustered = true
org.quartz.jobStore.clusterCheckinInterval=20000
#============================================================================
# Configure Datasources \u914D\u7F6E\u6570\u636E\u5E93\u7684\u8FDE\u63A5
#============================================================================
org.quartz.dataSource.myDS2.jndiURL=java\:comp/env/jdbc/insale
org.quartz.dataSource.myDS2.jndiAlwaysLookup=DB_JNDI_ALWAYS_LOOKUP
3.3配置Spring下的任务task.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<!-- 配置在quartz中执行的调度任务 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd">
<!--定义业务处理对象 -->
<beanid="printReport"class="com.pan.quartz.job.PrintReport"/>
<!--定义调度业务逻辑 -->
<beanid="printReportjobDetail"class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!--调用的类 -->
<propertyname="targetObject"ref="printReport"/>
<!--调用的方法 -->
<propertyname="targetMethod"value="run"/>
<!--false表示等上一个任务执行完后再开启新的任务-->
<propertyname="concurrent"value="false"/>
</bean>
<!--定义调度触发器 -->
<beanid="printReportjobDetailcronTrigger"class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<propertyname="jobDetail"ref="printReportjobDetail"/>
<!--cron表达式 -->
<propertyname="cronExpression"value="0/10 * * * * ?"/><!--每10秒执行一次 -->
</bean>
<!--设置调度管理器 -->
<beanid="quartzM"lazy-init="false"autowire="no"class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<propertyname="triggers">
<list>
<ref bean="printReportjobDetailcronTrigger"/>
</list>
</property>
</bean>
</beans>
3.4配置Spring,applicationContext.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
">
<!--引入quartz任务配置 -->
<importresource="classpath:task.xml"/>
</beans>
3.5在web.xml中配置Spring
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"id="WebApp_ID"version="3.0">
<!-- Spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/applicationContext.xml</param-value>
</context-param>
<!-- Spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>