是的,我会将列表存储为应用程序范围的属性。相反,从数据库中提取数据的效率可能较低,因为您只是每小时更新一次列表。为了向 Quartz 任务提供对对象的引用,可能需要创建 a。只能从 JavaEE 相关的类(如 Servlets 和侦听器)中检索。ServletContext
ServletContextListener
ServletContext
ServletContext
编辑:在 ServletContextListener 中,当您创建作业时,您可以通过将列表添加到 JobDataMap 中来将列表传递到作业中。
public class MyServletContextListener implements ServletContextListener{
public void contextInitialized(ServletContextEvent event){
ArrayList list = new ArrayList();
//add to ServletContext
event.getServletContext().setAttribute("list", list);
JobDataMap map = new JobDataMap();
map.put("list", list);
JobDetail job = new JobDetail(..., MyJob.class);
job.setJobDataMap(map);
//execute job
}
public void contextDestroyed(ServletContextEvent event){}
}
//Quartz job
public class MyJob implements Job{
public void execute(JobExecutionContext context){
ArrayList list = (ArrayList)context.getMergedJobDataMap().get("list");
//...
}
}