Quartz + Spring + Hibernate with JDBC Job Store
Here is how I configured Quartz to work with Spring and Hibernate. I’m using the Quartz JDBC Job Store which stores and retrieves the job settings from a database instead of a static configuration.
I use the SchedulerFactoryBean to manager the Quartz Scheduler lifecycle.
spring-servlet.xml
... <!-- Quartz --> <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="applicationContextSchedulerContextKey" value="applicationContext"/> <property name="configLocation" value="classpath:quartz.properties"/> </bean> ...
The Quartz settings are stored in a quartz.properties file so that they can be unique to each instance of the web application. This setup requires that quartz.properties be on the classpath. This file defines the JDBC connection for Quartz to use.
quartz.properties
... org.quartz.dataSource.myDS.jndiURL=java:comp/env/jdbc/MyDS org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate org.quartz.jobStore.dataSource=myDS org.quartz.jobStore.tablePrefix=JB_ org.quartz.jobStore.useProperties=true
MyBaseJob
public abstract class MyBaseJob extends QuartzJobBean
{
private transient ApplicationContext applicationContext = null;
public ApplicationContext getApplicationContext()
{
return applicationContext;
}
public void setApplicationContext(ApplicationContext applicationContext)
{
this.applicationContext = applicationContext;
}
}
Each of my job classes extend MyBaseJob, overriding executeInternal. The jobs can use the applicationContext to lookup Spring service beans. In my environment these service beans extend HibernateDaoSupport.
protected void executeInternal(JobExecutionContext ctx) throws JobExecutionException
{
MyServiceManager sm = applicationContext.getBean("myServiceManager"); // lookup Spring service bean
...
}