Quartz---Application学习(三)

·运行 ScanDirectoryJob 的多个实例

多次调用 schedulerJob() 方法,在设置上比如 Job 名称和扫描间隔名称通过参数传。因此从 createScheduler() 方法获取到 Scheduler 实例后,两个 Job(同一个类) 用不同的参数就被安排到了 Scheduler 上了。

public class MulJobScheduler {       
    static Log logger = LogFactory.getLog(MulJobScheduler.class);       
     
    public static void main(String[] args) {       
    	MulJobScheduler example = new MulJobScheduler();       
     
    	try {       
            // Create a Scheduler and schedule the Job       
            Scheduler scheduler = example.createScheduler();       
   
            // Jobs can be scheduled after Scheduler is running       
            scheduler.start();       
   
            logger.info("Scheduler started at " + new Date());       
   
            // Schedule the first Job       
            example.scheduleJob(scheduler, "ScanDirectory1", ScanDirectoryJob.class, "d:\\test", 10);       
   
            // Schedule the second Job       
            example.scheduleJob(scheduler, "ScanDirectory2", ScanDirectoryJob.class, "d:\\test", 15);       
   
       } catch (SchedulerException ex) {       
            logger.error(ex);       
       }          
   }       
     
   /*     
    * return an instance of the Scheduler from the factory     
    */      
   public Scheduler createScheduler() throws SchedulerException {       
        return StdSchedulerFactory.getDefaultScheduler();       
   }       
     
   // Create and Schedule a ScanDirectoryJob with the Scheduler       
   private void scheduleJob(Scheduler scheduler, String jobName,       
             Class jobClass, String scanDir, int scanInterval)       
             throws SchedulerException {       
    
         // Create a JobDetail for the Job       
        JobDetail jobDetail = new JobDetail(jobName, Scheduler.DEFAULT_GROUP, jobClass);       
    
        // Configure the directory to scan       
        jobDetail.getJobDataMap().put("SCAN_DIR", scanDir);       
    
        // Trigger that repeats every "scanInterval" secs forever       
        Trigger trigger = TriggerUtils.makeSecondlyTrigger(scanInterval);       
    
        trigger.setName(jobName + "-Trigger");       
    
        // Start the trigger firing from now       
        trigger.setStartTime(new Date());       
    
        // Associate the trigger with the job in the scheduler       
        scheduler.scheduleJob(jobDetail, trigger);       
   }        
}  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值