在石英调度程序中检索 servletContext 引用

编程入门 行业动态 更新时间:2024-10-10 05:26:39
本文介绍了在石英调度程序中检索 servletContext 引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在基于 spring 3.0 的应用程序中使用 Quartz Scheduler.我成功地创建了新的调度程序,并且它们运行良好.

I am using Quartz Scheduler with my spring 3.0 based application. I am successfully able to create new schedulers and they are working fine.

我已经看到了 参考.

但是.. 我无法在我的石英作业文件中检索 servletContext.任何人都可以帮助我如何在 executeInternal() 方法中检索 servletContext 引用??

But.. I am not able to retrieve servletContext in my quartz job file. can anyone help me for How to retrieve servletContext reference in executeInternal() method ??

推荐答案

我也有类似的需求.我以与此处提供的解决方案类似的方式对其进行了整理.在我的 servlet 上下文侦听器中,我使用作业数据映射对象设置 servlet 上下文,然后为作业设置:

I had a similar need. I sorted it out in a similar fashion to the solution presented here. In my servlet context listener I am setting the servlet context using the job data map object which then is set for a job:

@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); } }

然后在我的工作中我这样做:

Then inside my job I am doing this:

@Override public void execute(JobExecutionContext context) throws JobExecutionException { ServletContext servletContext = (ServletContext) context.getMergedJobDataMap().get("servletContext"); //... }

另外因为你提到你正在使用 Spring 我找到了这个链接,在上一篇文章中,有人提到要实现 ServletContextAware.就我个人而言,我会选择 JobDataMap,因为这是它的作用.

Also since you mention that you are using Spring I found this link, where in the last post a guy mentions to implement ServletContextAware. Personally, I would go with the JobDataMap, since that is its role.

更多推荐

在石英调度程序中检索 servletContext 引用

本文发布于:2023-11-25 20:54:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631307.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:程序   石英   servletContext

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!