Quartz does not support @autowired

I am performing a quartz job using the DAO service as shown below:

public class InitialFetchFrequenceScheduleJob implements Job { @Autowired private FetchFrequencyService fetchFrequencyService; @Override public void execute(JobExecutionContext context) throws JobExecutionException { try { List<FetchFrequency> frequencies = this.fetchFrequencyService.findAll(FetchFrequency.class); 

The problem is that when calling the execute () this.fetchFrequencyService.findAll(FetchFrequency.class); will call NPE because fetchFrequenceService is null. Am I something wrong here? Any answer is much appreciated. Thanks!

P / s I am using Quartz 2.1.7

Update: this is FetchFrequencyServiceImpl:

 @Service("fetchFrequencyService") public class FetchFrequencyServiceImpl extends GenericDaoImpl implements FetchFrequencyService { } 

Update: code implementation job:

 JobDetail job = JobBuilder.newJob(InitialFetchFrequenceScheduleJob.class).build(); Calendar cal = Calendar.getInstance(); cal.add(Calendar.MINUTE, 1); Trigger trigger = TriggerBuilder.newTrigger().forJob(job).startAt(cal.getTime()).build(); Scheduler scheduler = new StdSchedulerFactory("quartz.properties").getScheduler(); scheduler.start(); scheduler.scheduleJob(job, trigger); 
+5
source share
4 answers

@Autowired will not work in the Quartz implementation because it will not be instantiated by Spring. To get Spring -managed beans in a quartz job, you must first use org.springframework.scheduling.quartz.SchedulerFactoryBean to control the life cycle of the quartz. Using this FactoryBean, you can specify the applicationContextSchedulerContextKey property to reference the ApplicationContext provided to your Quartz job in the scheduler context, for example:

 <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="applicationContextSchedulerContextKey" value="applicationContext" /> <!-- additional properties here --> </bean> 

Now you can get the ApplicationContext link in your job, and then explicitly get the bean link from the ApplicationContext :

 @Override public void execute(JobExecutionContext context) throws JobExecutionException { ApplicationContext applicationContext = (ApplicationContext) executionContext .getScheduler().getContext().get("applicationContext"); FetchFrequencyService service = applicationContext.getBean(FetchFrequencyService.class); // Start using your service. } 
+5
source

The way you use Quartz does not include Spring in the process at all, and therefore no dependency posting occurs.

I suggest you take a look at this part of the official documentation for basic information on how to integrate Spring with Quartz as and this excellent SO answer

+1
source

use:

 SpringBeanAutowiringSupport.processInjectBasedOnCurrentContext(this) 

to autwire your beans in your quartz job

0
source

you must annotate your classes with any of the following annotations

for DAO

@Repository

service

@Service

dispatcher

@Controller

therefore, your class of service should have an annotation below.

 @Service("fetchFrequencyService") public Class FetchFrequencyService { } 

I need to add application context in SchedulerFactoryBean

 <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="false"> <property name="jobFactory"> <bean class="org.springframework.scheduling.quartz.SpringBeanJobFactory" /> </property> <property name="applicationContextSchedulerContextKey"> <value>applicationContext</value> </property> </bean> 
-1
source

Source: https://habr.com/ru/post/1202051/


All Articles