I am writing a java/spring library to include in other projects that are using quartz. I need it to log something before each task is executed.
I have a simple JobListener that looks like this:
public class MyJobListener extends JobListenerSupport {
private static final Logger log = LogManager.getLogger(LoggingJobListener.class);
@Override
public String getName() {
return "MyJobListener";
}
@Override
public void jobToBeExecuted(JobExecutionContext context) {
log.info("job will start")
}
}
I know we can do something like this to add joblisteners:
scheduler.getListenerManager().addJobListener(myJobListener, allJobs());
But how can I get the scheduler to add so I can add the listener?
I have tried implementing the SchedulerFactoryBeanCustomizer
but can't figure out how to add the listener (since the scheduler is not created yet?)
You can add a Postconstruct and add JobListener there.
@Component
public class JobListenerConfig {
@Autowired
private SchedulerFactoryBean schedulerFactoryBean;
@PostConstruct
public void addListeners() throws SchedulerException {
schedulerFactoryBean.getScheduler()
.getListenerManager()
.addJobListener(new MyJobListener());
}
}