使用quratz定时调用

来源:互联网 发布:上海老程宝马 淘宝店 编辑:程序博客网 时间:2024/06/05 19:59

最近在工作上需要定时调用的功能,定时调用在web中应用还是蛮多的,比如说课表提醒,每天晚上提醒一遍有什么课要上啦,或者就像目前我需要完成的功能,定时的一个数据库的数据导入到另一个数据库中。

而quratz是一个完全由java编写的开源作业调度框架。尽管Quartz框架整合了许多额外功能, 但就其简易形式看,你会发现它易用得简直让人受不了!使用起来确实特别方便,也很实用。

废话不多说,直接上!

首先需要quratz相关jar包,我使用的是quartz-2.2.1.jar。

接下来就需要一个job执行类和一个任务调度类。

任务调度类,在该类中制定corn表达式,通过该表达式来按你的要求来实现定时方式,比如每天某个时间运行。

package com.gisquest.djgx.modules.services;import java.util.Properties;import org.quartz.CronScheduleBuilder;import org.quartz.CronTrigger;import org.quartz.JobBuilder;import org.quartz.JobDetail;  import org.quartz.Scheduler;  import org.quartz.SchedulerFactory;  import org.quartz.TriggerBuilder;import org.quartz.impl.StdSchedulerFactory;  import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import org.springframework.transaction.annotation.Transactional;import com.gisquest.djgx.modules.sys.entity.WsDefine;import com.gisquest.djgx.modules.sys.utils.DictUtils;@Component@Transactional(readOnly = true)public class InvokeService {    SchedulerFactory sf = null;    Scheduler sched = null;// 点击启动按钮调用的方法public void scheduler(WsDefine wsDefine,String basePath,String id) {String deftime = wsDefine.getDeftime();String month = "*";String year = "?";String schedName = "schedu"+id;String jobName = "job"+id;String groupName = "group"+id;String triggerName = "trigger"+id;String schedString="";try {Properties props = new Properties();props.put("org.quartz.scheduler.instanceName", schedName);props.put("org.quartz.threadPool.threadCount", "100");sf = new StdSchedulerFactory(props);if(sf.getScheduler(schedName) != null){sched = sf.getScheduler(schedName);  // 初始化调度器}else{sched = sf.getScheduler();  // 初始化调度器}JobDetail job = JobBuilder.newJob(QuartzJob.class).withIdentity(jobName, groupName).build(); // 设置作业,具体操作在QuartzJob类里job.getJobDataMap().put("wsurl", basePath+wsDefine.getUrl());//添加调用功能需要的熟悉job.getJobDataMap().put("jobid", wsDefine.getJobid());job.getJobDataMap().put("wsname", wsDefine.getName());String[] str = deftime.split(":"); schedString = str[2] + " " + str[1] + " " +str[0]+ " " + "*"//这里就是设定定时的表达式,我这里将时间按“:”分割成str数组+ " " + month + " " + year;System.out.println(schedString);System.out.println(sched);CronTrigger trigger = (CronTrigger) TriggerBuilder.newTrigger()//创建触发器.withIdentity(triggerName, groupName)//设定标示.withSchedule(CronScheduleBuilder.cronSchedule(schedString))//添加表达式.build(); // 设置触发器sched.scheduleJob(job, trigger); // 设置调度作业sched.start(); // 开启调度任务,执行作业} catch (Exception ex) {ex.printStackTrace();}}// 点击停止按钮调用的方法public void stopService(String id) {String schedName = "schedu"+id;try {if (sf.getScheduler(schedName) != null) {sf.getScheduler(schedName).shutdown();}} catch (Exception ex) {ex.printStackTrace();}}}
这上面只使用了CronTrigger的方式,还有一种更简单点的方式SimpleTrigger,该方式没有cronTrigger方式更精确。在本文下面有CronTrigger表达式的配置格式。

job执行类,该类需要实现quratz的job接口,并实现execute方法,你要定时调用的功能就在该execute方法中调用。

<span style="font-size:18px;">package com.gisquest.djgx.modules.services;import org.quartz.Job;import org.quartz.JobDataMap;import org.quartz.JobExecutionContext;import org.quartz.JobExecutionException;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.stereotype.Component;import org.springframework.transaction.annotation.Transactional;@Component@Transactional(readOnly = true)public class QuartzJob implements Job {private static final Logger log = LoggerFactory.getLogger(QuartzJob.class);@Overridepublic void execute(JobExecutionContext context)throws JobExecutionException {try {JobDataMap dataMap = context.getJobDetail().getJobDataMap();String wsurl = dataMap.getString("wsurl");String jobid = String.valueOf(dataMap.getInt("jobid"));//从之前在任务调度中传入的属性String wsname = dataMap.getString("wsname");System.out.println(wsurl+jobid+wsname);WsService wsService = new WsService();System.out.println(wsService);wsService.callWebService(null,wsurl,"pushData",jobid,wsname);//定时调用的方法} catch (Exception e) {e.printStackTrace();log.error(e.getLocalizedMessage());}}}</span>

CronTrigger配置格式:
格式: [秒] [分] [小时] [日] [月] [周] [年]

序号说明是否必填允许填写的值允许的通配符1秒是0-59, - * /2分是0-59, - * /3小时是0-23, - * /4日是1-31, - * ? / L W5月是1-12 or JAN-DEC, - * /6周是1-7 or SUN-SAT, - * ? / L #7年否empty 或 1970-2099, - * /

常用示例:

0 0 12 * * ?每天12点触发0 15 10 ? * *每天10点15分触发0 15 10 * * ?每天10点15分触发0 15 10 * * ? *每天10点15分触发0 15 10 * * ? 20052005年每天10点15分触发0 * 14 * * ?每天下午的 2点到2点59分每分触发0 0/5 14 * * ?每天下午的 2点到2点59分(整点开始,每隔5分触发)0 0/5 14,18 * * ?每天下午的 2点到2点59分(整点开始,每隔5分触发) 每天下午的 18点到18点59分(整点开始,每隔5分触发)0 0-5 14 * * ?每天下午的 2点到2点05分每分触发0 10,44 14 ? 3 WED3月分每周三下午的 2点10分和2点44分触发0 15 10 ? * MON-FRI从周一到周五每天上午的10点15分触发0 15 10 15 * ?每月15号上午10点15分触发0 15 10 L * ?每月最后一天的10点15分触发0 15 10 ? * 6L每月最后一周的星期五的10点15分触发0 15 10 ? * 6L 2002-2005从2002年到2005年每月最后一周的星期五的10点15分触发0 15 10 ? * 6#3每月的第三周的星期五开始触发0 0 12 1/5 * ?每月的第一个中午开始每隔5天触发一次0 11 11 11 11 ?每年的11月11号 11点11分触发(光棍节)


0 0
原创粉丝点击