Terracotta Quartz 2.x support in Spring 3.1.RC1

Quartz 2.x support for Spring is finally here! Or here’s the changelog of the release.

Advertisements

Spring, Quartz, Maven example

This is a quick head-start Maven project showcasing Spring and Quartz integration.

First the POM will look like:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.dimitrisli.springQuartz</groupId>
  <artifactId>SpringQuartz</artifactId>
  <version>1.0</version>
  <build>
  	<plugins>
  		<plugin>
  			<groupId>org.apache.maven.plugins</groupId>
  			<artifactId>maven-compiler-plugin</artifactId>
  			<version>2.3.2</version>
  			<configuration>
  				<source>1.6</source>
  				<target>1.6</target>
  				<encoding>${project.source.sourceEncoding}</encoding>
  			</configuration>
  		</plugin>
  	</plugins>
  </build>
  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring</artifactId>
      <version>2.5.6</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
    	<groupId>org.quartz-scheduler</groupId>
    	<artifactId>quartz</artifactId>
    	<version>1.8.0</version>
    </dependency>
  </dependencies>
  <properties>
  	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
</project>

The main task we want to repeat is created inside of our bean:

package com.dimitrisli.quartz;

public class ProcessToExecute {

	public void output() {
		System.out.println("important stuff");
	}
}

When the job wakes up it will come to the executeInternal() method of ProcessToExecuteJob:

package com.dimitrisli.quartz;

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

public class ProcessToExecuteJob extends QuartzJobBean{

	private ProcessToExecute processToExecute;
	
	public void setProcessToExecute(ProcessToExecute processToExecute){
		this.processToExecute = processToExecute;
	}
	
	@Override
	protected void executeInternal(JobExecutionContext jobContext)	throws JobExecutionException {
		processToExecute.output();
	}

}

Finally the bean config file is defined like this:

<?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">

<bean id="processToExecute" class="com.dimitrisli.quartz.ProcessToExecute" />

<bean name="processToExecuteJob" class="org.springframework.scheduling.quartz.JobDetailBean">
	<property name="jobClass" value="com.dimitrisli.quartz.ProcessToExecuteJob" />
	<property name="jobDataAsMap">
		<map>
			<entry key="processToExecute" value-ref="processToExecute" />
		</map>
	</property>
</bean>

	<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
		<property name="jobDetail" ref="processToExecuteJob" />
		<property name="repeatInterval" value="5000" />
		<property name="startDelay" value="2000" />
	</bean>

	<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail" ref="processToExecuteJob" />
		<property name="cronExpression" value="0/5 * * * * ?" />
	</bean>

	<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="jobDetails">
			<list>
				<ref bean="processToExecuteJob" />
			</list>
		</property>
		<property name="triggers">
			<list>
				<ref bean="simpleTrigger" />
			</list>
		</property>
	</bean>

</beans>

The source code can be found in this Github repository.