Getting context path in class without servlet

In my webapp, I use Quartz to call a method defined in some class at some interval, this method as one of the arguments takes the path to the css file in my WebContent directory. My question is how can I get the path to this css file from a class without a servlet.

One thing I tried is that I made a class that calls an extension method HttpServletso that I can call

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

but this did not work, and my application just hangs on this line. I don’t want to show the path hard, as it seems unprofessional :)

+3
source share
3 answers

Quartz, .

CSS , /- / Quartz? . Quartz .

+3

WEB-INF/classes -, , getResourceAsStream(). WAR; getRealPath() .

.css? .

+1

, Quartz.

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

, .

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

All Articles