在石英调度程序中检索 servletContext 引用 [英] Retrieve servletContext reference in quartz scheduler

查看:77
本文介绍了在石英调度程序中检索 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 引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆