package com.megatim.fdxconvert.service;
|
|
import com.megatim.fdxconvert.model.Tache;
|
import com.megatim.fdxconvert.service.util.SchedulerUtil;
|
import java.util.List;
|
import java.util.logging.Level;
|
import java.util.logging.Logger;
|
import org.quartz.JobBuilder;
|
import org.quartz.JobDetail;
|
import org.quartz.Scheduler;
|
import org.quartz.SchedulerException;
|
import org.quartz.impl.StdSchedulerFactory;
|
|
/**
|
*
|
* @author MGT_DEV3
|
*/
|
public class TacheJobService {
|
|
private TacheJobService() {
|
}
|
|
private static Scheduler scheduler;
|
|
private static TacheJobService tacheJobService;
|
|
public static TacheJobService getInstance() {
|
|
if (tacheJobService == null) {
|
|
try {
|
|
scheduler = StdSchedulerFactory.getDefaultScheduler();
|
tacheJobService = new TacheJobService();
|
|
} catch (SchedulerException ex) {
|
}
|
}
|
|
return tacheJobService;
|
}
|
|
public void startJobs() {
|
|
try {
|
|
scheduler.start();
|
|
List<Tache> taches = TacheService.getInstance().getAll();
|
|
taches.forEach(t -> {
|
|
if (t.isActive()) {
|
|
//Creation concrète du Job (un Job concrèt se matérialise via la classe JobDetail)
|
JobDetail job = JobBuilder
|
.newJob(TacheJob.class)
|
.withIdentity(t.getLibelle())
|
.usingJobData("tacheId", t.getLibelle()).build();
|
|
//Ajout du Job crée dans l'ordonnanceur
|
try {
|
scheduler.scheduleJob(job, SchedulerUtil.buildTriggerForTache(t));
|
} catch (SchedulerException ex) {
|
Logger.getLogger(TacheJobService.class.getName()).log(Level.SEVERE, null, ex);
|
}
|
|
}
|
|
});
|
|
} catch (Exception ex) {
|
Logger.getLogger(TacheJobService.class.getName()).log(Level.SEVERE, null, ex);
|
}
|
|
}
|
|
public void restartJob() {
|
|
try {
|
|
scheduler.clear();
|
this.startJobs();
|
|
} catch (SchedulerException ex) {
|
Logger.getLogger(TacheJobService.class.getName()).log(Level.SEVERE, null, ex);
|
}
|
|
}
|
|
}
|