gpt4 book ai didi

spring - 在 Tomcat 上使用 spring 部署 Quartz Scheduler 时出现 NullPointerException

转载 作者:行者123 更新时间:2023-12-04 02:44:14 25 4
gpt4 key购买 nike

我正在尝试将 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>

最佳答案

正如您所指出的,我们无法在 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!!

希望对你有帮助。

关于spring - 在 Tomcat 上使用 spring 部署 Quartz Scheduler 时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19180934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com