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 -->




<!-- QUARTZ Configuration -->


	
            
            
        
    

        <map>
            
            
        </map>
    


<!-- Everyday but holidays -->

    


<!-- Every holiday -->

    


<!-- Normal Job -->




        <!-- Every five minutes from 8h to 14h -->
        0 0/5 8-14 * * ?
    


<!-- Holiday Job -->




        <!-- Every five minutes from 8h to 14h -->
        0 0/5 8-14 * * ?
    

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 dates = new ArrayList();
    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 returnDates = new ArrayList();
    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.