Knight of the round table

wansong

Quartz 简单的一个实例

SimpleJob.java

 

package com.test.job;

 

import org.quartz.Job;

import org.quartz.JobExecutionContext;

import org.quartz.JobExecutionException;

 

public class SimpleJob implements Job{

    public void execute(JobExecutionContext context)

           throws JobExecutionException {

       // your business logic

       // ...

       System.out.println("simpleJob running ....");

    }

}

TestSimpleJob.java

 

package com.test.job;

 

import java.text.ParseException;

 

import org.quartz.CronExpression;

import org.quartz.CronTrigger;

import org.quartz.JobDetail;

import org.quartz.Scheduler;

import org.quartz.SchedulerException;

import org.quartz.SimpleTrigger;

import org.quartz.impl.StdSchedulerFactory;

 

public class TestSimpleJob {

    public static void main(String[] args){

       try {

           Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

          

           String jobName = "simpleJob";

           String groupName = "DEFAULT";

          

           //删除作业

           if(scheduler.getJobDetail(jobName,groupName) != null){

              scheduler.deleteJob(jobName, groupName);

           }

          

           //作业的详细信息

           //任务名称,任务组名称,任务实现类

           JobDetail jobDetail = new JobDetail(jobName,groupName,SimpleJob.class);

          

           //创建简单触发器

           SimpleTrigger simpleTrigger = new SimpleTrigger("simpleTrigger",null);

 

           simpleTrigger.setRepeatCount(100); // 调用100

           simpleTrigger.setRepeatInterval(5*1000); //5秒钟调用一次

 

           //注册作业

           scheduler.scheduleJob(jobDetail, simpleTrigger);

          

          

           CronTrigger cronTrigger = new CronTrigger("cronTrigger",null);

           CronExpression cronExpression = null;

           try {

              cronExpression = new CronExpression("0 0 12 * * ?"); //每天12点运行

              cronTrigger.setCronExpression(cronExpression);

             

              //注册作业

              scheduler.scheduleJob(jobDetail, cronTrigger);

             

              if(! scheduler.isShutdown()){

                  scheduler.start();

              }

             

          

           } catch (ParseException e) {

              e.printStackTrace();

           }

          

       } catch (SchedulerException e) {

           e.printStackTrace();

       }

    }

}

 

posted on 2011-08-07 13:25 w@ns0ng 阅读(865) 评论(1)  编辑  收藏 所属分类: jbossjava

Feedback

# re: Quartz 简单的一个实例 2014-08-10 23:22 zuidaima

java quartz定时任务 demo教程源代码下载:http://zuidaima.com/share_topic/k%E4%BB%BB%E5%8A%A1%E8%B0%83%E5%BA%A6-p1-s1.htm  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航: