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.dispatcher.infrastructure; import airport.database.dispatcher.airplane.FlightDao; import airport.database.dispatcher.airplane.Flight; import airport.database.dispatcher.airplane.Runaway; import airport.database.dispatcher.airplane.RunawayDao; import java.util.GregorianCalendar; import java.util.List; import javax.annotation.PostConstruct; import org.apache.log4j.Logger; import org.quartz.Job; import static org.quartz.JobBuilder.newJob; import org.quartz.JobDataMap; import org.quartz.JobDetail; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; 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 AirportRunawaysImpl implements AirportRunaways { private final static Logger LOG = Logger.getLogger(AirportRunawaysImpl.class); private final static String PARAMETER_RUNAWAY_DAO = "param_runaway_dao"; private final static String PARAMETER_RUNAWAY_ID = "param_runaway_id"; private final static String SCHEDULLER_JOBDET_NAME = "name liberate runaway"; private final static String SCHEDULLER_TRIGGER_NAME = "name liberate runaway"; private final static String SCHEDULLER_GROUP_NAME = "group name liberate runaway"; private static int schedduler_name_count = 0; private static final int DELAY_TIMER_LEBIRATE = 5; @Autowired private FlightDao flightDao; @Autowired private RunawayDao runawayDao; @Autowired private Scheduler scheduler; @PostConstruct private void liberateRunawayAll() { runawayDao.liberateAll(); } @Override public void addFlight(Flight flight) { flightDao.addFlight(flight); if (LOG.isInfoEnabled()) { LOG.info("add flight. Flight : " + flight); } } @Override public boolean removeFlight(Flight flight, int runawayId) { boolean checkAvailable = runawayDao.checkAvailablerRunaway(runawayId); if (checkAvailable) { if (LOG.isInfoEnabled()) { LOG.info("runaway is lebirate. Flight :" + flight + ". RunawayId : " + runawayId); } flightDao.removeFlight(flight); runawayDao.occupayRunaway(runawayId); setTaimer(runawayId); } return checkAvailable; } @Override public List<Runaway> getAllRunAways() { return runawayDao.getAvailableRunaway(); } private void setTaimer(int runawayId) { if (LOG.isInfoEnabled()) { LOG.info("create task"); } JobDataMap dataMap = new JobDataMap(); dataMap.put(PARAMETER_RUNAWAY_ID, runawayId); dataMap.put(PARAMETER_RUNAWAY_DAO, runawayDao); GregorianCalendar calendar = new GregorianCalendar(); calendar.add(GregorianCalendar.SECOND, DELAY_TIMER_LEBIRATE); JobDetail jobDetail = newJob(JobLebirateRunaway.class) .withIdentity(SCHEDULLER_JOBDET_NAME + schedduler_name_count, SCHEDULLER_GROUP_NAME) .setJobData(dataMap).build(); Trigger trigger = newTrigger() .withIdentity(SCHEDULLER_TRIGGER_NAME + schedduler_name_count, SCHEDULLER_GROUP_NAME) .startAt(calendar.getTime()).withSchedule(SimpleScheduleBuilder.simpleSchedule()).build(); if (schedduler_name_count == Integer.MAX_VALUE) { schedduler_name_count = 0; } else { schedduler_name_count++; } try { scheduler.scheduleJob(jobDetail, trigger); scheduler.start(); } catch (SchedulerException ex) { if (LOG.isInfoEnabled()) { LOG.info("error start taimer", ex); } } } public static class JobLebirateRunaway implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { RunawayDao runawayDao = (RunawayDao) context.getJobDetail().getJobDataMap().get(PARAMETER_RUNAWAY_DAO); int runawayId = (Integer) context.getJobDetail().getJobDataMap().get(PARAMETER_RUNAWAY_ID); runawayDao.liberateRunaway(runawayId); if (LOG.isInfoEnabled()) { LOG.info("task is succesful. Job lebirate runaway. RunawayId : " + runawayId); } } } }