Injecting spring bean to quartz jobs

Why my service always return a null pointer in quartz jobs? I was asked my self and try to do some research.

Finally, i know that the spring context and quartz was run as a different singleton context and start at the same time. That is why my service class always have a null value. Ok, now everything is clear for me. What i have to do now is injecting the service rather than made it auto wiring. (Auto wiring is also possible, you can read it at harinair blog)

Ok, now let’s start to create a scheduler.

1. Create a scheduler class that implements Job

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class HelloScheduler implements Job{
    public void execute(JobExecutionContext context) throws JobExecutionException {
        System.out.println("Hello World ..");
    }
}

2. Add this configuration to your applicationContext.xml

    <bean id="job" class="org.springframework.scheduling.quartz.JobDetailBean">
        <property name="jobClass" value="org.jug.ind.scheduler.HelloScheduler" />
        <property name="jobDataAsMap">
            <map>
                <entry key="message" value="executing job!" />
            </map>
        </property>
    </bean>

    <bean id="trigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
        <property name="jobDetail" ref="job" />
        <property name="startDelay" value="5000" />
        <property name="repeatInterval" value="1000" />
        <property name="jobDataAsMap">
            <map>
                <entry key="triggerMessage" value="Trigger Message from trigger" />
            </map>
        </property>
    </bean>

    <bean id="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref local="trigger" />
            </list>
        </property>
    </bean>

As you can see at the definition above, the scheduler Factory will invoke SimpleTriggerBean to execute the overide execute method at JobDetailBean.

Just about the time if you use spring, you will find that you need to invoke some method in spring service. Let’s assume that you have a service name “myService”, and you want to execute some method at those class. Now, what you have to do are :

1. Modified the bean definition

    <bean id="job" class="org.springframework.scheduling.quartz.JobDetailBean">
        <property name="jobClass" value="org.jug.ind.scheduler.HelloScheduler" />
        <property name="jobDataAsMap">
            <map>
                <entry key="message" value="executing job!" />
                <entry key="myService" value-ref="myService" />
            </map>
        </property>
    </bean>

2. Add setter and getter to your job class.

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class HelloScheduler implements Job {
    private MyService myService;

    public void execute(JobExecutionContext context) throws JobExecutionException {
        myService = (MyService)context.getJobDetail().getJobDataMap().get("myService");
        System.out.println("Hello World ..");
        myService.sayHello();
    }
}

Now you can use your service for your quartz scheduler. Happy Coding All.. :D

Incoming search terms:

  • add service in jobdetailbean (10)
  • jobdetailbean - set attributes on jobclass instance (4)
  • how to inject a bean to jobclass (4)
  • quartz scheduler jobdataasmap example (3)
  • spring context in quartz job (1)
  • spring 3 jobexecutioncontext get bean (1)
  • quartz_jobs jobdataasmap (1)
  • quartzjobbean spring inject singleton class (1)
  • quartzjobbean (1)
  • spring mvc quartz (1)
0saves
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Tagged , , , . Bookmark the permalink.

7 Responses to Injecting spring bean to quartz jobs

  1. David says:

    Very useful. Thak you very much.

  2. drnrusu says:

    Great material,
    Thanks a lot :)

  3. nice article bro! thanks for share!

  4. Thanks a lot :)

    Just want to clarify a thing: the link to http://www.harinair.com/2008/01/spring-quartz-and-auto-wiring-of-quartz-jobs/ is not about how to make @Autowired run in Quartz jobs. It’s basically the same with what you’re doing here.

    • hi lee, sorry for a very late reply.

      i was too busy to maintain my web until google sent me a message to update my blog.
      Ya, perhaps you are right. i was create this blog after read his blog about those.
      it has been a year since i worked with spring but perhaps now in the new version, they got a native interface to bridge this problem.
      So no need to do it manually like what has been written here(i guess)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>