准备工作
导入jar包:WEB-INFlibquartz-1.8.5.jar
web.xml中的配置如下
<!-- quartz job start --> <context-param> <param-name>quartz:wait-on-shutdown</param-name> <param-value>true</param-value> </context-param> <servlet> <servlet-name>QuartzInitializer</servlet-name> <servlet-class>org.quartz.ee.servlet.QuartzInitializerServlet</servlet-class> <init-param> <param-name>config-file</param-name> <param-value>config/quartz_jobs.properties</param-value> </init-param> <init-param> <param-name>shutdown-on-unload</param-name> <param-value>true</param-value> </init-param> <init-param> <!-- Start delay time 60 seconds --> <param-name>start-delay-seconds</param-name> <param-value>60</param-value> </init-param> <init-param> <param-name>start-scheduler-on-load</param-name> <param-value>true</param-value> </init-param> <load-on-startup> 5 </load-on-startup> </servlet> <!-- quartz job end -->
web.xml中配置的config/quartz_jobs.properties文件内的详细内容如下
#============================================================================
# Configure Main Scheduler Properties
#============================================================================
org.quartz.scheduler.instanceName = TestScheduler
org.quartz.scheduler.instanceId = SchedulerTID
#============================================================================
# Configure ThreadPool
#============================================================================
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 3
org.quartz.threadPool.threadPriority = 5
#============================================================================
# Configure JobStore
#============================================================================
org.quartz.jobStore.misfireThreshold = 60000
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
#org.quartz.jobStore.class = org.quartz.simpl.JDBCJobStore
#org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
#org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
#org.quartz.jobStore.useProperties = false
#config dataSource
#org.quartz.jobStore.dataSource = myDS
#table prefix
#org.quartz.jobStore.tablePrefix = QRTZ_
#setting true Activate cluster characteristics
#org.quartz.jobStore.isClustered = false
#org.quartz.jobStore.maxMisfiresToHandleAtATime=1
#============================================================================
# Configure Datasources connect
#============================================================================
#one
#org.quartz.dataSource.myDS.driver = com.microsoft.jdbc.sqlserver.SQLServerDriver
##jdbc:microsoft:sqlserver://CANGZDWSQL10;DatabaseName=COMPUPTC
#org.quartz.dataSource.myDS.URL = jdbc:microsoft:sqlserver://CANGZDWSQL10:1433;DatabaseName=COMPUPTC
#org.quartz.dataSource.myDS.user = fvpuser
#org.quartz.dataSource.myDS.password = fvpuser
#org.quartz.dataSource.myDS.maxConnections = 5
# tow jndi
#dataSourceçJNDI url
#org.quartz.dataSource.myDS.jndiURL = java:comp/env/jdbc/app4-hdcp3r7ccmp01-fvp
## used JNDI InitialContextFactory class name.
#org.quartz.dataSource.myDS.java.naming.factory.initial =
##JNDI URL
#org.quartz.dataSource.myDS.java.naming.provider.url =
##JNDI user name
#org.quartz.dataSource.myDS.java.naming.security.principal = fvpuser
##connect to JNDI
#org.quartz.dataSource.myDS.java.naming.security.credentials = fvpuser
#============================================================================
# Configure Plugins
#============================================================================
org.quartz.plugin.triggHistory.class = org.quartz.plugins.history.LoggingJobHistoryPlugin
# v1.8 before JobInitializationPlugin
#org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.JobInitializationPlugin
org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin
# /WEB-INF/config/quartz_jobs.xml,/WEB-INF/config/quartz_savejobs.xml
org.quartz.plugin.jobInitializer.fileNames = /WEB-INF/quartzjobs.xml
#org.quartz.plugin.jobInitializer.overWriteExistingJobs = false
org.quartz.plugin.jobInitializer.failOnFileNotFound = true
org.quartz.plugin.jobInitializer.scanInterval = 10
org.quartz.plugin.jobInitializer.wrapInUserTransaction = false
quartz_jobs.properties文件内的/WEB-INF/quartzjobs.xml 详细配置 主要是指定去定时执行那个类 请看配置如下
<?xml version='1.0' encoding='utf-8'?> <job-scheduling-data xmlns="http://www.quartz-scheduler.org/xml/JobSchedulingData" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.quartz-scheduler.org/xml/JobSchedulingData http://www.quartz-scheduler.org/xml/job_scheduling_data_1_8.xsd" version="1.8"> <schedule> <job> <name> ws_soap_job</name> <group>ErrLog</group> <description>The job description </description> <job-class>com.gmw9.quartz.WsSoapQuartzJob</job-class> </job> <trigger> <cron> <name>wsSoap-trigger</name> <group>ErrLog_GROUP</group> <job-name>ws_soap_job</job-name> <job-group>ErrLog</job-group> <cron-expression>0 0 8,12,16,20 * * ?</cron-expression> </cron> </trigger> <!-- <job> <name>jabName</name> <group>upload</group> <description>The job description </description> <job-class>com.gmw9.quartz.hongshanQuartzJob</job-class> </job> <trigger> <cron> <name>uploadFile-trigger</name> <group>upload_GROUP</group> <job-name>jabName</job-name> <job-group>upload</job-group> <cron-expression>0 10 15 22 4 ? 2016</cron-expression> </cron> </trigger> --> </schedule> </job-scheduling-data>
被执行类的案例代码如下
package com.gmw9.quartz; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class WsSoapQuartzJob implements Job { private final String JOBNAME = Constant.JOBNAME; private final static Log log = LogFactory.getLog(WsSoapQuartzJob.class); //定时任务框架会调用这个方法 @Override public void execute(JobExecutionContext arg0) throws JobExecutionException { System.out.println("IVS wsSoap quartz job launch time ..." + System.currentTimeMillis()); this.wsSoapCall(); } //这里就是定时执行的内容 public void wsSoapCall(){ System.out.println("========wsSoap Call======="); } }