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.

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">
      <entry key="timeout" value="5"/>

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

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;

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



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());
			} catch (Exception ex) {

#quartz, #spring-framework-scheduler