agirbot/src/org/april/hebdobot/cron/CronManager.java

123 lines
3.5 KiB
Java

/**
* Copyright (C) 2011-2013,2017 Nicolas Vinot <aeris@imirhil.fr>
* Copyright (C) 2017 Christian Pierre MOMON <cmomon@april.org>
*
* 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 <http://www.gnu.org/licenses/>
*/
package org.april.hebdobot.cron;
import org.april.hebdobot.model.Hebdobot;
import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.quartz.TriggerListener;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.matchers.KeyMatcher;
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
{
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();
}
}