Quartz

本文应用版本为quartz-all-1.6.6.jar

CronQuartzJob.java代码如下:
import java.util.Date;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class CronQuartzJob implements Job {

	public CronQuartzJob() {
		
	}

	public void execute(JobExecutionContext context) throws JobExecutionException {
		System.out.println("In SimpleQuartzJob - executing its JOB at " + DateFormatUtils.format(new Date(), "yyyy-MM-dd") + " by " + context.getTrigger().getName());

	}

}


quartz.properties代码如下:
org.quartz.scheduler.instanceName = ShgfmisScheduler
org.quartz.scheduler.instanceId = shgfmis

org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 2
org.quartz.threadPool.threadPriority = 4


# org.quartz.plugin.triggHistory.class = org.quartz.plugins.history.LoggingJobHistoryPlugin
org.quartz.plugin.triggHistory.class = org.quartz.plugins.history.LoggingTriggerHistoryPlugin
# org.quartz.plugin.triggHistory.triggerFiredMessage = Trigger{1}.{0} fired job {6}.{5} at: {4, date, HH:mm:ss MM/dd/yyyy}
# org.quartz.plugin.triggHistory.triggerCompleteMessage = Trigger{1}.{0} completed firing job {6}.{5} at: {4, date, HH:mm:ss MM/dd/yyyy} with resulting trigger instruction code: {9}
org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.JobInitializationPlugin
org.quartz.plugin.jobInitializer.fileName = quartz_job.xml

org.quartz.plugin.jobInitializer.overWriteExistingJobs = false
org.quartz.plugin.jobInitializer.failOnFileNotFound = true
org.quartz.plugin.shutdownhook.class = org.quartz.plugins.management.ShutdownHookPlugin
org.quartz.plugin.shutdownhook.cleanShutdown =true


quartz_job.xml代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<quartz>
 <job>

   <job-detail>  
         <name>RunQuartzJob</name>  
          <group>DEFAULT</group>  
          <description>RunQuartzJob</description>  
          <job-class>com.quartz.test.CronQuartzJob</job-class>  
          <volatility>false</volatility>  
          <durability>false</durability>  
          <recover>false</recover>   
      </job-detail>

      <trigger> 
         <cron>
          <name>RunQuartzJobTrigger</name>  
             <group>DEFAULT</group>  
             <description>RunQuartzJobTrigger</description>
             <job-name>RunQuartzJob</job-name>  
             <job-group>DEFAULT</job-group> 
             
             <cron-expression>0/5 * * * * ?</cron-expression>
         </cron>  
      </trigger>
      
 </job>
</quartz>


web.xml中的代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
 xmlns="http://java.sun.com/xml/ns/javaee" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<context-param>
     	<param-name>config-file</param-name>
     	<param-value>/quartz.properties</param-value>
     </context-param>
     <context-param>
     	<param-name>shutdown-on-unload</param-name>
     	<param-value>true</param-value>
     </context-param>
     <context-param>
     	<param-name>start-scheduler-on-load</param-name>
     	<param-value>true</param-value>
     </context-param>
     
     <listener>
     	<listener-class>org.quartz.ee.servlet.QuartzInitializerListener</listener-class>
     </listener>
     
     
     <!--
     <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>/quartz.properties</param-value>
     	</init-param>
     	<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>
     
	
	<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>/quartz.properties</param-value>
         </init-param>
         <init-param>
             <param-name>shutdown-on-unload</param-name>
             <param-value>true</param-value>
         </init-param>
         <load-on-startup>1</load-on-startup>
     </servlet>
	-->

   <welcome-file-list>
     <welcome-file>index.jsp</welcome-file>
   </welcome-file-list>
</web-app>


【注】:工程中需要的jar如下:

quartz-all-1.6.6.jar

commons-beanutils-1.8.3.jar

commons-collections-3.2.1.jar

commons-configuration-1.6.jar

commons-dbcp-1.4.jar

commons-digester3-3.0.jar

commons-digester-2.1.jar

commons-lang3-3.0.1.jar

commons-logging-1.1.1.jar

commons-modeler-2.0.jar

commons-pool-1.5.6.jar

commons-validator-1.3.1.jar

jms.jar

jms.jar

jta.jar

log4j-1.2.16.jar



附件里实例代码。

----------------------------------------------------------------------
【说明】:上述工程的jar包组在Tomcat6和Jboss4下没问题,但在WebLogic10.3下就会报:
ERROR [[ACTIVE] ExecuteThread: '0' for queue: 'weblogic.kernel.Default (self-tuning)']: (Digester.java:794) - Digester.getParser:
java.lang.UnsupportedOperationException: This parser does not support specification " null" version " null"
含义:解析XML的文件版本不匹配。
【解决办法】:把上述工程的jar组中的 commons-digester3-3.0.jarcommons-digester-2.1.jar删除掉,换成
              commons-digester-1.8.jar即可,原因是WebLogic自带的xml解析器与apache的
              commons-digester-2.1.jar以上的版本冲突,WebLogic不能兼容太高版本的xml解析器。

你可能感兴趣的:(quartz,javaee)