Quartz中SimpleTrigger的探讨

让我们继续用第一个例子中StringTest.
1.来写一个每隔10秒启动一次任务的例子.

import  java.util.Date;

import  org.quartz.JobDetail;
import  org.quartz.Scheduler;
import  org.quartz.SchedulerFactory;
import  org.quartz.SimpleTrigger;
import  org.quartz.impl.StdSchedulerFactory;

public   class  SimpleTriggerTest
{

    
public static void main(String[] args) throws Exception
    
{
        SchedulerFactory schedulerFactory 
= new StdSchedulerFactory();
        Scheduler scheduler 
= schedulerFactory.getScheduler();
        
// 系统当前时间10秒后
        long startTime = System.currentTimeMillis() + 10000L;
        SimpleTrigger trigger 
= new SimpleTrigger("myTrigger"nullnew Date(
                startTime), 
null00L);

        JobDetail jobDetail 
= new JobDetail();
        jobDetail.setJobClass(StringTest.
class);
        jobDetail.setName(
"test");
        jobDetail.setGroup(
"A");

        scheduler.scheduleJob(jobDetail, trigger);
        scheduler.start();
    }

}



2.创建一个触发器,立即启动,每隔60秒,启动一次.
SimpleTrigger trigger  =   new  SimpleTrigger( " myTrigger " ,
                                            
null ,
                                            
new  Date(),
                                            
null ,
                                            SimpleTrigger.REPEAT_INDEFINITELY,
                                            
60L   *   1000L );
3.创建一个触发器,立即启动.从现在开始的第10秒到第40秒重复运行.
long  endTime  =  System.currentTimeMillis()  +   40000L ;

  SimpleTrigger trigger 
=   new  SimpleTrigger( " myTrigger " ,
                                            
" myGroup " ,
                                            
new  Date(),
                                            
new  Date(endTime),
                                            SimpleTrigger.REPEAT_INDEFINITELY,
                                            
10L   *   1000L );
4.创建一个触发器,在2008年5月9日,上午10点半执行,重复5次,每隔30秒一次.
java.util.Calendar cal  =   new  java.util.GregorianCalendar( 2008 , cal.MARCH, 9 );
  cal.set(cal.HOUR, 
10 );
  cal.set(cal.MINUTE, 
30 );
  cal.set(cal.SECOND, 
0 );
  cal.set(cal.MILLISECOND, 
0 );

  Data startTime 
=  cal.getTime()

  SimpleTrigger trigger 
=   new  SimpleTrigger( " myTrigger " ,
                                            
null ,
                                            startTime,
                                            
null ,
                                            
5 ,
                                            
30L   *   1000L );

仔细体会SimpleTrigger的例子,你会发现用这个制作执行计划会更简单方便.

々上善若水々 2008-05-08 17:01 发表评论

你可能感兴趣的:(quartz)