Sunday, May 30, 2010

Quartz Scheduler example in Java using Spring


Requirement: Run a task (process) without user intervention at customized regular intervals of time and perform service to your application.
Solution: Quartz Scheduler has this feature of scheduling jobs at specified intervals of time mentioning from seconds to years called cron expression.

  • In spring framework scheduling package we have this Quartz included, so just download the jar spring-context-support.jar into your application.
  • Now we need to identify and configure the Class and the method that needs to be triggered at regular intervals with the cron expression.

Let us move forward with a requirement scenario, say you have an application which brings all the stocks details from another service provider and you want to get those details every afternoon at 2:30 pm.
Say you already have the functionality with the help of a class StocksScheduler.class which extends ContextLoaderPlugIn in spring-webmvc-struts.jar, we have to extend ContextLoaderPlugIn because we want the scheduler to start working on my application start.

Step 1: Define your bean (Class) in which your task is implemented that you want to perform

<bean id="stocksScheduler" class="com.app.stocks.StocksLoader"/>

Step 2: Define the method which has the functionality that you have implemented in the targetMethod property and your class in targetObject property

          <bean  id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
              <property name="targetObject" ref="stocksScheduler" />
              <property name="targetMethod" value="getDailyStocks" />
         </bean>
     
Step 3: Define the cron expression, i.e. the interval that you want your getDailyStocks to run and save in your application, the jobDetail and the cropnExpression

<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
               <property name="jobDetail" ref="jobDetail" />
              <property name="cronExpression" value="${scheduling.stocksCronExpression}" />
            </bean>

Step 4: The scheduling.stocksCronExpression is a property in the property file scheduling.properties file which is defined in your app-context.xml as below
<bean id="propertyConfigurer"
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
              <property name="locations">
                  <list>
                        <value>scheduling.properties</value>
                  </list>
              </property>
          </bean>

Step 5: Scheduling.properties file will contain this property scheduling.stocksCronExpression as follows. The cron expression has to be changed according your required interval.  The following cron expression will make the stocksScheduler run every day at 2:30 pm.

          scheduling.stocksCronExpression = 0 30 14 ? * *

Step 6: We have to include this following plugin into the struts-config.xml file for making our scheduler when the context is loaded.

          <plug-in className="com.app.stocks.StocksLoader" >
                 <set-property property="contextConfigLocation" value="/WEB-INF/app-context.xml"/>
          </plug-in>