1

I have a web application I use spring application context to inject my beans which is annotated with @Autowired inside my application I have a service that have a method to generate report, this service is like this:

public ReportingService extends CommonService {
//this method is called from a controller to generate a report right now
//after filling the parameters of the report i.e. businessday
public generateReport(Request request) {.....}  
public generateScheduledReport() {
//read configured parameters from database and fill request
Request rr = ...;
generateReport(request);
}

for the scheduled report I have defined a job which extends QuartzJobBea` and used a field of my ReportingService to invoke its generateScheduledReport like this:

public class ScheduledReportJob extends QuartzJobBean {
@Autowired
@Qualifier("reportScheduler")
CommonService reportScheduler;
//getters and setters  
@Override
protected void executeInternal(JobExecutionContext arg0)
        throws JobExecutionException {
    reportScheduler.generateScheduledReport();

in my applicationContext I set quartz and spring beans as follows:

 <bean id"reportScheduler" class="com.monim.ReportingService"/>
 <bean name="schedulingjob" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
     <property name="jobClass"
        value="com.monim.ScheduledReportJob " />
     <property name="name" value="name" />
     <property name="group" value="group"/>
     <property name="durability" value="true" />
 </bean>
 <bean id="trigger"
    class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
     <property name="name" value="name" />
     <property name="group" value="group" />
     <property name="jobDetail" ref="schedulingjob" />  
     <property name="cronExpression" value="0/5 * * * * ?" />
    <!-- MISFIRE_INSTRUCTION_IGNORE_MISFIRE_POLICY - SEE MISFIRE INSTRUCTIONS 
    <property name="misfireInstruction" value="1" />  
  <bean id="Scheduler"
    class="org.springframework.scheduling.quartz.SchedulerFactoryBean"
    p:waitForJobsToCompleteOnShutdown="false" lazy-init="true">
     <property name="dataSource" ref="dataSource" />
     <property name="transactionManager" ref="transactionManager" />  
    <property name="overwriteExistingJobs" value="false" />
    <property name="quartzProperties">  
 <map>  
 <!-- scheduler configuration -->
 <entry key="org.quartz.scheduler.instanceName" value="scheduler" />
 <entry key="org.quartz.scheduler.skipUpdateCheck" value="true" />
 <entry key="org.quartz.jobStore.misfireThreshold" value="60000" />
 <!-- JobStore configuration -->
 <entry key="org.quartz.jobStore.class" value="org.quartz.impl.jdbcjobstore.JobStoreCMT" />
 <entry key="org.quartz.jobStore.dataSource" value="dataSource" />
 <entry key="org.quartz.jobStore.nonManagedTXDataSource" value="quartzDataSource" />
 <entry key="org.quartz.jobStore.driverDelegateClass" value="org.quartz.impl.jdbcjobstore.oracle.OracleDelegate"/>
<entry key="org.quartz.jobStore.tablePrefix" value="QRTZ_" />
<entry key="org.quartz.jobStore.useProperties" value="true" />
<entry key="org.quartz.jobStore.selectWithLockSQL"
value="SELECT * FROM {0}LOCKS WHERE SCHED_NAME = {1} AND LOCK_NAME = ? FOR UPDATE" />
<entry key="org.quartz.jobStore.isClustered" value="false" />
<!-- ThreadPool configuration -->
<entry key="org.quartz.threadPool.class" value="org.quartz.simpl.SimpleThreadPool" />
<entry key="org.quartz.threadPool.threadCount" value="3" />
</map>  
</property>  
<property name="applicationContextSchedulerContextKey">
    <value>applicationContext</value>
</property>
<property name="jobDetails">
<list>
<ref bean="schedulingjob" />
</list>
</property>
<property name="triggers">
<list>
<ref bean="trigger" />
</list>
</property>
</bean>

my ReportingService contains references to other beans which are also injected through application context. My problem is that the scheduler is working and have started successfully but all injected beans used to schedule the job are initialized to null, help please

monim
  • 3,427
  • 3
  • 23
  • 36

1 Answers1

5

after searching many threads and blogs, I have my application working by doing the following: in the first line of method executeInternal I put this line:
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this); which I found from an answer here
Then I launched the application "still beans are not injected" but this made quartz to save its triggers into the database which will be used later. after that I added to my job bean these properties:

  <property name="jobDataAsMap">
    <map>
      <entry key="schedulerTask" value-ref="reportWithTWService" />
     </map>
  </property>

and everything went right. But when adding these properties with quartz tables empty it doesnt work, still I hope if someone could explain to me what happened

Community
  • 1
  • 1
monim
  • 3,427
  • 3
  • 23
  • 36
  • 1
    My solution was based on this solution http://stackoverflow.com/questions/6990767/inject-bean-reference-into-a-quartz-job-in-spring/15211030#15211030. It's working correctly too. – Diogo de Góes Zanetti Jan 12 '16 at 12:00