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("setTimeout==>" + timeout);
this.timeout = timeout;
}
@Override
protected void executeInternal(JobExecutionContext arg0)
throws JobExecutionException {
System.out.println("Job executed" + new Date());
}
}
TestScheduler.java
package com.asdf;
import java.util.Date;
import org.quartz.impl.StdScheduler;
public class TestScheduler {
public static <T> T getObjectFromSpringContainer(final String beanId, final Class<T> 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("schedulerFactoryBean",StdScheduler.class);
for (int i = 0; i < 999999; i++) {
System.out.println("Sleeping and waiting for scheduler to pickup jobs.==>" + i);
try {
System.out.println("is Started==>"+sch.isStarted());
System.out.println("time pass loop:"+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



