Scheduling – Spring – Quartz

It is difficult to debate J2SE Vs J2EE scheduling. I.e Java timer utils Vs Quartz Scheduler.
I prefer to use Quartz, because it is most used, proven and well developed product.

http://onjava.com/onjava/2004/03/10/quartz.html

http://www.opensymphony.com/quartz/wikidocs/Tutorial.html
http://static.springsource.org/spring/docs/1.2.9/reference/scheduling.html
http://www.opensymphony.com/quartz/wikidocs/CronTriggers%20Tutorial.html

Finally it runs in Spring Container. If we need to simulate in Unit Tests or Eclipse for testing purpose use following sample code.

Unit Test Code / Example Code to initialize it.
It is not required, If we are running in container.

Spring Configuration File

<bean name="testJob" class="org.springframework.scheduling.quartz.JobDetailBean">
  <property name="jobClass" value="com.abcd.TestJob"/>
  <property name="jobDataAsMap">
    <map>
      <entry key="timeout" value="5"/>
    </map>
  </property>
</bean>

<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
  <property name="jobDetail" ref="testJob"/>
	<!-- run every minute -->
	<!-- Seoncs, Minutes, Hours, day of month, month, day of week, year -->
  <property name="cronExpression" value="0 * * * * ?"/>
</bean>

<bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  <property name="triggers">
    <list>
      <ref bean="cronTrigger"/>
    </list>
  </property>
  <property name="schedulerContextAsMap">
  	<map>
  	    <!-- The following classes are injected to jobClass by Quartz. Not by spring -->
  	    <entry key="propertyName" value-ref="referenceBeanName"/>
  	</map>
   </property>
</bean>

TestJob.java

package com.abcd;

import java.util.Date;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

public class TestJob extends QuartzJobBean {

	private int timeout;

	public void setTimeout(int timeout) {
		System.out.println(&quot;setTimeout==&gt;&quot; + timeout);
		this.timeout = timeout;
	}

	@Override
	protected void executeInternal(JobExecutionContext arg0)
			throws JobExecutionException {
		System.out.println(&quot;Job executed&quot; + new Date());

	}

}

TestScheduler.java

package com.asdf;

import java.util.Date;

import org.quartz.impl.StdScheduler;

public class TestScheduler {

	public static &lt;T&gt; T getObjectFromSpringContainer(final String beanId, final Class&lt;T&gt; clazz )
	{
		//Do coding to load Spring configuration files.
		return null;
	}

	public static void main(String[] str){
		
		//This starts the scheduler for testing purpose
		StdScheduler sch = (StdScheduler) getObjectFromSpringContainer(&quot;schedulerFactoryBean&quot;,StdScheduler.class);

		for (int i = 0; i &lt; 999999; i++) {
			System.out.println(&quot;Sleeping and waiting for scheduler to pickup jobs.==&gt;&quot; + i);
			try {
		
				System.out.println(&quot;is Started==&gt;&quot;+sch.isStarted());
				System.out.println(&quot;time pass loop:&quot;+new Date());
				
				Thread.sleep(60000);
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
	}
}

http://www.docjar.com/docs/api/org/springframework/scheduling/quartz/QuartzJobBean.html
http://forum.springsource.org/showthread.php?t=76974