Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package airport.services.dispatcher; import airport.database.dispatcher.airplane.FlyingMachineTypeDao; import java.util.GregorianCalendar; import javax.annotation.PostConstruct; import org.apache.log4j.Logger; import static org.quartz.JobBuilder.newJob; import org.quartz.JobDataMap; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.SimpleScheduleBuilder; import org.quartz.Trigger; import static org.quartz.TriggerBuilder.newTrigger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; /** * * @author mcdoker */ @Component public class GeneratorFlightImpl implements GeneratorFlight { private final static Logger LOG = Logger.getLogger(GeneratorFlightImpl.class); @Autowired private Scheduler scheduler; @Autowired private FlyingMachineTypeDao flyingMachineTypeDao; @Autowired private GeneratorFlightJMS jMS; private final static String SCHEDULE_GROUP_NAME = "group_add_airplane"; private final static String SCHEDULE_JOBDET_NAME = "jobdet_add_airplane"; private final static String SCHEDULE_TRIGGER_NAME = "trigger_add_airplane"; private final static int DELAY_TIMER_SEC = 20; private JobDetail jobDetailFlight; private Trigger triggerFlight; @PostConstruct private void postInit() { if (LOG.isInfoEnabled()) { LOG.info("create task"); } try { scheduler.start(); } catch (SchedulerException ex) { LOG.error("error start taimer", ex); } } private void createNewJobAndTrigger() { JobDataMap dataMap = new JobDataMap(); dataMap.put(PARAMETER_FMTDAO_NAME, flyingMachineTypeDao); dataMap.put(PARAMETER_GENFLIGHTJMS_NAME, jMS); GregorianCalendar calendar = new GregorianCalendar(); calendar.add(GregorianCalendar.SECOND, DELAY_TIMER_SEC); jobDetailFlight = newJob(JobAddFlight.class).withIdentity(SCHEDULE_JOBDET_NAME, SCHEDULE_GROUP_NAME) .setJobData(dataMap).build(); triggerFlight = newTrigger().withIdentity(SCHEDULE_TRIGGER_NAME, SCHEDULE_GROUP_NAME) .startAt(calendar.getTime()).withSchedule(SimpleScheduleBuilder.simpleSchedule() .withIntervalInSeconds(DELAY_TIMER_SEC).repeatForever()) .build(); } @Override public void start() { try { if (jobDetailFlight == null || !scheduler.checkExists(jobDetailFlight.getKey())) { createNewJobAndTrigger(); scheduler.scheduleJob(jobDetailFlight, triggerFlight); if (LOG.isInfoEnabled()) { LOG.info("task started"); } } } catch (SchedulerException ex) { LOG.error("error start taimer", ex); } } @Override public void stop() { try { if (scheduler.checkExists(jobDetailFlight.getKey())) { scheduler.deleteJob(jobDetailFlight.getKey()); if (LOG.isInfoEnabled()) { LOG.info("task stoped"); } } } catch (SchedulerException ex) { LOG.error("error stop taimer", ex); } } }