0

I am using Quartz scheduler in java web application.
My question is an extension to How to pass instance variables into Quartz job

I followed the Accepted answer in the above post & could pass instance variable to my quartz job. But after restarting the web server, the passed instance variable is not retained (i.e., it is null)

public class SimpleJob implements Job {
    @Override
    public void execute(JobExecutionContext context)
            throws JobExecutionException {
        SchedulerContext schedulerContext = null;
        try {
            schedulerContext = context.getScheduler().getContext();
        } catch (SchedulerException e1) {
            e1.printStackTrace();
        }
        ExternalInstance externalInstance =
            (ExternalInstance) schedulerContext.get("ExternalInstance");

        System.out.println(externalInstance); //Will print "null" after web server restart

        float avg = externalInstance.calculateAvg();
    }
}

How can I retain the "ExternalInstance" even after web server restart?

Thank you very much in advance.

Community
  • 1
  • 1
Chakra
  • 31
  • 2
  • 11

1 Answers1

0

You can add a ServletContextListener to your web project, and on contextInitialized(), you can put the externalInstance in the SchedulerContext. That way, it will be loaded every time the server restarts.

Pradeep Pati
  • 5,779
  • 3
  • 29
  • 43