2

In my webapp I am using Quartz to call a method defined in some class at some interval, that method as one of the arguments takes a path to a css file in my WebContent directory. My question is how can I obtain the path to that css file from a non-servlet class.

One thing that I did try was I made the class that calls the method extend the HttpServlet so that I can make a call to

String contextPath = getServletContext().getRealPath("");

but that did not work and my application just hangs on that line. I do not want to hardcode the path as this seem to be unprofessional :)

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
SneakyMummin
  • 683
  • 1
  • 11
  • 30

3 Answers3

3

You cannot access the servlet context from the Quartz job as the job is not invoked as a part of the request handling pipeline.

Why not just make the CSS file path an argument to the job, so that it can be passed by the servlet/web-code scheduling/invoking the Quartz job? See the Quartz documentation for an example.

matt b
  • 138,234
  • 66
  • 282
  • 345
1

If you put a file in the WEB-INF/classes directory of your web app, you can access it using getResourceAsStream(). This will work with a WAR file; getRealPath() will not.

Why does Quartz need to know about a .css file? That should be purely view.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • Quartz does not care about the .css file. Its the method called by quartz requires css path – SneakyMummin May 22 '12 at 16:18
  • Can't imagine why. Quartz schedules a task on the server side; .css should be about view. Still don't get it, but then I don't have to. Good luck. – duffymo May 22 '12 at 16:52
0

No, we can access servlet context from Quartz 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);
    }
}

In Quartz job we ca get servlet context as below.

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