3

我正在尝试将 Quartz 2.2.0 与 spring 3.2.x 一起使用,使用 ServletContextListener 来监听 FileChangeListener 类。我的 importManagerService 对象是否为空?有什么建议么?不知道如何解决

部署时出错

 INFO  [2013-10-04 15:13:16.009] [localhost-startStop-1]: org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization started
    ERROR [2013-10-04 15:13:16.061] [DefaultQuartzScheduler_Worker-1]: org.quartz.core.JobRunShell - Job g1.t1 threw an unhandled Exception: 
    java.lang.NullPointerException at
   com.demo.portal.web.importExportFile.ScheduleImportFile.execute(ScheduleImportFile.java:40)
        at org.quartz.core.JobRunShell.run(JobRunShell.java:207)
        at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:560)
    ERROR [2013-10-04 15:13:16.065] [DefaultQuartzScheduler_Worker-1]: org.quartz.core.ErrorLogger - Job (g1.t1 threw an exception.
    org.quartz.SchedulerException: Job threw an unhandled exception. [See nested exception: java.lang.NullPointerException]
        at org.quartz.core.JobRunShell.run(JobRunShell.java:218)
        at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:560)
    Caused by: java.lang.NullPointerException
        at com.happiestminds.portal.web.importExportFile.ScheduleImportFile.execute(ScheduleImportFile.java:40)
        at org.quartz.core.JobRunShell.run(JobRunShell.java:207)
        ... 1 more

文件更改监听器

 public class FileChangeListener implements ServletContextListener {

        private static final Logger logger = Logger.getLogger(FileChangeListener.class);
        @Override
        public void contextDestroyed(ServletContextEvent arg0) {    
            System.out.println("Stopping Application successfully");
        }

        @Override
        public void contextInitialized(ServletContextEvent arg0) {  
            logger.info("Initializing Application successfully..........");    
            JobDetail job = JobBuilder.newJob(ScheduleImportFile.class).withIdentity("t1", "g1").build();
            Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "g1").startNow()
                    .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(60).repeatForever()).forJob("t1", "g1").build();
            SchedulerFactory schFactory = new StdSchedulerFactory();
            Scheduler sch;
            try {
                sch = schFactory.getScheduler();
                sch.start();
                sch.scheduleJob(job, trigger);
            } catch (SchedulerException e) {
                e.printStackTrace();
            }
        }
    }

计划导入文件

   **public class ScheduleImportFile implements Job{
@Autowired
    ImportManagerService importManagerService;      
@Override
        public void execute(JobExecutionContext arg0) throws JobExecutionException {
            //some logic for reading and parsing files
            Line no. 40
            Map<String, List<File>> fileToBeProcessMap = importManagerService.getFilesInfo(config);
            Config is object of Configuration class 
    }**

Web.xml

<listener>
         <listener-class>com.demo.portal.web.importExportFile.FileChangeListener</listener-class>
    </listener>
4

1 回答 1

4

正如您所指出的,我们不能在 Quartz 作业中自动连接 Spring bean,因为 Spring Bean 的生命周期在作业类中是被禁止的。

但是我们可以通过一种简单的方式来获取那些spring bean,而无需再次加载Spring bean xml。就这个。

public class MyJob  Implements Job
{
private MyBean myBean;

   @Override
   public void execute(JobExecutionContext context) throws JobExecutionException
   {
     getBeansFromContext(context);
     mybean.doSomeThing();
   }

   private void getBeansFromContext(JobExecutionContext context) throws SchedulerException
   {
        ApplicationContext applicationContext = (ApplicationContext)context.getScheduler().getContext().get("applicationContext");
        this.mybean=applicationContext.getBean(MyBean.class);
   }
}

您应该在 beans.xml 中配置 schedulerFactoryBean。

<beans:bean id="schedulerFactoryBean"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<beans:property name="jobFactory">
    <beans:bean class="org.springframework.scheduling.quartz.SpringBeanJobFactory"></beans:bean>
</beans:property>
...
<beans:property name="applicationContextSchedulerContextKey"
    value="applicationContext" /> -- Here is the guy!!

希望这对您有所帮助。

于 2013-10-07T17:17:20.797 回答