0

I don't know how to access to the original context of my application from a Quartz Job.

I can see that both context are not communicated because I am trying to inject a DAO bean as a property of my Quartz Job bean and I get this error:

No such bean named: "the DAO bean that I am trying to inject as a property".

Is there any way to use an DAO from my QuartzJob?

halfer
  • 19,824
  • 17
  • 99
  • 186
juldeh
  • 129
  • 13
  • 1
    Tip: when communicating in English, use English acronyms. Else you'll have people wondering what e.g. an OAD is, instead of answering your question. – walen Feb 20 '17 at 15:00
  • 1
    Possible duplicate of [Using Hibernate session with quartz](http://stackoverflow.com/questions/4446103/using-hibernate-session-with-quartz) – Mario Santini Feb 20 '17 at 15:12

1 Answers1

0

(Posted on behalf of the OP).

Solution:

In the Job (it is mandatory to get an Interface):

public class SchedulerJob extends QuartzJobBean {
public void executeInternal(JobExecutionContext context)
        throws JobExecutionException {
    try{
        <YOUR_BEAN_DAO_INTERFACE_OBJECT> = ((ApplicationContext) context.getJobDetail().getJobDataMap().get("applicationContext")).get("<YOUR_BEAN_DAO_INTERFACE_ID>");
    } catch (Exception e ){
        e.printStackTrace();
        return;
    }
}
}

In the .xml context of the application: It is also necessary to declare <YOUR_BEAN_DAO_INTERFACE> in this XML as a bean:

<!-- Spring Quartz Scheduler job -->
<bean name="schedulerJob" class="org.springframework.scheduling.quartz.JobDetailBean">
    <property name="jobClass" value="<PATH_OF_YOUR_CLASS_JOB>.SchedulerJob" />
    <property name="applicationContextJobDataKey" value="applicationContext" />
</bean>

<!-- Cron Trigger, run every 10 seconds -->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
    <property name="jobDetail" ref="schedulerJob" />
    <property name="cronExpression" value="0/10 * * * * ?" />
</bean>

<!-- DI -->
<bean id="scheduler"
    class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="jobDetails">
        <list>
            <ref bean="schedulerJob" />
        </list>
    </property>

    <property name="triggers">
        <list>
            <ref bean="cronTrigger" />
        </list>
    </property>
</bean>
halfer
  • 19,824
  • 17
  • 99
  • 186