I am using quartz in my apache isis project for scheduling. I have a class MyJob which implements org.quartz.Job and it has method execute which is called when scheduler triggers at given time.
My problem is, I have a class DemoService
and it has a method showDemo()
which I want to call from the execute method.
But when the scheduler runs, it throws Null Pointer Exception at demoService.showDemo()
.
I have not been able to inject any service in that class. It always gives NPE. How can I inject a service into the MyJob
class?
Here is the code:-
public class MyJob implements Job {
@Inject
DemoService demoService;
public MyJob() {
}
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
demoService.showDemo();
}
}
The easiest approach is to put the logic you want to run in a subclass of AbstractIsisSessionTemplate, and then instantiate and execute it from your quartz job.
This technique is used by the Incode Platform's quartz job to run background commands, see here; the quartz module shows this from the quartz perspective (which I think you already have figured out).
HTH Dan