其他分享
首页 > 其他分享> > 在Tomcat上使用Spring部署Quartz Scheduler时发生NullPointerException

在Tomcat上使用Spring部署Quartz Scheduler时发生NullPointerException

作者:互联网

我试图将Quartz 2.2.0与spring 3.2.x一起使用,并使用ServletContextListener侦听FileChangeListener类.我的importManagerService对象是否为null?有什么建议么?没办法解决

部署时发生错误

 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

FileChangeListener

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

ScheduleImportFile

   **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>

解决方法:

如您所知,我们无法在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!!

希望这对您有所帮助.

标签:quartz-scheduler,spring
来源: https://codeday.me/bug/20191030/1967178.html