调度框架Quartz,初体验

 Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用。Quartz可以用来创建简单或为运行十个,百个,甚至是好几万个Jobs这样复杂的日程序表。Jobs可以做成标准的Java组件或 EJBs。 Quartz的最新版本为 Quartz 1.6.0。
     
       首先我建了一个java project ,导了一个quartz.jar到类路径。
       然后新建两个类,如下:
TestJob.java

      package csu.cn.test604;

import java.util.Date;

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;
import org.apache.commons.logging.LogFactory;
import javax.transaction.UserTransaction;
public class TestJob {

 
 public void Test() throws SchedulerException{
  //initiate a schedule factory
  SchedulerFactory sf=new StdSchedulerFactory();
  //Retrieve a scheduler from schedule factory;
  Scheduler s=sf.getScheduler();
  
  //initiate jobDetail with job name,job group, and executable job class
  JobDetail jobDetail=new JobDetail("autoinsepction","inspection",XunHuan.class);
  //init simpleTrigger
  SimpleTrigger simpleTrigger=new SimpleTrigger("simpleTrigger",null,
    new Date(),//start immediatly
    null,SimpleTrigger.REPEAT_INDEFINITELY,//repeat forever
    2L*60L*1000L
  );//every 2 minutes
  
  s.scheduleJob(jobDetail, simpleTrigger);
  //start the scheduler
  s.start();
  
 }
 public static void main(String[] args) {
  // TODO Auto-generated method stub
       TestJob tj=new TestJob();
       try{
        tj.Test();
       }
       catch(SchedulerException e)
       {
        e.printStackTrace();
       }
 }

}
每两分中执行一次XunHuan

package csu.cn.test604;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class XunHuan implements Job {

 private static int i=0;
 @Override
 public void execute(JobExecutionContext arg0) throws JobExecutionException {
  // TODO Auto-generated method stub
   System.out.println("第"+i+"次调用");
   i++;
 }

}

因为第一次尝试,所以,只加了一个包进去,结果,运行的时候,老提示少XX每找到。

于是一次次慢慢尝试加包
log4j-1.2.15.jar

jta.jar                        javax.transaction

commons-collections-3.2.jar    org.apache.commons.collections

commons-logging-api-1.1.jar    org.apache.commons.logging
最后可以成功运行
2008-6-4 20:22:12 org.quartz.simpl.SimpleThreadPool initialize
信息: Job execution threads will use class loader of thread: main
2008-6-4 20:22:12 org.quartz.core.QuartzScheduler
信息: Quartz Scheduler v.1.6.0 created.
2008-6-4 20:22:12 org.quartz.simpl.RAMJobStore initialize
信息: RAMJobStore initialized.
2008-6-4 20:22:12 org.quartz.impl.StdSchedulerFactory instantiate
信息: Quartz scheduler 'DefaultQuartzScheduler' initialized from default resource file in Quartz package: 'quartz.properties'
2008-6-4 20:22:12 org.quartz.impl.StdSchedulerFactory instantiate
信息: Quartz scheduler version: 1.6.0
2008-6-4 20:22:12 org.quartz.core.QuartzScheduler start
信息: Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED started.
第0次调用
第1次调用
......

 

你可能感兴趣的:(开源框架)