A Simple Example of creating a Scheduled Job using Spring and Quartz

This example use the MethodInvokingJobDetailFactoryBean

Required Jars:

  • spring-core.jar
  • spring-beans.jar
  • spring-context.jar
  • spring-context-support.jar
  • spring-tx.jar
  • quartz-1.5.2.jar

Create the class that you want to be executed by the job (this should be your bussiness object)

package com.scheduler.job;

public class Jobs {
	public void processSomething(){
		System.out.println("execute processSomething()");
	}
}

Create the spring quartz configuration and import the file on your application context configuration

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

	<!-- Create bean of Jobs object -->
	<bean id="jobs" class="com.scheduler.job.Jobs"/>

	<!-- Create the Jobs Detail using Method Invocation -->
	<bean id="doJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
	  <property name="targetObject" ref="jobs" />
	  <property name="targetMethod" value="processSomething" />
	  <property name="concurrent" value="false" />
	</bean>

	<!-- Create the trigger for the Job (doJob) -->
	<bean id="theTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
		<property name="jobDetail" ref="doJob" />
		<property name="startDelay" value="1000" />
		<property name="repeatInterval" value="5000" />
	</bean>

	<!-- This is an example triger using cron expression -->
	<!--
	<bean id="theTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail" ref="doJob" />
		<property name="cronExpression" value="0 59 23 * * ?"/>
	</bean>
	-->

	<!-- Add the triggers to SchedulerFactoryBean -->
	<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				<ref bean="theTrigger" />
			</list>
		</property>
	</bean>

</beans>


Similar Posts:

Bookmark:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • description
  • LinkedIn
  • Slashdot
  • Technorati
  • TwitThis
  • Yahoo! Buzz

Leave a comment

Your comment

Spam Protection by WP-SpamFree