I'm trying to set up Quartz using a JobStoreTX to keep my jobs and triggers. Right now I'm using the SchedulerFactoryBean to set up quartz and loading the initial job data with the XMLSchedulingDataProcessorPlugin.
application-context.xml
quartz.propertiesCode:<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="applicationContextSchedulerContextKey" value="applicationContext" /> <property name="configLocation" value="classpath:quartz.properties" /> <property name="dataSource" ref="dataSource" /> <property name="transactionManager" ref="transactionManager" /> </bean>
quartz_data.xmlCode:org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.oracle.OracleDelegate org.quartz.jobStore.tablePrefix = QRTZ_ org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin org.quartz.plugin.jobInitializer.fileNames = quartz_data.xml org.quartz.plugin.jobInitializer.failOnFileNotFound = true org.quartz.plugin.jobInitializer.scanInterval = 0 org.quartz.plugin.jobInitializer.wrapInUserTransaction = false
In my com.blah.ScheduledJob class, I am getting the bean from the like so:Code:<schedule> <job> <name>scheduledJob</name> <group>DEFAULT</group> <description>Process blah...description> <job-class>com.blah.ScheduledJob</job-class> <volatility>false</volatility> <durability>false</durability> <recover>false</recover> </job> <trigger> <cron> <name>scheduledTrigger</name> <group>DEFAULT</group> <job-name>scheduledJob</job-name> <job-group>DEFAULT</job-group> <!-- every minute --> <cron-expression>0 * * ? * *</cron-expression> </cron> </trigger> </schedule>
Outside of Quartz and inside my spring application, my service implementation works fine.Code:public class ScheduledJob implements StatefulJob { public void execute(JobExecutionContext context) throws JobExecutionException { ApplicationContext ctx = context.getScheduler().getContext().get("applicationContext"); MyServiceInterface mbi = (MyServiceInterface) ctx.getBean(MyServiceInterface.class); mbi.doDatabaseStuff(); } }
I know that Quartz is creating a new instance of my ScheduledJob class in a new thread. That's why I was using the getBean() method to get my service implementation which I thought was already set up with the datasource and transaction manager, but when my service tries to do any database access from Scheduledjob, it throws a the No Session found for current thread exception.
Do I need to set up the trigger and job as beans instead of using the xml file? Wouldn't I still have the issue with the thread? I saw some posts about a using a HibernateInterceptor somehow, but I also saw that it doesn't exist for Hibernate 4.
I'm very frustrated - any suggestions?


Reply With Quote
