定时框架quartz使用

使用simpleSchedule

import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;

import org.quartz.impl.StdSchedulerFactory;

public class SimpleScheduler {
    
    public static void main(String[] args)
    {
        System.out.println("startt");
        new SimpleScheduler().startScheduler();
    }
    
    public void startScheduler()
    {
        
        try {
            Scheduler scheduler= StdSchedulerFactory.getDefaultScheduler();  

            JobDetail job = JobBuilder.newJob(DemoJob.class).withIdentity("job1", "group1").build();

            Trigger trigger = TriggerBuilder.newTrigger()
                    .withIdentity("trigger1", "group1")
                    .startNow()
                    .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                            .withIntervalInSeconds(1)
                            .repeatForever())            
                    .build();
            
            scheduler.scheduleJob(job, trigger);
            
            scheduler.start();

            
        } catch (Exception e) {
            // TODO: handle exception
        }
        

    }
}

 

 

使用cronSchedule

import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.CronScheduleBuilder; 
import org.quartz.impl.StdSchedulerFactory;

public class SimpleScheduler {
    
    public static void main(String[] args)
    {
        System.out.println("startt");
        new SimpleScheduler().startScheduler();
    }
    
    public void startScheduler()
    {
        
        try {
            Scheduler scheduler= StdSchedulerFactory.getDefaultScheduler();  

            JobDetail job = JobBuilder.newJob(DemoJob.class).withIdentity("job1", "group1").build();

            Trigger trigger = TriggerBuilder.newTrigger()
                    /*.withIdentity("trigger1", "group1")*/
                    .startNow()
                    .withSchedule(CronScheduleBuilder.cronSchedule("0/2 * * * * ?"))            
                    .build();
            
            
            
            scheduler.scheduleJob(job, trigger);
            
            scheduler.start();

            
        } catch (Exception e) {
            // TODO: handle exception
        }
        

    }
}

 

 

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


public class DemoJob implements Job{
    public void execute(JobExecutionContext arg0) throws JobExecutionException {  
        System.out.println("Hello");  
   }  
}

 

posted @ 2015-01-21 14:46  iyjhabc  阅读(258)  评论(0编辑  收藏  举报