一个非常简单的quartz例子

一个非常简单的quartz例子

在quartz中一个作业实例必须实现org.quartz.Job接口
如:

package com.unicom.gdnum.jobs;

import java.util.*;

import org.apache.commons.logging.*;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class HelloworldJob implements Job{
 
 static Log log=LogFactory.getLog(HelloworldJob.class);
 public  HelloworldJob() {
  
 }
 public void execute(JobExecutionContext arg0) throws JobExecutionException {
  // TODO Auto-generated method stub
  log.info("Hello World Quartz......."+(new Date()).toLocaleString());
 }
}


quartz有自己的配置文个名为quartz.properties,如果我们不在src/(根目录)如果我们不建立文件那么quartz就会使用quartz.jar包里的这个的文件。该文件通常包含以下内容:

#
# Configure Main Scheduler Properties
#

org.quartz.scheduler.instanceName = TestScheduler
org.quartz.scheduler.instanceId = AUTO

#
# Configure ThreadPool
#

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

#
# Configure JobStore
#

org.quartz.jobStore.misfireThreshold = 5000

org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore

# ===========================================================================
# Configure SchedulerPlugins  ===============================================
# ===========================================================================
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所需的配置,其中org.quartz.plugin.jobInitializer.fileName = /quartz_job.xml指定作业配置文件名,下面我是为HelloworldJob 写的一个配置,quartz_job.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<quartz>
  <job>
    <job-detail>
      <name>helloworld</name>
      <group>group1</group>
      <job-class>com.unicom.gdnum.jobs.HelloworldJob</job-class>
    </job-detail>
    <trigger>
      <cron>
        <name>test</name>
        <group>group1</group>
        <job-name>helloworld</job-name>
        <job-group>group1</job-group>
        <cron-expression>0 0/1 * * * ?</cron-expression>
     </cron>
    </trigger>
  </job>
</quartz>

这是在tomcat中作的一个例子,当然要使用quartz面要下载其相关包!

你可能感兴趣的:(一个非常简单的quartz例子)