Community   SpringSource   Projects    Downloads    Documentation    Forums    Training   Exchange   Blogs

Go Back   Spring Community Forums > Core Spring Projects > Core Container

Reply
 
Thread Tools Display Modes
  #1  
Old Mar 12th, 2005, 07:29 AM
vmarcinko vmarcinko is offline
Member
 
Join Date: Aug 2004
Posts: 63
Default Runtime Quartz jobs and ApplicationContext

Hi.

Am I missing something obvious here, but I dunno how to access ApplicationContext inside Quartz job who is created runtime? Or even better, I don't need ApplicationContext, I need some spring bean injected in it.

It seems to me that whole Spring support is concentrated around static jobs, that can be defined in applicationContext.xml beforehand, but my jobs are created in runtime, in other words, I fetch Scheduler and add newly created jobs through this interface. Parameters are stored inside job's JobDataMap, so I can fecth them during execution, but I dunno how to fecth spring bean from ApplicationContext?

Regards,
Vjeran
Reply With Quote
  #2  
Old Mar 13th, 2005, 05:33 PM
gmatthews gmatthews is offline
Senior Member
 
Join Date: Aug 2004
Location: Sydney
Posts: 503
Default

1. Set the applicationCOntextSchedulerContextKey property.

Code:
<bean name="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  <!-- name used to publish applicationContext to Quartz Job instances, so they can call business services -->
  <property name="applicationContextSchedulerContextKey"><value>applicationContext</value></property>
</bean>
2. From your Job's execute method.

Code:
SchedulerContext schedulerContext = jobExecutionContext.getScheduler().getContext();
ApplicationContext applicationContext = (ApplicationContext) schedulerContext.get("applicationContext");
Reply With Quote
  #3  
Old Mar 14th, 2005, 12:48 AM
vmarcinko vmarcinko is offline
Member
 
Join Date: Aug 2004
Posts: 63
Default

Thanx a bunch.

And now I'm greedy - is there some way for Spring to already inject required beans, rather than provide me with ApplicationContext where I would pull them from?
Reply With Quote
  #4  
Old Mar 14th, 2005, 01:27 AM
gmatthews gmatthews is offline
Senior Member
 
Join Date: Aug 2004
Location: Sydney
Posts: 503
Default

um...no.

but if you find out, let me know.
Reply With Quote
  #5  
Old Jun 22nd, 2005, 07:21 PM
rpatton rpatton is offline
Junior Member
 
Join Date: Jun 2005
Location: Santa Ana, CA, USA
Posts: 1
Default

Thanks for the info about getting the context, that helped me also. It looks to me like what prevents Spring from wiring up the job implementations is that Quartz takes a class object, not an instance of the jobs. It would be nice if that changed, though I can see why it is that way, so they aren't keeping objects alive forever I assume. Perhaps there is a way to do it through code generation / AOP.
Reply With Quote
  #6  
Old Jun 23rd, 2005, 07:26 PM
sethladd sethladd is offline
Senior Member
 
Join Date: Aug 2004
Location: Hawaii, US
Posts: 225
Default

Have you seen the MethodInvokingJobDetailFactoryBean ? This is just a proxy that uses reflection to call a method on some bean. I use it all the time, it works great. Plus, using this, I don't have to create a special Job bean. I just use quartz to call a method in my service layer.

Code:
	<bean id="receivedBusinessUpdaterJob" 
			class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject"><ref local="receivedBusinessUpdater"/></property>
		<property name="targetMethod"><value>updateAllBusinesses</value></property>
		<property name="concurrent"><value>false</value></property>
	</bean>
Reply With Quote
  #7  
Old Apr 21st, 2006, 11:58 AM
djcampbell djcampbell is offline
Junior Member
 
Join Date: Dec 2005
Posts: 16
Default Helpful thread

Just want to post a thanks as, once I could figure out which keywords I needed to search, this info solved my learning curve issue.

Regarding MethodInvokingJobDetailFactoryBean: I understand the concept, but I'm missing the details on how any parameter values are passed to the (say) business method being invoked? If this thread is not dead any clues would be appreciated.

Thx.
Reply With Quote
  #8  
Old Feb 6th, 2008, 12:21 AM
dpeker dpeker is offline
Junior Member
 
Join Date: Feb 2006
Posts: 3
Default JDBCJobStore

I am currently using JDBCJobStore and when the job executes I want to access applicationContect and get a handle to RMI service bean. Application Context access Method mentioned in this thread also works also in this case?
-- Deha Peker



Quote:
Originally Posted by gmatthews View Post
1. Set the applicationCOntextSchedulerContextKey property.

Code:
<bean name="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  <!-- name used to publish applicationContext to Quartz Job instances, so they can call business services -->
  <property name="applicationContextSchedulerContextKey"><value>applicationContext</value></property>
</bean>
2. From your Job's execute method.

Code:
SchedulerContext schedulerContext = jobExecutionContext.getScheduler().getContext();
ApplicationContext applicationContext = (ApplicationContext) schedulerContext.get("applicationContext");
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Scheduling Jobs with Quartz and Spring MmarcoM Core Container 3 Jul 6th, 2006 03:15 PM
NullPointerException Quartz & applicationContext jcbeck02 Core Container 0 Jul 13th, 2005 11:34 AM
Spring & Quartz. Scheduling stateful jobs via appcontext MmarcoM Core Container 1 May 15th, 2005 07:37 AM
runtime jobs using quartz without using application context? madx80 Core Container 3 Apr 6th, 2005 07:21 PM
Self-scheduling Quartz jobs rstearns01 Core Container 1 Aug 26th, 2004 09:08 AM


All times are GMT -5. The time now is 11:00 AM.


Contegix provides first-class managed hosting and partial sponsorship of these forums.

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.