码迷,mamicode.com
首页 > 其他好文 > 详细

定时执行程序-Quartz简单实例

时间:2015-07-12 11:16:04      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:jar

定时执行程序-Quartz简单实例


1、加jar包:Quartz自己quartz-1.8.3.jar和依赖包commons-logging.jar  、slf4j-log4j12-1.5.10.jar 、slf4j-api-1.5.10.jar

2、一个类搞定!!!,直接run
import  org.quartz.CronTrigger;
 import  org.quartz.Job;
 import  org.quartz.JobDetail;
 import  org.quartz.JobExecutionContext;
 import  org.quartz.JobExecutionException;
 import  org.quartz.Scheduler;
 import  org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;

import  java.util.Date;

  public class QuartzReport implements  Job{

      public   void  execute(JobExecutionContext cntxt)  throws  JobExecutionException   {
        System.out.println( "输出:"+cntxt.getJobDetail().getJobDataMap().get("name")+new Date());
     } 
 
      public static void  main(String[] args)   {
         try    {
            SchedulerFactory schedFact=new StdSchedulerFactory();
            Scheduler sched=schedFact.getScheduler();
            sched.start();
            JobDetail jobDetail=new JobDetail("a","b",QuartzReport.class);
            jobDetail.getJobDataMap().put("name","lucy");
            
            CronTrigger trigger=new  CronTrigger("c","d");
            trigger.setCronExpression("0/1 * * * * ? " ); // 启动之后立即执行 每一秒继续重复。 
            sched.scheduleJob(jobDetail, trigger);
            
        }   catch  (Exception e)   {
            e.printStackTrace();
        } 
    } 


/**
 * 输出:
 *log4j:WARN No appenders could be found for logger (org.quartz.simpl.SimpleThreadPool).
log4j:WARN Please initialize the log4j system properly.
输出:lucyWed Apr 13 15:11:54 CST 2011
输出:lucyWed Apr 13 15:11:55 CST 2011
输出:lucyWed Apr 13 15:11:56 CST 2011
输出:lucyWed Apr 13 15:11:57 CST 2011
输出:lucyWed Apr 13 15:11:58 CST 2011
输出:lucyWed Apr 13 15:11:59 CST 2011
输出:lucyWed Apr 13 15:12:00 CST 2011
输出:lucyWed Apr 13 15:12:01 CST 2011
输出:lucyWed Apr 13 15:12:02 CST 2011
输出:lucyWed Apr 13 15:12:03 CST 2011
输出:lucyWed Apr 13 15:12:04 CST 2011
输出:lucyWed Apr 13 15:12:05 CST 2011
输出:lucyWed Apr 13 15:12:06 CST 2011
输出:lucyWed Apr 13 15:12:07 CST 2011
输出:lucyWed Apr 13 15:12:08 CST 2011
输出:lucyWed Apr 13 15:12:09 CST 2011
输出:lucyWed Apr 13 15:12:10 CST 2011
输出:lucyWed Apr 13 15:12:11 CST 2011
输出:lucyWed Apr 13 15:12:12 CST 2011
输出:lucyWed Apr 13 15:12:13 CST 2011
输出:lucyWed Apr 13 15:12:14 CST 2011

 */

附:cronExpression配置说明

         
  0-59   , - * /
  0-59   , - * /
小时   0-23   , - * /
日期   1-31   , - * ? / L W C
月份   1-12 或者 JAN-DEC   , - * /
星期   1-7 或者 SUN-SAT   , - * ? / L C #
年(可选)   留空, 1970-2099   , - * /

版权声明:本文为博主http://www.zuiniusn.com 原创文章,未经博主允许不得转载。

定时执行程序-Quartz简单实例

标签:jar

原文地址:http://blog.csdn.net/u013141940/article/details/46848419

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!