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 {

       //内容

   }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值