2013-10-04 4 views
3

Spring 3.2.x에서 Quartz 2.2.0을 사용하려고하는데 ServletContextListener를 사용하여 FileChangeListener 클래스를 수신하려고합니다. 내 importManagerService 객체가 null입니까? 어떤 제안?Tomcat에 Spring이있는 Quartz Scheduler를 배포하는 동안 NullPointerException이 발생했습니다.

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 
    }** 
,691

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을 배포하는 동안 그것을

오류를 해결하는 방법을 점점되지 않음 당신이 확인 된 363,210

의 Web.xml은

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

Autowired 주석을 제거하여이 문제를 해결했지만 autowired object와 quartz를 모두 사용할 수 있도록 Bean XML을 작성하는 것과는 다른 방법이 있습니다. 감사합니다 사전에 –

답변

4

, 우리는 봄 콩의 수명주기와 같은 석영 작업 내부 스프링 빈의 자동 와이어는 작업 클래스 측면에서 금지 할 수 없다.

하지만 Spring bean xml을 다시로드하지 않고도 간단한 방법으로 해당 봄 bean을 얻을 수 있습니다. 여기 있습니다.

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

희망이 도움이 될 것입니다.

+0

감사합니다 @devThoughts. 방금 오늘 당신의 대답을 확인하고, 오랜 시간 이래로 여기에서 활발하지 않았습니다. :) 당신의 대답을 upvoted –

관련 문제