Project

General

Profile

Revision 5116

Added by daigle over 9 years ago

Add ability to schedule daily, weekly and monthly jobs.

View differences:

src/edu/ucsb/nceas/metacat/scheduler/SchedulerService.java
194 194
	 *            the user's group name
195 195
	 * @return a message saying that the job was scheduled
196 196
	 */
197
	@SuppressWarnings("unchecked")
197 198
	public String scheduleJob(String jobName, Calendar startCal, Calendar endCal, int intervalValue, 
198 199
			String intervalUnit, String jobClassName, String jobGroup, HashMap<String, String> jobParams) 
199 200
	throws ServiceException {
......
260 261
	 *            the user's group name
261 262
	 * @return a message saying that the job was scheduled
262 263
	 */
264
	@SuppressWarnings("unchecked")
263 265
	public String scheduleDelayedJob(String jobName, String delay, String jobClassName, 
264 266
			String jobGroup, HashMap<String, String> jobParams, 
265 267
			String username, String[] groups) throws ServiceException {
......
356 358
	 *            that was running when metacat was shut down.
357 359
	 * @return a message saying that the job was rescheduled
358 360
	 */
361
	@SuppressWarnings("unchecked")
359 362
	public String rescheduleJob(ScheduledJobDAO jobDAO, String username, String[] groups, boolean checkStatus) throws ServiceException {
360 363
		
361 364
		try {
......
665 668
		JobDetail jobDetail = new JobDetail(jobName, jobGroup, jobClass);
666 669
		jobDetail.setJobDataMap(new JobDataMap(jobParams));
667 670
		
668
		char intervalChar = intervalUnit.charAt(0);
669
		
670 671
		// call the appropriate scheduling method depending on the schedule interval unit
671
		switch (intervalChar) {
672
		case 's':
673
		case 'S':
672
		if (intervalUnit.equals("sec")) {
674 673
			scheduleSecondlyJob(jobName, jobClass, startCal, endCal, intervalValue, jobGroup, jobDetail);
675
			break;
676
		case 'm':
677
		case 'M':
674
		} else if (intervalUnit.equals("min")) {
678 675
			scheduleMinutelyJob(jobName, jobClass, startCal, endCal, intervalValue, jobGroup, jobDetail);
679
			break;
680
		case 'h':
681
		case 'H':
676
		} else if (intervalUnit.equals("hour")) {
682 677
			scheduleHourlyJob(jobName, jobClass, startCal, endCal, intervalValue, jobGroup, jobDetail);
683
			break;
684
		case 'd':
685
		case 'D':
678
		} else if (intervalUnit.equals("day")) {
686 679
			scheduleDailyJob(jobName, jobClass, startCal, endCal, intervalValue, jobGroup, jobDetail);
687
			break;
688
		default:
680
		} else if (intervalUnit.equals("week")) {
681
			scheduleWeeklyJob(jobName, jobClass, startCal, endCal, intervalValue, jobGroup, jobDetail);
682
		} else if (intervalUnit.equals("mon")) {
683
			scheduleMonthlyJob(jobName, jobClass, startCal, endCal, intervalValue, jobGroup, jobDetail);
684
		} else {
689 685
			throw new ServiceException("SchedulerService.scheduleJob - Could not interpret interval unit: " 
690 686
					+ intervalUnit + ". Unit must be s, m, h or d");	
691 687
		}	
......
840 836
	 */
841 837
	private void scheduleDailyJob(String jobName, Class<Job> jobClass, Calendar startTime, Calendar endTime, int interval, String jobGroup, JobDetail jobDetail) throws ServiceException {
842 838

  
843
		Trigger trigger = TriggerUtils.makeHourlyTrigger(interval * 24);
839
		Trigger trigger = 
840
			TriggerUtils.makeDailyTrigger(startTime.get(Calendar.HOUR), startTime.get(Calendar.MINUTE));	
844 841
		trigger.setName(jobName);
845 842
		trigger.setStartTime(startTime.getTime());
846 843
		if (endTime != null) {
......
856 853
	}
857 854
	
858 855
	/**
856
	 * Schedule a job in the scheduler that has an interval based in days
857
	 * 
858
	 * @param jobName
859
	 *            the name of the job
860
	 * @param jobClass
861
	 *            the job class object
862
	 * @param startTime
863
	 *            the time of the first run
864
	 * @param interval
865
	 *            the interval in days between runs
866
	 * @param jobGroup
867
	 *            the group of this job
868
	 * @param jobDetail
869
	 *            the job detail object
870
	 */
871
	private void scheduleWeeklyJob(String jobName, Class<Job> jobClass, Calendar startTime, Calendar endTime, int interval, String jobGroup, JobDetail jobDetail) throws ServiceException {
872

  
873
		Trigger trigger = 
874
			TriggerUtils.makeWeeklyTrigger(startTime.get(Calendar.DAY_OF_WEEK), 
875
					startTime.get(Calendar.HOUR), startTime.get(Calendar.MINUTE));	
876
		trigger.setName(jobName);
877
		trigger.setStartTime(startTime.getTime());
878
		if (endTime != null) {
879
			trigger.setEndTime(endTime.getTime());
880
		}
881

  
882
		try {
883
			sched.scheduleJob(jobDetail, trigger);
884
		} catch (SchedulerException se) {
885
			throw new ServiceException("SchedulerService.scheduleHourlyJob - Could not create " 
886
					+ "scheduler: " + se.getMessage());
887
		}
888
	}
889
	
890
	/**
891
	 * Schedule a job in the scheduler that has an interval based in days
892
	 * 
893
	 * @param jobName
894
	 *            the name of the job
895
	 * @param jobClass
896
	 *            the job class object
897
	 * @param startTime
898
	 *            the time of the first run
899
	 * @param interval
900
	 *            the interval in days between runs
901
	 * @param jobGroup
902
	 *            the group of this job
903
	 * @param jobDetail
904
	 *            the job detail object
905
	 */
906
	private void scheduleMonthlyJob(String jobName, Class<Job> jobClass, Calendar startTime, Calendar endTime, int interval, String jobGroup, JobDetail jobDetail) throws ServiceException {
907

  
908
		Trigger trigger = 
909
			TriggerUtils.makeMonthlyTrigger(startTime.get(Calendar.DAY_OF_MONTH), 
910
					startTime.get(Calendar.HOUR), startTime.get(Calendar.MINUTE));	
911
		trigger.setName(jobName);
912
		trigger.setStartTime(startTime.getTime());
913
		if (endTime != null) {
914
			trigger.setEndTime(endTime.getTime());
915
		}
916

  
917
		try {
918
			sched.scheduleJob(jobDetail, trigger);
919
		} catch (SchedulerException se) {
920
			throw new ServiceException("SchedulerService.scheduleHourlyJob - Could not create " 
921
					+ "scheduler: " + se.getMessage());
922
		}
923
	}
924
	
925
	/**
859 926
	 * Extract the start date from the delay value
860 927
	 * 
861 928
	 * @param delay

Also available in: Unified diff