Quartz 框架快速入門(三)&n…

項目用到jar包

quartz-1.7.3.jar

quartz-all-1.7.3.jar

 

加載Quartz 類

 

package com.longcredit.gxmx.quartz;

import java.io.FileInputStream;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
import org.tanukisoftware.wrapper.WrapperListener;
import org.tanukisoftware.wrapper.WrapperManager;
import com.jolbox.bonecp.BoneCPDataSource;
import com.longcredit.gxmx.command.ClsSystem;

import ey.db.boncp.BonecpDB2Config;
import ey.db.boncp.BonecpMySqlConfig;
import ey.db.db2.DB2ServerParameter;
import ey.db.mysql.MySqlServerParameter;

public class Schedulers implements WrapperListener {
 
 private static Logger log = Logger.getLogger(Schedulers.class);

 
 public static BoneCPDataSource bcpds = null;
 
 Scheduler scheduler = null;

 public static void main(String[] args) {
   new Schedulers().Startrun();

 }

 public static void out(Object msg) {
  log.info(msg);
 }

 public void Startrun() {
  out("---------調度框架開始------------");
  try {
   //加載配置文件或者數據庫連接池

   //........
   //加載調度配置文件
   FileInputStream fileInputStream = null;
   Properties properties = null;
   try {
    log.info("讀取 quartz.properties配置文件");

    String filepath = MessageFormat.format("{0}{1}",
      ClsSystem.getClassesPath(), "quartz.properties");
    fileInputStream = new FileInputStream(filepath);
    properties = new Properties();
    properties.load(fileInputStream);
    log.info("成功讀取quartz.properties 配置文件");

    log.info("配置Quartz信息");
    // 創建任務調度器羣
    SchedulerFactory sf = new StdSchedulerFactory(properties);
    // 其中一個調度器
    this.scheduler = sf.getScheduler();
    this.scheduler.start();
    log.info("配置Quartz信息成功");
   } catch (Exception e) {
    log.error("加載quartz.properties配置文件出錯:錯誤原因如下:" + e.getMessage());
   } finally {
    properties.clear();
    properties = null;
    try {
     fileInputStream.close();
    } catch (IOException e) {
    }
    fileInputStream = null;
   }
  }catch(Exception e){
   log.error("加載連接池配置文件出錯:錯誤原因如下:" + e.getMessage());
  }
 }

 @Override
 public void controlEvent(int event) {
  if ((WrapperManager.isControlledByNativeWrapper())
    || ((event != 200) && (event != 201) && (event != 203))) {
   return;
  } else {
   WrapperManager.stop(0);
  }
 }

 @Override
 public Integer start(String[] arg0) {
  Startrun();
  out("啓動成功.....");
  return null;
 }

 @Override
 public int stop(int arg0) {
  try {
   if (this.scheduler != null) {
    // 停止任務調度
    out("停止任務調度");
    this.scheduler.shutdown();
    if (Schedulers.bcpds != null) {
     // 關閉數據庫鏈接
     Schedulers.bcpds.close();
    }
    out("停止任務調度成功");
   }
  } catch (SchedulerException e) {
  }
  return 0;
 }

}

 

quartz.properties 文件配置

 

# Default Properties file for use by StdSchedulerFactory
# to create a Quartz Scheduler Instance, if a different
# properties file is not explicitly specified.
#
#===============================================================    
# Quartz 調度屬性 
#===============================================================
# 調度器的實例名 
org.quartz.scheduler.instanceName: DefaultQuartzScheduler
org.quartz.scheduler.rmi.export: false
org.quartz.scheduler.rmi.proxy: false
org.quartz.scheduler.wrapJobExecutionInUserTransaction: false

#===============================================================    
# Quartz 線程池屬性
#===============================================================
# 這個值是一個實現了 org.quartz.spi.ThreadPool 接口的類的全限名稱
org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool
# 最大工作線程
org.quartz.threadPool.threadCount: 20
# 工作線程優先級 最小爲1,最大爲10
org.quartz.threadPool.threadPriority: 7
# 工作線程是否爲守護線程
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread: true

# 最大能忍受的觸發超時時間,如果超過則認爲"失敗"
org.quartz.jobStore.misfireThreshold: 60000
# 將schedule相關信息保存在RAM中,輕量級,速度快,遺憾的是應用重啓時相關信息都將丟失
# org.quartz.jobStore.class: org.quartz.simpl.RAMJobStore

#============================================================================
# Quartz 插件設置
#============================================================================
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 = quartz_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

org.quartz.plugin.shutdownhook.class = org.quartz.plugins.management.ShutdownHookPlugin 
org.quartz.plugin.shutdownhook.cleanShutdown = 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">
 <job>
  <job-detail>
   <name>001</name>
   <!-- 屬於分組 -->
   <group>DEFAULT</group>
   <description>
    普通訂單執行   
       </description>
   <job-class>
    com.longcredit.gxmx.quartz.job.CreateTree
       </job-class>
   <volatility>false</volatility>
   <durability>false</durability>
   <recover>false</recover>
   <!-- 是否執行任務信息存儲 -->
   <job-data-map allows-transient-data="false">
    <entry>
     <!-- 存儲名稱 -->
     <key>SCAN_DIR</key>
     <!-- 存儲路徑 -->
     <value>E:\conf1</value>
    </entry>
   </job-data-map>
  </job-detail>
  <trigger>
   <simple>
    <name>Trigger_001</name>
    <group>DEFAULT</group>
    <job-name>001</job-name>
    <job-group>DEFAULT</job-group>
    <!-- 開始時間 -->
    <start-time>2010-09-03T14:43:00</start-time>
    <repeat-count>1</repeat-count>
    <!-- 間隔時間(毫秒) -->
    <repeat-interval>120000</repeat-interval>
   </simple>
  </trigger>
 </job>
</quartz>


定時調度類

 

public class CreateTree implements Job, StatefulJob {

   @Override
   public void execute(JobExecutionContext context)
         throws JobExecutionException {

       //內容

   }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章