不,我們可以從Quartz作業訪問servlet上下文。
@Override
public void contextInitialized(ServletContextEvent sce) {
try {
//Create & start the scheduler.
StdSchedulerFactory factory = new StdSchedulerFactory();
factory.initialize(sce.getServletContext().getResourceAsStream("/WEB-INF/my_quartz.properties"));
scheduler = factory.getScheduler();
//pass the servlet context to the job
JobDataMap jobDataMap = new JobDataMap();
jobDataMap.put("servletContext", sce.getServletContext());
// define the job and tie it to our job's class
JobDetail job = newJob(ImageCheckJob.class).withIdentity("job1", "group1").usingJobData(jobDataMap).build();
// Trigger the job to run now, and then repeat every 3 seconds
Trigger trigger = newTrigger().withIdentity("trigger1", "group1").startNow()
.withSchedule(simpleSchedule().withIntervalInMilliseconds(3000L).repeatForever()).build();
// Tell quartz to schedule the job using our trigger
scheduler.scheduleJob(job, trigger);
// and start it off
scheduler.start();
} catch (SchedulerException ex) {
log.error(null, ex);
}
}
石英工作,我們得到CA servlet上下文如下。
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
ServletContext servletContext = (ServletContext) context.getMergedJobDataMap().get("servletContext");
//...
}
石英計劃的工作是一種方法,它採用參數文件路徑之一。 – SneakyMummin
當您的代碼安排作業時,是否將參數值傳遞給Quartz,在JobDetail.setJobDataMap()中? –
這樣做。謝謝 – SneakyMummin