<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>mikwat code &#187; spring</title>
	<atom:link href="http://code.mikwat.com/archives/category/spring/feed" rel="self" type="application/rss+xml" />
	<link>http://code.mikwat.com</link>
	<description>A coder's daily explorations through PHP, Java, CSS, JavaScript, Linux, OS X, Apache, Tomcat, and everything else.</description>
	<lastBuildDate>Wed, 20 Feb 2008 22:14:01 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Quartz + Spring + Hibernate with JDBC Job Store</title>
		<link>http://code.mikwat.com/archives/24</link>
		<comments>http://code.mikwat.com/archives/24#comments</comments>
		<pubDate>Thu, 04 Oct 2007 23:25:15 +0000</pubDate>
		<dc:creator>mikwat</dc:creator>
				<category><![CDATA[hibernate]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[quartz]]></category>
		<category><![CDATA[spring]]></category>

		<guid isPermaLink="false">http://code.mikwat.com/archives/24</guid>
		<description><![CDATA[Here is how I configured Quartz to work with Spring and Hibernate.  I&#8217;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

...
	&#60;!-- Quartz --&#62;
	&#60;bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"&#62;
		&#60;property name="applicationContextSchedulerContextKey" value="applicationContext"/&#62;
		&#60;property name="configLocation" value="classpath:quartz.properties"/&#62;
	&#60;/bean&#62;
...

The Quartz settings are [...]]]></description>
			<content:encoded><![CDATA[<p>Here is how I configured Quartz to work with Spring and Hibernate.  I&#8217;m using the Quartz JDBC Job Store which stores and retrieves the job settings from a database instead of a static configuration.</p>
<p>I use the <a href="http://static.springframework.org/spring/docs/1.2.x/api/index.html?org/springframework/orm/hibernate3/HibernateTransactionManager.html" onclick="pageTracker._trackPageview('/outgoing/static.springframework.org/spring/docs/1.2.x/api/index.html?org/springframework/orm/hibernate3/HibernateTransactionManager.html&amp;referer=');">SchedulerFactoryBean</a> to manager the Quartz Scheduler lifecycle.</p>
<p><b>spring-servlet.xml</b></p>
<pre>
...
	&lt;!-- Quartz --&gt;
	&lt;bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"&gt;
		&lt;property name="applicationContextSchedulerContextKey" value="applicationContext"/&gt;
		&lt;property name="configLocation" value="classpath:quartz.properties"/&gt;
	&lt;/bean&gt;
...
</pre>
<p>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.</p>
<p><b>quartz.properties</b></p>
<pre>
...
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
</pre>
<p><b>MyBaseJob</b></p>
<pre>
public abstract class MyBaseJob extends QuartzJobBean
{
    private transient ApplicationContext applicationContext = null;

    public ApplicationContext getApplicationContext()
    {
        return applicationContext;
    }

    public void setApplicationContext(ApplicationContext applicationContext)
    {
        this.applicationContext = applicationContext;
    }
}
</pre>
<p>Each of my job classes extend MyBaseJob, overriding <code>executeInternal</code>.  The jobs can use the <code>applicationContext</code> to lookup Spring service beans.  In my environment these service beans extend <code>HibernateDaoSupport</code>.</p>
<pre>
    protected void executeInternal(JobExecutionContext ctx) throws JobExecutionException
    {
        MyServiceManager sm = applicationContext.getBean("myServiceManager"); // lookup Spring service bean
        ...
    }
</pre>
]]></content:encoded>
			<wfw:commentRss>http://code.mikwat.com/archives/24/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
