作业调度之quartz

quartz是一个相对于JDKTimer更加强健的作业调度框架,它不但可以在单机环境下运行,更提供了一套集群环境下的解决方案。Quartz在集群环境下的使用方式很简单,只需要在配置文件里配置即可,不过需要数据库的支持。

首先需要实现org.quartz.Job接口,在execute方法中调用需要执行的作业。

 

public class SendBillPerMonthJob implements Job { private static Logger log = Logger .getLogger(SendBillPerMonthJob.class); public void execute(JobExecutionContext context) throws JobExecutionException { log.info("Start to execute SendBillPerMonthJob at " + new Timestamp(System.currentTimeMillis())); try { String instanceID = context.getScheduler().getSchedulerInstanceId(); log.info("Job is executed by node " + instanceID); } catch (SchedulerException e) { e.printStackTrace(); } // Every job has its own job detail JobDetail jobDetail = context.getJobDetail(); // The name is defined in the job definition String jobName = jobDetail.getName(); // Log the time the job started log.info(jobName + " fired at " + new Date()); // The directory to scan is stored in the job map JobDataMap dataMap = jobDetail.getJobDataMap(); String delayDayCount = dataMap.getString(MAP_KEY_REMAINING_DAY_COUNT); //Send bill

//Start to call send bill service log.info("End to execute SendBillPerMonthJob at " + new Timestamp(System.currentTimeMillis())); } }

 

接下来就需要配置了,quartz可作为一个独立的组件随AppServer启动而开始执行,这样就需要在web.xml中加入以下配置

<context-param> <param-name>quartz:config-file</param-name> <param-value>/quartz.properties</param-value> </context-param> <context-param> <param-name>quartz:shutdown-on-unload</param-name> <param-value>true</param-value> </context-param> <context-param> <param-name>quartz:start-on-load</param-name> <param-value>true</param-value> </context-param> <listener> <listener-class>org.quartz.ee.servlet.QuartzInitializerListener</listener-class> </listener>

 

QuartzInitializerListener会加载位于classes下的quartz.properties,在quartz.properties你需要进行以下配置

#============================================================================ # Configure Main Scheduler Properties #============================================================================ org.quartz.scheduler.instanceName=BillScheduler org.quartz.scheduler.instanceId=AUTO org.quartz.scheduler.skipUpdateCheck=true #============================================================================ # Configure ThreadPool #============================================================================ org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool org.quartz.threadPool.threadCount=5 org.quartz.threadPool.threadPriority=5 #============================================================================ # Configure JobStore #============================================================================ org.quartz.jobStore.misfireThreshold=60000 org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate org.quartz.jobStore.useProperties=false org.quartz.jobStore.dataSource=myDS org.quartz.jobStore.tablePrefix=QRTZ_

#如果是集群环境,这里需要配置为true org.quartz.jobStore.isClustered=false org.quartz.jobStore.clusterCheckinInterval=20000 #============================================================================ # Other Example Delegates #============================================================================ #org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.CloudscapeDelegate #org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.DB2v6Delegate #org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.DB2v7Delegate #org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.DriverDelegate #org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.HSQLDBDelegate #org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.MSSQLDelegate #org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.PointbaseDelegate #org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.PostgreSQLDelegate #org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate #org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.WebLogicDelegate #org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.oracle.OracleDelegate #org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.oracle.WebLogicOracleDelegate #============================================================================ # Configure Datasources #============================================================================ org.quartz.dataSource.myDS.driver=com.ibm.db2.jcc.DB2Driver org.quartz.dataSource.myDS.URL=jdbc:db2://localhost:50005/QuartzDB org.quartz.dataSource.myDS.user=db2inst1 org.quartz.dataSource.myDS.password=password org.quartz.dataSource.myDS.maxConnections=10 #org.quartz.dataSource.myDS.validationQuery= #============================================================================ # Configure Plugins #============================================================================ #org.quartz.plugin.shutdownHook.class = org.quartz.plugins.management.ShutdownHookPlugin #org.quartz.plugin.shutdownHook.cleanShutdown = true org.quartz.plugin.triggHistory.class=org.quartz.plugins.history.LoggingJobHistoryPlugin

#配置此插件读取quartz_jobs.xml

  org.quartz.plugin.jobInitializer.class=org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin org.quartz.plugin.jobInitializer.fileNames=quartz_jobs.xml org.quartz.plugin.jobInitializer.failOnFileNotFound=true org.quartz.plugin.jobInitializer.scanInterval=120 org.quartz.plugin.jobInitializer.wrapInUserTransaction=false

最后,需要在quartz_jobs.xml中定义作业调度的相关信息。

<?xml version="1.0" encoding="UTF-8"?> <job-scheduling-data xmlns="http://www.quartz-scheduler.org/xml/JobSchedulingData" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.quartz-scheduler.org/xml/JobSchedulingData http://www.quartz-scheduler.org/xml/job_scheduling_data_1_8.xsd" version="1.8"> <pre-processing-commands> <delete-jobs-in-group>*</delete-jobs-in-group> <!-- clear all jobs in scheduler --> <delete-triggers-in-group>*</delete-triggers-in-group> <!-- clear all triggers in scheduler --> </pre-processing-commands> <processing-directives> <!-- if there are any jobs/trigger in scheduler of same name (as in this file), overwrite them --> <overwrite-existing-data>true</overwrite-existing-data> <!-- if there are any jobs/trigger in scheduler of same name (as in this file), and over-write is false, ignore them rather then generating an error --> <ignore-duplicates>true</ignore-duplicates> </processing-directives> <schedule> <job> <name>SendBillPerMonth</name> <job-class>com.job.SendBillPerMonthJob</job-class> <volatility>false</volatility> <durability>false</durability> <recover>false</recover> <job-data-map> <entry> <key>remainingDayCount</key> <value>5</value> </entry> </job-data-map> </job> <trigger> <cron> <name>SendBillPerMonthTrigger</name> <job-name>SendBillPerMonth</job-name> <cron-expression>0 30 0 1 * ?</cron-expression> </cron> </trigger> </schedule> </job-scheduling-data>

 

作业的调度方式采用了cron表达式,此例表示每月1号发送一次账单。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值