Run quartz job at a given set of clock times, every day
Hi folks,
We have an applications which uses quartz for job scheduling. I had to implement this simple behavior. I needed to run a particular job at given set of clock times say (10AM,11.30AM, 1PM, 5PM) every day. I’m familiar with quartz simple and cron triggers. But I found out that either of the triggers are unable to implement this behavior. So I decided to implement a new quartz trigger to support this requirement.(only
)
Here goes the source code.
Source code of the TriggeringTimesCollection class. This is an utility class for the CustomTrigger.
package trigger;
import java.util.*;
import java.text.SimpleDateFormat;
import org.apache.log4j.Logger;
public class TriggeringTimesCollection {
private static final String DATES_DELIMITER = “,”;
public static Logger logger = Logger.getLogger(TriggeringTimesCollection.class);
private List<Date> triggerTimes;
private int currentPoss;
public static TriggeringTimesCollection createInstance(String datesList) {
StringTokenizer st = new StringTokenizer(datesList, DATES_DELIMITER);
List<Date> triggerTimesList = new ArrayList<Date>();
SimpleDateFormat format = new SimpleDateFormat(“HH/mm/ss”);
Date tmpDate;
while (st.hasMoreTokens()) {
try {
tmpDate = format.parse(st.nextToken());
triggerTimesList.add(tmpDate);
} catch (Exception e) {
logger.error(e.getMessage(), e);
continue;
}
}
Collections.sort(triggerTimesList);
return new TriggeringTimesCollection(triggerTimesList, -1);
}
public TriggeringTimesCollection(List<Date> triggerTimes, int currentPoss) {
this.triggerTimes = triggerTimes;
this.currentPoss = currentPoss;
}
public Date getTimeAfter(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
Calendar cal2 = (Calendar) calendar.clone();
Calendar cal3 = (Calendar) calendar.clone();
Date result = null;
cal2.set(Calendar.MILLISECOND, 0);
for (Date tmpDate : triggerTimes) {
cal3.setTime(tmpDate);
cal2.set(Calendar.YEAR, cal3.get(Calendar.YEAR));
cal2.set(Calendar.MONTH, cal3.get(Calendar.MONTH)); //compare regardless of the date
cal2.set(Calendar.DATE, cal3.get(Calendar.DATE));
if (cal2.getTime().before(tmpDate)) {
result = tmpDate;
break;
} }
if (result == null) { // get first start of next date
result = triggerTimes.get(0);
calendar.setTimeInMillis(calendar.getTimeInMillis() + 24 * 60 * 60 * 1000);//switch to next day
}
cal2.setTime(result);
calendar.set(Calendar.HOUR_OF_DAY, cal2.get(Calendar.HOUR_OF_DAY));
calendar.set(Calendar.MINUTE, cal2.get(Calendar.MINUTE));
calendar.set(Calendar.SECOND, cal2.get(Calendar.SECOND));
result = calendar.getTime();
return result;
}
public Date getTimeBefore(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
Calendar cal2 = (Calendar) calendar.clone();
Calendar cal3 = (Calendar) calendar.clone();
Date result = null;
cal2.set(Calendar.MILLISECOND, 0);
for (Date tmpDate : triggerTimes) {
cal3.setTime(tmpDate);
cal2.set(Calendar.YEAR, cal3.get(Calendar.YEAR));
cal2.set(Calendar.MONTH, cal3.get(Calendar.MONTH)); //compare regardless of the date
cal2.set(Calendar.DATE, cal3.get(Calendar.DATE));
if (cal2.getTime().before(tmpDate)) {
break;
} else { result = tmpDate; } }
if (result == null) { // get first start of prevous date
cal3.setTime(triggerTimes.get(triggerTimes.size() – 1));
cal2.set(Calendar.YEAR, cal3.get(Calendar.YEAR));
cal2.set(Calendar.MONTH, cal3.get(Calendar.MONTH)); //compare regardless of the date
cal2.set(Calendar.DATE, cal3.get(Calendar.DATE));
calendar.setTimeInMillis(calendar.getTimeInMillis() – 24 * 60 * 60 * 1000);//switch to next day
}
cal2.setTime(result);
calendar.set(Calendar.HOUR, cal2.get(Calendar.HOUR)); calendar.set(Calendar.MINUTE, cal2.get(Calendar.MINUTE)); calendar.set(Calendar.SECOND, cal2.get(Calendar.SECOND));
result = calendar.getTime();
return result;
}
}
The source code for the CustomTrigger class.
package trigger;
import org.quartz.*;
import java.util.Date;
import java.util.List;
public class CustomTrigger extends CronTrigger {
private TriggeringTimesCollection triggeringTimesCollection;
public CustomTrigger(String triggerName, String jobName, String triggerDates){
super(triggerName, jobName);
triggeringTimesCollection = TriggeringTimesCollection.createInstance(triggerDates);
}
protected Date getTimeAfter(Date afterTime) {
return (triggeringTimesCollection == null) ? null : triggeringTimesCollection.getTimeAfter(afterTime);
}
protected Date getTimeBefore(Date beforeTime){
return (triggeringTimesCollection == null) ? null : triggeringTimesCollection.getTimeBefore(beforeTime);
}
}
Here CustomTrigger is the actual trigger and TriggeringTimesCollection is an utility class.
Thanks.
Regards,
Lasith.
Mr. Lasith,
This is not readable.