`

定时任务---自定义HourlyCalender 能从每小中排除某些分钟

    博客分类:
  • Code
阅读更多

自定义HourlyCalender 能从每小中排除某些分钟

/**
 * 自定义自己的Calendar --- 排除一个小时中的一些分钟
 * @name HourlyCalendar.java 
 * @author vivi
 * @time 2009-9-3 上午10:39:06
 */
public class HourlyCalendar extends BaseCalendar {
	
	//Array of Integer from 0 to 59
	private List excludedMinutes = new ArrayList();

	public HourlyCalendar(){
		super();
	}
	public HourlyCalendar(org.quartz.Calendar baseCalendar){
		super(baseCalendar);
	}
	
	public List getExcludeMinutes() {
		return excludedMinutes;
	}
	
	public boolean isMinuteExcluded(int minute){
		Iterator iter = excludedMinutes.iterator();
		while(iter.hasNext()){
			Integer excludeMin = (Integer)iter.next();
			
			if(minute == excludeMin.intValue()){
				return true;
			}
			continue;
		}
		return false;
	}
	
	public void setMinutesExcluded(List minutes){
		if(minutes == null){
			return;
		}
		excludedMinutes.addAll(minutes);
	}
	
	public void setMinuteExcluded(int minute) {		  
        if (isMinuteExcluded(minute)){
        	return;
        }  
        excludedMinutes.add(new Integer(minute));   
	}
	
	public boolean isTimeIncluded(long timeStamp) {		  
        if (super.isTimeIncluded(timeStamp) == false){   
             return false;   
        }   

        Calendar cal = getJavaCalendar(timeStamp);   
        int minute = cal.get(Calendar.MINUTE);   

        return !(isMinuteExcluded(minute));   
	}   
	
	public long getNextIncludedTime(long timeStamp) {   
        // Call base calendar implementation first   
        long baseTime = super.getNextIncludedTime(timeStamp);   
        if ((baseTime > 0) && (baseTime > timeStamp))   
            timeStamp = baseTime;   

        // Get timestamp for 00:00:00   
        long newTimeStamp = buildHoliday(timeStamp);   
        Calendar cal = getJavaCalendar(newTimeStamp);   
        int minute = cal.get(Calendar.MINUTE);   

        if (isMinuteExcluded(minute) == false)   
             return timeStamp; // return the   
        // original value   

        while (isMinuteExcluded(minute) == true) {   
             cal.add(java.util.Calendar.MINUTE, 1);   
        }   
        return cal.getTime().getTime();   
	}  

}

 测试自定义Clendar

public class TestHourlyCalendar {
	static Log logger = LogFactory.getLog(TestHourlyCalendar.class);   
	  
    public static void main(String[] args) {   
    	TestHourlyCalendar example = new TestHourlyCalendar();   
         example.startScheduler();   
    }   
 
    public void startScheduler() {   
    	try {   
    		// Create a default instance of the Scheduler   
            Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();   
            scheduler.start(); 
            
            // Using the NoOpJob, but could have been any   
            scheduleJob(scheduler, PrintInfoJob.class);   
 
            logger.info("Scheduler starting up...");                
         } catch (SchedulerException ex) {   
              logger.error(ex);   
         }   
    }   
 
    private void scheduleJob(Scheduler scheduler, Class jobClass) {   
         try {   
              // Create an instance of the Quartz AnnualCalendar   
              HourlyCalendar cal = new HourlyCalendar();   
              //cal.setMinuteExcluded(47);   
              cal.setMinuteExcluded(48);   
              //cal.setMinuteExcluded(49);   
              cal.setMinuteExcluded(50);  
 
              // Add Calendar to the Scheduler   
              scheduler.addCalendar("hourlyExample", cal, true, true);   
 
              Trigger trigger = TriggerUtils.makeImmediateTrigger("myTrigger", -1, 3000);   
 
              // Trigger will use Calendar to exclude firing times   
              trigger.setCalendarName("hourlyExample");   
 
              JobDetail jobDetail = new JobDetail(jobClass.getName(),   
                        Scheduler.DEFAULT_GROUP, jobClass);   
 
              // Associate the trigger with the job in the scheduler   
              scheduler.scheduleJob(jobDetail, trigger);   
         } catch (SchedulerException ex) {   
              logger.error(ex);   
         }   
    } 
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics