2013-10-04 104 views
3

我試圖在Spring 3.2.x中使用Quartz 2.2.0,使用ServletContextListener監聽FileChangeListener類.Is我的importManagerService對象爲null?有什麼建議麼?沒有得到如何解決它在Tomcat上用Spring部署Quartz Scheduler時出現NullPointerException

錯誤在部署

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> 
+0

我通過刪除Autowired註釋解決了這個問題,但除了創建一個bean xml外,還有其他任何方法,以便我們可以使用自動裝配對象和石英。在此先感謝 –

回答

4

當你確定,我們不能石英工作作爲春季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); 
    } 
} 

您應該將您的schedulerFactoryBean配置到您的beans.xml中。

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

希望這可以幫助你。

+0

謝謝@devThoughts。只是今天檢查你的答案,長期以來沒有在這裏活躍.. :) upvoted你的答案 –

相關問題