Posts Tagged ‘quartz’
Complementary triggers using Quartz and Spring
Following example configures one quartz job to run on normal days and another quartz job to run on holidays dates. That is, one quartz trigger run when the other not. I imagine some easier solution exists, but it works.
applicationContext.xml
<!-- Properties file -->
<bean id="properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location" value="file:/foo/dates.prop"/>
</bean>
<!-- QUARTZ Configuration -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="normalTrigger" />
<ref bean="holidayTrigger" />
</list>
</property>
<property name="calendars">
<map>
<entry key="calendarNormal"><ref bean="normalCalendar" /></entry>
<entry key="calendarHoliday"><ref bean="holidayCalendar" /></entry>
</map>
</property>
</bean>
<!-- Everyday but holidays -->
<bean id="normalCalendar" class="MyNormalCalendar">
<constructor -arg><ref bean="properties" /></constructor>
</bean>
<!-- Every holiday -->
<bean id="holidayCalendar" class="MyHolidayCalendar">
<constructor -arg><ref bean="properties" /></constructor>
</bean>
<!-- Normal Job -->
<bean id="normalTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="someJobDetail" />
<property name="calendarName" value="calendarNormal"/>
<property name="cronExpression">
<!-- Every five minutes from 8h to 14h -->
<value>0 0/5 8-14 * * ?</value>
</property>
</bean>
<!-- Holiday Job -->
<bean id="holidayTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="anotherJobDetail" />
<property name="calendarName" value="calendarHoliday"/>
<property name="cronExpression">
<!-- Every five minutes from 8h to 14h -->
<value>0 0/5 8-14 * * ?</value>
</property>
</bean>
dates.prop
workingDays.legalHolidays=2008-01-01,2008-03-21,2008-03-24,2008-05-01,2008-05-08,2008-05-12,2008-07-14,2008-08-15,2008-11-11,2008-12-25
MyHolidayCalendar.java
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Properties;
import org.quartz.impl.calendar.HolidayCalendar;
/**
*
* Activate trigger on holidays.
*
* @author gz451w
*
*/
@SuppressWarnings("serial")
public class MyHolidayCalendar extends HolidayCalendar {
private static final String WORKING_DAYS_LEGAL_HOLIDAYS = "workingDays.legalHolidays";
private Properties properties;
/**
* Build activate days list
* @param properties
*/
public MyHolidayCalendar(Properties properties) {
this.properties = properties;
String stringDates = getProperties().getProperty(WORKING_DAYS_LEGAL_HOLIDAYS);
if (stringDates != null) {
Calendar[] dates = getDatesInverse(stringDates);
for (Calendar date : dates) {
addExcludedDate(date.getTime());
}
}
}
/**
* @return the properties
*/
public Properties getProperties() {
return properties;
}
/**
* @param properties the properties to set
*/
public void setProperties(Properties properties) {
this.properties = properties;
}
/**
* Inverse list from a date list
* @param stringDates
* @return
*/
private Calendar[] getDatesInverse(String stringDates) {
String[] stringDatesArray = stringDates.split(",");
// Holiday list
ArrayList<calendar> dates = new ArrayList</calendar><calendar>();
for (String stringDate : stringDatesArray) {
String[] splitDate = stringDate.split("-");
Calendar date =
new GregorianCalendar(
Integer.parseInt(splitDate[0]),
Integer.parseInt(splitDate[1]) - 1, // Month field starts with 0!
Integer.parseInt(splitDate[2]));
dates.add(date);
}
// Exclude non-holiday dates
ArrayList</calendar><calendar> returnDates = new ArrayList</calendar><calendar>();
try {
Calendar c = Calendar.getInstance();
c.setTime(new SimpleDateFormat("dd/MM/yyyy").parse("01/01/" + dates.get(0).get(Calendar.YEAR)));
while(c.get(Calendar.YEAR) == dates.get(0).get(Calendar.YEAR)){
if (!dates.contains(c)) {
returnDates.add((Calendar)c.clone());
}
c.add(Calendar.DAY_OF_YEAR, 1);
}
} catch (Exception e) {
e.printStackTrace();
}
return returnDates.toArray(new Calendar[]{});
}
}
MyNormalCalendar.java
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Properties;
import org.quartz.impl.calendar.HolidayCalendar;
/**
*
* Activate trigger on non-holiday dates.
*
* @author gz451w
*
*/
@SuppressWarnings("serial")
public class MyNormalCalendar extends HolidayCalendar {
private static final String WORKING_DAYS_LEGAL_HOLIDAYS = "workingDays.legalHolidays";
private Properties properties;
/**
* Exclude holidays
* @param properties
*/
public MyNormalCalendar(Properties properties) {
this.properties = properties;
String stringDates = getProperties().getProperty(WORKING_DAYS_LEGAL_HOLIDAYS);
if (stringDates != null) {
Calendar[] dates = getDates(stringDates);
for (Calendar date : dates) {
addExcludedDate(date.getTime());
}
}
}
/**
* @return the properties
*/
public Properties getProperties() {
return properties;
}
/**
* @param properties the properties to set
*/
public void setProperties(Properties properties) {
this.properties = properties;
}
/**
* Get holidays
* @param stringDates
* @return
*/
private Calendar[] getDates(String stringDates) {
String[] stringDatesArray = stringDates.split(",");
Calendar[] dates = new Calendar[stringDatesArray.length];
int i = 0;
for (String stringDate : stringDatesArray) {
String[] splitDate = stringDate.split("-");
Calendar date =
new GregorianCalendar(
Integer.parseInt(splitDate[0]),
Integer.parseInt(splitDate[1]) - 1, // Month field starts with 0!
Integer.parseInt(splitDate[2]));
dates[i++] = date;
}
return dates;
}
}
Accessing Spring’s applicationContext from a Quartz job
Quartz is a job scheduler for java. Execution periods can be expressed using the same expressions used in cron. Jobs are lauched by Quartz as static classes outside in a standalone way.
If a Quartz job uses a bean from inside a webapp, applicationContext from Spring must be shared with the job. I’ve found two resources in order to perform this operation:
Both articles explain the same. However, I must force my imagination to understand the first one and I can read the second one naturally. Curious.