home personal workZone travel about
code.pst » tech musings and code posts

Posts Tagged ‘quartz’

Injecting beans into Quartz job

Thursday, May 28th, 2009

Finally as promised the revised and more efficient way to inject beans directly into your quartz jobs. Simply use the jobDataAsMap property.

<bean name="jobBean" class="org.springframework.scheduling.quartz.JobDetailBean">
		<property name="jobClass" value="com.irahul.example.SpringQuartzJob" />	  
	  	<property name="name" value="jobName" />
	  	<property name="jobDataAsMap">
			<map>
				<entry key="setterName" value-ref="beanRef" />				
                                .....
			</map>
		</property>
	</bean>

Accessing Spring beans from Quartz jobs

Saturday, November 3rd, 2007

I had referred to Mark’s blog in my earlier post and actually now have a improvement over his solution. Define your context as usual (courtesy to Mark for his example)

<beans>
  <!-- Define the Job Bean that will be executed. 
  Our bean is named in the jobClass property. -->
  <bean name="myJob" class="org.springframework.scheduling.quartz.JobDetailBean">
    <property name="jobClass" value="com.gsoftware.common.util.MyJob"/>
  </bean>
  <!-- Associate the Job Bean with a Trigger. 
  Triggers define when a job is executed. -->
  <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
    <!-- see the example of method invoking job above -->
    <property name="jobDetail" ref="myJob"/>
    <property name="startDelay" value="2000"/>
    <property name="repeatInterval" value="10000"/>
  </bean>
  <!-- A list of Triggers to be scheduled and executed by Quartz -->
  <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="triggers">
      <list><ref bean="simpleTrigger"/></list>
    </property>
    <property name="applicationContextSchedulerContextKey" value="applicationContext"/>
  </bean>
</beans>

Note that in the factory I have added the property for the application context.
Now all you need to do in your job is extend the spring base class QuartzJobBean for a job and create a setter as follows:

public class MyJob extends QuartzJobBean {
  private ApplicationContext applicationContext;
 
  public void setApplicationContext(ApplicationContext appContext) {
    applicationContext = appContext;
  }
 
  @Override
  protected void executeInternal(JobExecutionContext executionContext) {
    //your code...
 
  }
}

Now you have the application context available in your job. Nothing else required!

OSGi – Spring Quartz job

Tuesday, October 30th, 2007

Mark’s blog provides a good example of how to provide the Spring application context to a Job. Here I will show how to ’inject’ OSGi services into your Spring OSGi application. This will become much cleaner once Spring Dynamic Modules executor bundle is released but at the moment using Spring 2.0.7 I need to provide some OSGi services to my Quartz job.

I have say a bundle ‘MyServiceBundle’ that registers a service with the following interface:

package com.irahul.example.myservicebundle; 
public interface TransactionService{ 
    void updateTransaction(Long id, String status); 
}

The activator looks something like:

package com.irahul.example.myservicebundle; 
public class Activator implements BundleActivator { 
    public void start(BundleContext bundleContext){ 
       TransactionService txnSvc = new ....//an implementation of it 
       Map<String,String> metaProperties = new... 
       //define whatever meta data you want to register for service discovery 
       bundleContext.registerService("com.irahul.example.TransactionService", 
          txnSvc,metaProperties); 
    } 
}

Now over in the bundle ‘MyQuartzJob’ define a JobContext which can be injected with all the beans it requires to perform the Job.

package com.irahul.example.myquartzjob; 
public interface JobContext{ 
    void setTransactionService(TransactionService txnSvc); 
    TransactionService getTransactionService(); 
}

Implement the QuartzJobBean to get the application context like in Mark’s blog. Now in your application context create an empty bean with your JobContext implementation.

<!-- Job Context --> 
<bean id="jobContext" class="com.irahul.example.myquartzjob.JobContextImpl"></bean>

After that all that remains is the activator, which looks like:

package com.irahul.example.myquartzjob
public class Activator implements BundleActivator { 
    public void start(BundleContext bundleContext){ 
    // Initialize the Spring application context 
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext( 
         new String[]{"context.xml"}); 
    //get the service 
    ServiceReference txnRef = bundleContext. 
               getServiceReference("com.irahul.example.TransactionService"); 
               //you can search on additonal meta data filter as well 
    TransactionService txnService = (TransactionService)bundleContext.getService(txnRef); 
    //now get the spring bean as setup 
    JobContext jobContext = (JobContext)applicationContext.getBean("jobContext"); 
    jobContext.setTransactionService(txnService); 
}

Your Job now has the application context all setup and ready to go!