How to use Quartz in web application
(copied from http://cake.wikidot.com/quartz )
General references
Home
http://www.opensymphony.com/quartz/
Wiki (Tutorial , Configuration, CookBook, …)
http://www.opensymphony.com/quartz/wikidocs/
Tutorial
http://www.onjava.com/pub/a/onjava/2005/09/28/what-is-quartz.html?page=1
Examples
Quartz version: 1.6.0
Java 1.6.0
Tomcat 5.5.25
jar files under the WEB-INF/lib
- quartz-1.6.0.jar
- log4j-1.2.11.jar
- commons-beanutils.jar
- commons-collections-3.1.jar
- commons-digester-1.7.jar
- commons-logging-api.jar
- commons-logging.jar
- jta.jar
QuartzInitializerServlet configuration
<servlet>
<servlet-name>QuartzInitializer</servlet-name>
<servlet-class>
org.quartz.ee.servlet.QuartzInitializerServlet
</servlet-class>
<init-param>
<param-name>shutdown-on-unload</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>start-scheduler-on-load</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Copy the 3 following files under WEB-INF\classes
log4j.properties
# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=INFO, A1
# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
quartz.properties
This sample is based on the examples of the Quartz distribution
#============================================================================
# Configure Main Scheduler Properties
#============================================================================
org.quartz.scheduler.instanceName = CakeNoteScheduler
org.quartz.scheduler.instanceId = AUTO
org.quartz.scheduler.rmi.export = false
org.quartz.scheduler.rmi.proxy = false
#============================================================================
# Configure ThreadPool
#============================================================================
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 12
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.impl.jdbcjobstore.JobStoreTX
#org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.PostgreSQLDelegate
#org.quartz.jobStore.useProperties = false
#org.quartz.jobStore.dataSource = myDS
#org.quartz.jobStore.tablePrefix = QRTZ_
#org.quartz.jobStore.isClustered = false
#============================================================================
# Configure Datasources
#============================================================================
#org.quartz.dataSource.myDS.driver = org.postgresql.Driver
#org.quartz.dataSource.myDS.URL = jdbc:postgresql://localhost/dev
#org.quartz.dataSource.myDS.user = jhouse
#org.quartz.dataSource.myDS.password =
#org.quartz.dataSource.myDS.maxConnections = 5
#============================================================================
# JobInitializationPlugin (xml declarative way to trigger jobs)
#============================================================================
org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.JobInitializationPlugin
#org.quartz.plugin.jobInitializer.fileName = quartz_jobs.xml
#org.quartz.plugin.jobInitializer.overWriteExistingJobs = false
#org.quartz.plugin.jobInitializer.failOnFileNotFound = true
quartz_jobs.xml
<?xml version="1.0" encoding="UTF-8"?>
<quartz xmlns="http://www.opensymphony.com/quartz/JobSchedulingData"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.opensymphony.com/quartz/JobSchedulingData
http://www.opensymphony.com/quartz/xml/job_scheduling_data_1_5.xsd"
version="1.5" overwrite-existing-jobs="true">
<job>
<job-detail>
<name>testjob</name>
<group>CAKENOTE_GROUP</group>
<description>Basic test job</description>
<job-class>cakenote.quartz.job.TestJob</job-class>
<job-data-map allows-transient-data="false">
<entry>
<key>param1</key>
<value>value1</value>
</entry>
<entry>
<key>param2</key>
<value>value2</value>
</entry>
</job-data-map>
</job-detail>
<trigger>
<simple>
<name>my-trigger</name>
<group>MYTRIGGER_GROUP</group>
<job-name>testjob</job-name>
<job-group>CAKENOTE_GROUP</job-group>
<!-- repeat indefinitely every 5 seconds -->
<repeat-count>-1</repeat-count>
<repeat-interval>5000</repeat-interval>
</simple>
</trigger>
</job>
</quartz>
Create the following class
package cakenote.quartz.job;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class TestJob implements Job {
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println(context.getJobDetail().getName());
}
}
0 Comments:
Post a Comment
<< Home