public class TriggerNextJobListener implements JobExecutionListener, InitializingBean {
private static final Log logger = LogFactory.getLog(TriggerNextJobListener.class);
private String nextJobName;
private int delay = 10;
public void setDelay(int delay) {
this.delay = delay;
}
public void setNextJobName(String nextJobName) {
this.nextJobName = nextJobName;
}
@Override
public void beforeJob(JobExecution jobExecution) {
// Nothing
}
@Override
public void afterJob(JobExecution jobExecution) {
// Getting ApplicationContext
ApplicationContext context = SpringBatchComponentContainer.getCommonBatchBeansContext();
// Instance of bean SimpleScheduleRepsitory from ApplicationContext
SimpleScheduleRepository rep = context.getBean(SimpleScheduleRepository.class);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, delay);
try {
rep.rescheduleJob(nextJobName, cal.getTime());
} catch (SchedulerException e) {
logger.error("Erreur lors de la replanification du job " + nextJobName, e);
}
}
@Override
public void afterPropertiesSet() throws Exception {
Assert.notNull(nextJobName, "'nextJobName' property value is required");
}
I know that's not the best way to do this but maybe it can help other users who need this feature.
I think that it will be good if trooper can offer this feature (not necessarily my implementation), it's very userful.