I use Spring 4.0 and have moved a project from xml to java-config, and everything works except accessing an @Service("scheduleService")
annotated class from QuartzJobBean.executeInternal
.
The xml-bit I had to make it work was:
<bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="schedulerContextAsMap">
<map>
<entry key="scheduleService" value-ref="scheduleService" />
<entry key="knxUtil" value-ref="knxUtil" />
</map>
</property>
</bean>
Then, for scheduling the Job inside @Service("scheduleService")
I used:
JobBuilder jobBuilder = JobBuilder.newJob(ScheduledActionRunner.class)
Furthermore, to actually execute the Job
I had it working like this:
@Component
public class ScheduledActionRunner extends QuartzJobBean {
private KNXUtil knxUtil;
private ScheduleService scheduleService;
public ScheduledActionRunner() {
}
@Autowired
public void setScheduleService(ScheduleService scheduleService) {
this.scheduleService = scheduleService;
}
@Autowired
public void setKnxUtil(KNXUtil knxUtil) {
this.knxUtil = knxUtil;
}
@Override
public void executeInternal(JobExecutionContext context) throws JobExecutionException {
JobDataMap jobDataMap = context.getMergedJobDataMap();
String scheduleId = jobDataMap.getString("scheduleId");
Schedule schedule = scheduleService.get(scheduleId);
Set<ScheduledAction> actions = schedule.getScheduledActions();
for (ScheduledAction scheduledAction : actions) {
scheduledAction.getAction().execute(logger, knxUtil);
}
}
As mentioned above, all this used to work while using xml-configuration.
Now, with java-config, it fails with NullPointerException
at scheduleService.get(scheduleId);
For java-configuration I set up the SchedulerFactoryBean
like this:
@Configuration
@PropertySource(value = "classpath:properties.${target_env:dev}.properties")
@ComponentScan(basePackages = { "com.example.smart" }
public class SpringRootApplication {
@Autowired
private ScheduleService scheduleService;
@Autowired
private KNXUtil knxUtil;
@Bean
SchedulerFactoryBean schedulerFactoryBean() {
SchedulerFactoryBean bean = new SchedulerFactoryBean();
Map<String, Object> schedulerContextAsMap = new HashMap<String, Object>();
schedulerContextAsMap.put("scheduleService", scheduleService);
schedulerContextAsMap.put("knxUtil", knxUtil);
bean.setSchedulerContextAsMap(schedulerContextAsMap);
return bean;
}
}
How can I insert the reference to scheduleService
inside the schedulerContextAsMap
using java-config?