/** * Copyright (C) 2011-2013,2017 Nicolas Vinot * Copyright (C) 2017 Christian Pierre MOMON * * This file is part of (April) Hebdobot. * * Hebdobot is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Hebdobot is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with Hebdobot. If not, see */ package org.april.hebdobot.cron; import java.util.Date; import org.april.hebdobot.bot.Hebdobot; import org.quartz.CronScheduleBuilder; import org.quartz.JobBuilder; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.SimpleTrigger; import org.quartz.Trigger; import org.quartz.TriggerBuilder; import org.quartz.TriggerKey; import org.quartz.TriggerListener; import org.quartz.impl.JobDetailImpl; import org.quartz.impl.StdSchedulerFactory; import org.quartz.impl.matchers.KeyMatcher; import org.quartz.impl.triggers.SimpleTriggerImpl; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * The Class CronManager. */ public class CronManager { private static final Logger logger = LoggerFactory.getLogger(CronManager.class); private CronSettings crons; private Scheduler scheduler; private Hebdobot bot; /** * Instantiates a new cron manager. * * @param bot * the bot * @param crons * the crons * @throws SchedulerException * the scheduler exception */ public CronManager(final Hebdobot bot, final CronSettings crons) throws SchedulerException { this.bot = bot; this.crons = crons; this.scheduler = new StdSchedulerFactory().getScheduler(); for (CronValue cron : crons) { addCron(cron); } } /** * Adds the cron. * * @param cron * the cron * @throws SchedulerException * the scheduler exception */ public void addCron(final CronValue cron) throws SchedulerException { { final JobDetailImpl jobDetail = new JobDetailImpl(); jobDetail.setName("Mon job"); jobDetail.setJobClass(CronFooJob.class); JobDetail job = JobBuilder.newJob(CronFooJob.class).withIdentity(cron.getName()).build(); { final SimpleTriggerImpl trigger = new SimpleTriggerImpl(); trigger.setName("Trigger execution toutes les 5 secondes"); trigger.setDescription("Foo"); trigger.setStartTime(new Date(System.currentTimeMillis() + 1000)); trigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY); trigger.setRepeatInterval(5000); } { TriggerBuilder builder = TriggerBuilder.newTrigger(); builder.withDescription(cron.getName()); builder.withIdentity(cron.getName()); builder.withSchedule(CronScheduleBuilder.cronSchedule(cron.getCron())); Trigger trigger = builder.build(); } } { Trigger trigger = TriggerBuilder.newTrigger().withDescription(cron.getName()).withIdentity(cron.getName()) .withSchedule(CronScheduleBuilder.cronSchedule(cron.getCron())).build(); JobDetail job = JobBuilder.newJob(CronFooJob.class).withIdentity(cron.getName()).build(); this.scheduler.scheduleJob(job, trigger); TriggerListener listener = new CronListener(this.bot, cron); this.scheduler.getListenerManager().addTriggerListener(listener, KeyMatcher.keyEquals(new TriggerKey(cron.getName()))); } logger.info("Added cron value: " + cron.getName()); } /** * Gets the crons. * * @return the crons */ public CronSettings getCrons() { return this.crons; } /** * Shutdown. * * @throws SchedulerException * the scheduler exception */ public void shutdown() throws SchedulerException { this.scheduler.shutdown(); } /** * Start. * * @throws SchedulerException * the scheduler exception */ public void start() throws SchedulerException { this.scheduler.start(); } }