ECM
ECM
总版主
总版主
  • 社区居民
  • 忠实会员
  • 原创写手
阅读:851回复:0

How to use SchedulerFactoryBean.schedulerContextAsMap with javaconfig

楼主#
更多 发布于:2016-05-16 10:53
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?
answer(解决方案):

I found the answer here: http://stackoverflow.com/a/17394905/272180 The solution was to remove bean.setSchedulerContextAsMap(schedulerContextAsMap) when setting up SchedulerFactoryBean, and modify executeInternal as follows: @Autowired
private KNXUtil         knxUtil;

@Autowired
private ScheduleService scheduleService;

@Override
public void executeInternal(JobExecutionContext context) throws JobExecutionException {

    // Adding this autowires everything as needed
    SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);

    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);
    }

}
 This also simplifies the code since the @Autowired annotated setters are not needed anymore.

 

 

 

 

 

 

 

异常中心网是一家专门收集整理程序员编程过程中遇到的常见异常(exception)以及各种异常问答中心的网站。异常中心网旨在,减少程序员在编码遇到异常,处理各种异常时间和痛苦,让程序员能更愉快的、快速的定位异常并查找对应的异常解决方案。异常中心网诚心打造最完美的编程社区为程序员用户服务,努力成为最好的程序员乐园程序员社区程序异常中心程序bug中心异常问答中心

 

喜欢0 评分0
游客

返回顶部