首页 > 代码库 > Quartz使用-入门使用(java定时任务实现)
Quartz使用-入门使用(java定时任务实现)
注:这里使用的是Quartz1.6.5版本(包:quartz-1.6.5.jar)
[java] view plain copy
- //<a href="http://lib.csdn.net/base/softwaretest" class=‘replace_word‘ title="软件测试知识库" target=‘_blank‘ style=‘color:#df3434; font-weight:bold;‘>测试</a>main函数
- //QuartzTest.<a href="http://lib.csdn.net/base/javase" class=‘replace_word‘ title="Java SE知识库" target=‘_blank‘ style=‘color:#df3434; font-weight:bold;‘>Java</a>
- package quartzPackage;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- public class QuartzTest {
- /** *//**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- SimpleDateFormat DateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
- Date d = new Date();
- String returnstr = DateFormat.format(d);
- TestJob job = new TestJob();
- String job_name ="11";
- try {
- System.out.println(returnstr+ "【系统启动】");
- QuartzManager.addJob(job_name,job,"0/2 * * * * ?"); //每2秒钟执行一次
- // Thread.sleep(10000);
- // System.out.println("【修改时间】");
- // QuartzManager.modifyJobTime(job_name,"0/10 * * * * ?");
- // Thread.sleep(20000);
- // System.out.println("【移除定时】");
- // QuartzManager.removeJob(job_name);
- // Thread.sleep(10000);
- //
- // System.out.println("/n【添加定时任务】");
- // QuartzManager.addJob(job_name,job,"0/5 * * * * ?");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
简单的任务管理类
[java] view plain copy
- //简单的任务管理类
- //QuartzManager.java
- package quartzPackage;
- import java.text.ParseException;
- import org.quartz.CronTrigger;
- import org.quartz.Job;
- import org.quartz.JobDetail;
- import org.quartz.Scheduler;
- import org.quartz.SchedulerException;
- import org.quartz.SchedulerFactory;
- import org.quartz.Trigger;
- import org.quartz.impl.StdSchedulerFactory;
- /** *//**
- * @Title:Quartz管理类
- *
- * @Description:
- *
- * @Copyright:
- * @author zz 2008-10-8 14:19:01
- * @version 1.00.000
- *
- */
- public class QuartzManager {
- private static SchedulerFactory sf = new StdSchedulerFactory();
- private static String JOB_GROUP_NAME = "group1";
- private static String TRIGGER_GROUP_NAME = "trigger1";
- /** *//**
- * 添加一个定时任务,使用默认的任务组名,触发器名,触发器组名
- * @param jobName 任务名
- * @param job 任务
- * @param time 时间设置,参考quartz说明文档
- * @throws SchedulerException
- * @throws ParseException
- */
- public static void addJob(String jobName,Job job,String time)
- throws SchedulerException, ParseException{
- Scheduler sched = sf.getScheduler();
- JobDetail jobDetail = new JobDetail(jobName, JOB_GROUP_NAME, job.getClass());//任务名,任务组,任务执行类
- //触发器
- CronTrigger trigger =
- new CronTrigger(jobName, TRIGGER_GROUP_NAME);//触发器名,触发器组
- trigger.setCronExpression(time);//触发器时间设定
- sched.scheduleJob(jobDetail,trigger);
- //启动
- if(!sched.isShutdown())
- sched.start();
- }
- /** *//**
- * 添加一个定时任务
- * @param jobName 任务名
- * @param jobGroupName 任务组名
- * @param triggerName 触发器名
- * @param triggerGroupName 触发器组名
- * @param job 任务
- * @param time 时间设置,参考quartz说明文档
- * @throws SchedulerException
- * @throws ParseException
- */
- public static void addJob(String jobName,String jobGroupName,
- String triggerName,String triggerGroupName,
- Job job,String time)
- throws SchedulerException, ParseException{
- Scheduler sched = sf.getScheduler();
- JobDetail jobDetail = new JobDetail(jobName, jobGroupName, job.getClass());//任务名,任务组,任务执行类
- //触发器
- CronTrigger trigger =
- new CronTrigger(triggerName, triggerGroupName);//触发器名,触发器组
- trigger.setCronExpression(time);//触发器时间设定
- sched.scheduleJob(jobDetail,trigger);
- if(!sched.isShutdown())
- sched.start();
- }
- /** *//**
- * 修改一个任务的触发时间(使用默认的任务组名,触发器名,触发器组名)
- * @param jobName
- * @param time
- * @throws SchedulerException
- * @throws ParseException
- */
- public static void modifyJobTime(String jobName,String time)
- throws SchedulerException, ParseException{
- Scheduler sched = sf.getScheduler();
- Trigger trigger = sched.getTrigger(jobName,TRIGGER_GROUP_NAME);
- if(trigger != null){
- CronTrigger ct = (CronTrigger)trigger;
- ct.setCronExpression(time);
- sched.resumeTrigger(jobName,TRIGGER_GROUP_NAME);
- }
- }
- /** *//**
- * 修改一个任务的触发时间
- * @param triggerName
- * @param triggerGroupName
- * @param time
- * @throws SchedulerException
- * @throws ParseException
- */
- public static void modifyJobTime(String triggerName,String triggerGroupName,
- String time)
- throws SchedulerException, ParseException{
- Scheduler sched = sf.getScheduler();
- Trigger trigger = sched.getTrigger(triggerName,triggerGroupName);
- if(trigger != null){
- CronTrigger ct = (CronTrigger)trigger;
- //修改时间
- ct.setCronExpression(time);
- //重启触发器
- sched.resumeTrigger(triggerName,triggerGroupName);
- }
- }
- /** *//**
- * 移除一个任务(使用默认的任务组名,触发器名,触发器组名)
- * @param jobName
- * @throws SchedulerException
- */
- public static void removeJob(String jobName)
- throws SchedulerException{
- Scheduler sched = sf.getScheduler();
- sched.pauseTrigger(jobName,TRIGGER_GROUP_NAME);//停止触发器
- sched.unscheduleJob(jobName,TRIGGER_GROUP_NAME);//移除触发器
- sched.deleteJob(jobName,JOB_GROUP_NAME);//删除任务
- }
- /** *//**
- * 移除一个任务
- * @param jobName
- * @param jobGroupName
- * @param triggerName
- * @param triggerGroupName
- * @throws SchedulerException
- */
- public static void removeJob(String jobName,String jobGroupName,
- String triggerName,String triggerGroupName)
- throws SchedulerException{
- Scheduler sched = sf.getScheduler();
- sched.pauseTrigger(triggerName,triggerGroupName);//停止触发器
- sched.unscheduleJob(triggerName,triggerGroupName);//移除触发器
- sched.deleteJob(jobName,jobGroupName);//删除任务
- }
- }
测试工作类
[java] view plain copy
- //测试工作类
- //TestJob.java
- package quartzPackage;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import org.quartz.Job;
- import org.quartz.JobExecutionContext;
- import org.quartz.JobExecutionException;
- public class TestJob implements Job {
- SimpleDateFormat DateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- Date d = new Date();
- String returnstr = DateFormat.format(d);
- public void execute(JobExecutionContext arg0) throws JobExecutionException {
- // TODO Auto-generated method stub
- System.out.println(returnstr+"★★★★★★★★★★★");
- }
- }
第三方包
通过测试。一个简单任务只需要以下几个包:commons-beanutils.jar、commons-collections.jar、commons-logging.jar、commons-digester.jar、quartz.jar即可
名称 | 必须/备注 | 网址 |
activation.jar | 主要是 JavaMail 要用到 | http://java.sun.com/products/javabeans/glasgow/jaf.html |
commons-beanutils.jar | 是 | http://jakarta.apache.org/commons/beanutils |
commons-collections.jar | 是 | http://jakarta.apache.org/commons/collections |
commons-dbcp-1.1.jar | 是,假如用到数据库作为作业存储 | http://jakarta.apache.org/commons/dbcp |
commons-digester.jar | 是 | 假如你使用了某些插件,就需要它 |
commons-logging.jar | 是 | http://jakarta.apache.org/commons/logging/ |
commons-pool-1.1.jar | http://jakarta.apache.org/commons/pool/ | |
javamail.jar | 发送 e-mail 用 | http://java.sun.com/products/javamail/ |
jdbc2_0-stdext.jar | 是,假如用到数据库作为作业存储 | http://java.sun.com/products/jdbc/ |
jta.jar | 是,假如用到数据库作为作业存储 | http://java.sun.com/products/jta/database |
quartz.jar | 是 | Quart 框架核心包 |
servlet.jar | 假如使用了Servlet 容器,但容器中应该存在 | http://java.sun.com/products/servlet/ |
log4j.jar | 是,日志 | http://logging.apache.org/ |
Quartz使用-入门使用(java定时任务实现)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。