quartz + xml + java代码加载(非spring配置方式)的应用

(一)注意:
    1.quartz.jar用1.6版本的,版本问题纠结很久
    2.所用到jar:
     commons-beanutils.jar
     commons-collections-3.1.jar
     commons-digester.jar
     commons-logging.jar
     jta-1.1.jar
     quartz-1.6.5.jar
(二)HelloWorldQuartzJob.java

package com.quartz;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class HelloWorldQuartzJob implements Job {

@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
// TODO Auto-generated method stub
System.out.println("");
System.out.println("==============================");
         System.out.println("Time("+formateDate(new Date())+") - Hello World Quartz (xml)");
System.out.println("==============================");
System.out.println("");
}

static String formateDate(Date date) {
SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
return formate.format(date);
}

}

(三)配置job.xml
<?xml version='1.0' encoding='utf-8'?>
<quartz>
<job>
   <job-detail>
    <name>test</name>
    <group>DEFAULT</group>
    <description>testJobhere</description>
    <job-class>com.quartz.HelloWorldQuartzJob</job-class>
  </job-detail>
   <trigger>
             <cron>
                  <name>testTrigger</name>
                  <group>DEFAULT</group>
                  <job-name>test</job-name>
                 <job-group>DEFALUT</job-group>
                 <cron-expression>0/5 * * * * ?</cron-expression>
             </cron>
       </trigger>
  </job>
</quartz>

(四)quartz.properties
#============================================================================
# Configure Main Scheduler Properties 
#============================================================================

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

#============================================================================
# Configure ThreadPool 
#============================================================================

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

#============================================================================
# Configure JobStore 
#============================================================================

org.quartz.jobStore.misfireThreshold = 60000

#============================================================================
# Configure Plugins
#============================================================================

org.quartz.plugin.triggHistory.class = org.quartz.plugins.history.LoggingJobHistoryPlugin

org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.JobInitializationPlugin
org.quartz.plugin.jobInitializer.fileNames = conf/jobs.xml
org.quartz.plugin.jobInitializer.overWriteExistingJobs = true
org.quartz.plugin.jobInitializer.failOnFileNotFound = true
org.quartz.plugin.jobInitializer.scanInterval = 10
org.quartz.plugin.jobInitializer.wrapInUserTransaction = false

(五)启动类
package startup;

import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;

public class Main {

public static void main(String[] args) throws SchedulerException {
//First we must get a reference to a scheduler
StdSchedulerFactory sf = getSchedulerFactory("conf/quartz.properties");
Scheduler sched = sf.getScheduler();
sched.start();
}

protected static StdSchedulerFactory getSchedulerFactory(String configFile)
    throws SchedulerException
  {
    StdSchedulerFactory factory;
    if (configFile != null)
      factory = new StdSchedulerFactory(configFile);
    else {
      factory = new StdSchedulerFactory();
    }
    return factory;
  }
}

调试结果如下:
三月 09, 2017 5:46:53 下午 org.quartz.core.SchedulerSignalerImpl <init>
信息: Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
三月 09, 2017 5:46:53 下午 org.quartz.core.QuartzScheduler <init>
信息: Quartz Scheduler v.1.6.5 created.
三月 09, 2017 5:46:53 下午 org.quartz.plugins.xml.JobInitializationPlugin initialize
信息: Registering Quartz Job Initialization Plug-in.
三月 09, 2017 5:46:53 下午 org.quartz.simpl.RAMJobStore initialize
信息: RAMJobStore initialized.
三月 09, 2017 5:46:53 下午 org.quartz.impl.StdSchedulerFactory instantiate
信息: Quartz scheduler 'MyScheduler' initialized from the specified file : 'conf/quartz.properties'
三月 09, 2017 5:46:53 下午 org.quartz.impl.StdSchedulerFactory instantiate
信息: Quartz scheduler version: 1.6.5
三月 09, 2017 5:46:53 下午 org.quartz.xml.JobSchedulingDataProcessor processFile
信息: Parsing XML file: conf/jobs.xml with systemId: conf/jobs.xml validating: false validating schema: jar:file:/D:/User/ee-keeper/workspace/quartz-example/lib/quartz-1.6.5.jar!/org/quartz/xml/job_scheduling_data_1_5.xsd
三月 09, 2017 5:46:53 下午 org.quartz.xml.JobSchedulingDataProcessor scheduleJobs
信息: Scheduling 1 parsed jobs.
三月 09, 2017 5:46:53 下午 org.quartz.xml.JobSchedulingDataProcessor scheduleJob
信息: Adding job: DEFAULT.test
三月 09, 2017 5:46:53 下午 org.quartz.xml.JobSchedulingDataProcessor scheduleJobs
信息: 1 scheduled jobs.
三月 09, 2017 5:46:53 下午 org.quartz.core.QuartzScheduler start
信息: Scheduler MyScheduler_$_NON_CLUSTERED started.
三月 09, 2017 5:46:53 下午 org.quartz.plugins.history.LoggingJobHistoryPlugin jobToBeExecuted
信息: Job JobInitializationPlugin.JobInitializationPlugin_jobInitializer_jobs_xml fired (by trigger JobInitializationPlugin.JobInitializationPlugin_jobInitializer_jobs_xml) at:  17:46:53 03/09/2017
三月 09, 2017 5:46:53 下午 org.quartz.plugins.history.LoggingJobHistoryPlugin jobWasExecuted
信息: Job JobInitializationPlugin.JobInitializationPlugin_jobInitializer_jobs_xml execution complete at  17:46:53 03/09/2017 and reports: null
三月 09, 2017 5:46:55 下午 org.quartz.plugins.history.LoggingJobHistoryPlugin jobToBeExecuted
信息: Job DEFAULT.test fired (by trigger DEFAULT.testTrigger) at:  17:46:55 03/09/2017

==============================
Time(2017-03-09 17:46:55.004) - Hello World Quartz (xml)
==============================


三月 09, 2017 5:46:55 下午 org.quartz.plugins.history.LoggingJobHistoryPlugin jobWasExecuted
信息: Job DEFAULT.test execution complete at  17:46:55 03/09/2017 and reports: null
三月 09, 2017 5:47:00 下午 org.quartz.plugins.history.LoggingJobHistoryPlugin jobToBeExecuted
信息: Job DEFAULT.test fired (by trigger DEFAULT.testTrigger) at:  17:47:00 03/09/2017

==============================
Time(2017-03-09 17:47:00.003) - Hello World Quartz (xml)
==============================


三月 09, 2017 5:47:00 下午 org.quartz.plugins.history.LoggingJobHistoryPlugin jobWasExecuted
信息: Job DEFAULT.test execution complete at  17:47:00 03/09/2017 and reports: null
三月 09, 2017 5:47:03 下午 org.quartz.plugins.history.LoggingJobHistoryPlugin jobToBeExecuted
信息: Job JobInitializationPlugin.JobInitializationPlugin_jobInitializer_jobs_xml fired (by trigger JobInitializationPlugin.JobInitializationPlugin_jobInitializer_jobs_xml) at:  17:47:03 03/09/2017
三月 09, 2017 5:47:03 下午 org.quartz.plugins.history.LoggingJobHistoryPlugin jobWasExecuted
信息: Job JobInitializationPlugin.JobInitializationPlugin_jobInitializer_jobs_xml execution complete at  17:47:03 03/09/2017 and reports: null

==============================
Time(2017-03-09 17:47:05.003) - Hello World Quartz (xml)
==============================


三月 09, 2017 5:47:05 下午 org.quartz.plugins.history.LoggingJobHistoryPlugin jobToBeExecuted
信息: Job DEFAULT.test fired (by trigger DEFAULT.testTrigger) at:  17:47:05 03/09/2017
三月 09, 2017 5:47:05 下午 org.quartz.plugins.history.LoggingJobHistoryPlugin jobWasExecuted
信息: Job DEFAULT.test execution complete at  17:47:05 03/09/2017 and reports: null
三月 09, 2017 5:47:10 下午 org.quartz.plugins.history.LoggingJobHistoryPlugin jobToBeExecuted
信息: Job DEFAULT.test fired (by trigger DEFAULT.testTrigger) at:  17:47:10 03/09/2017

==============================
Time(2017-03-09 17:47:10.004) - Hello World Quartz (xml)
==============================


三月 09, 2017 5:47:10 下午 org.quartz.plugins.history.LoggingJobHistoryPlugin jobWasExecuted
信息: Job DEFAULT.test execution complete at  17:47:10 03/09/2017 and reports: null
三月 09, 2017 5:47:13 下午 org.quartz.plugins.history.LoggingJobHistoryPlugin jobToBeExecuted
信息: Job JobInitializationPlugin.JobInitializationPlugin_jobInitializer_jobs_xml fired (by trigger JobInitializationPlugin.JobInitializationPlugin_jobInitializer_jobs_xml) at:  17:47:13 03/09/2017
三月 09, 2017 5:47:13 下午 org.quartz.plugins.history.LoggingJobHistoryPlugin jobWasExecuted
信息: Job JobInitializationPlugin.JobInitializationPlugin_jobInitializer_jobs_xml execution complete at  17:47:13 03/09/2017 and reports: null

==============================
三月 09, 2017 5:47:15 下午 org.quartz.plugins.history.LoggingJobHistoryPlugin jobToBeExecuted
信息: Job DEFAULT.test fired (by trigger DEFAULT.testTrigger) at:  17:47:15 03/09/2017
三月 09, 2017 5:47:15 下午 org.quartz.plugins.history.LoggingJobHistoryPlugin jobWasExecuted
信息: Job DEFAULT.test execution complete at  17:47:15 03/09/2017 and reports: null
Time(2017-03-09 17:47:15.003) - Hello World Quartz (xml)
==============================


三月 09, 2017 5:47:20 下午 org.quartz.plugins.history.LoggingJobHistoryPlugin jobToBeExecuted
信息: Job DEFAULT.test fired (by trigger DEFAULT.testTrigger) at:  17:47:20 03/09/2017

==============================
Time(2017-03-09 17:47:20.003) - Hello World Quartz (xml)
==============================


三月 09, 2017 5:47:20 下午 org.quartz.plugins.history.LoggingJobHistoryPlugin jobWasExecuted
信息: Job DEFAULT.test execution complete at  17:47:20 03/09/2017 and reports: null
三月 09, 2017 5:47:23 下午 org.quartz.plugins.history.LoggingJobHistoryPlugin jobToBeExecuted
信息: Job JobInitializationPlugin.JobInitializationPlugin_jobInitializer_jobs_xml fired (by trigger JobInitializationPlugin.JobInitializationPlugin_jobInitializer_jobs_xml) at:  17:47:23 03/09/2017
三月 09, 2017 5:47:23 下午 org.quartz.plugins.history.LoggingJobHistoryPlugin jobWasExecuted
信息: Job JobInitializationPlugin.JobInitializationPlugin_jobInitializer_jobs_xml execution complete at  17:47:23 03/09/2017 and reports: null

==============================
Time(2017-03-09 17:47:25.004) - Hello World Quartz (xml)
==============================


三月 09, 2017 5:47:25 下午 org.quartz.plugins.history.LoggingJobHistoryPlugin jobToBeExecuted
信息: Job DEFAULT.test fired (by trigger DEFAULT.testTrigger) at:  17:47:25 03/09/2017
三月 09, 2017 5:47:25 下午 org.quartz.plugins.history.LoggingJobHistoryPlugin jobWasExecuted
信息: Job DEFAULT.test execution complete at  17:47:25 03/09/2017 and reports: null

猜你喜欢

转载自wobenqinren.iteye.com/blog/2361259