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

136 lines
3.7 KiB
Java
Raw Normal View History

2017-12-28 04:21:54 +01:00
/**
* Copyright (C) 2017-2018 Christian Pierre MOMON <cmomon@april.org>
2017-12-28 04:21:54 +01:00
* Copyright (C) 2011-2013,2017 Nicolas Vinot <aeris@imirhil.fr>
*
* 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;
2018-09-21 22:16:09 +02:00
import java.text.ParseException;
2018-09-21 18:34:42 +02:00
2018-09-20 18:25:29 +02:00
import org.april.hebdobot.bot.Hebdobot;
2018-09-21 22:16:09 +02:00
import org.quartz.JobDataMap;
2017-12-28 04:21:54 +01:00
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
2018-09-21 18:34:42 +02:00
import org.quartz.impl.JobDetailImpl;
2017-12-28 04:21:54 +01:00
import org.quartz.impl.StdSchedulerFactory;
2018-09-21 22:16:09 +02:00
import org.quartz.impl.triggers.CronTriggerImpl;
2017-12-28 04:21:54 +01:00
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
2018-01-04 23:28:30 +01:00
* @param crons
* the crons
2017-12-28 04:21:54 +01:00
* @throws SchedulerException
2018-01-04 23:28:30 +01:00
* the scheduler exception
2018-09-21 22:16:09 +02:00
* @throws ParseException
2019-05-03 12:38:26 +02:00
* the parse exception
2017-12-28 04:21:54 +01:00
*/
2018-09-21 22:16:09 +02:00
public CronManager(final Hebdobot bot, final CronSettings crons) throws SchedulerException, ParseException
2017-12-28 04:21:54 +01:00
{
this.bot = bot;
this.crons = crons;
this.scheduler = new StdSchedulerFactory().getScheduler();
for (CronValue cron : crons)
{
addCron(cron);
}
2018-09-21 22:16:09 +02:00
this.scheduler.start();
2017-12-28 04:21:54 +01:00
}
/**
* Adds the cron.
*
2018-01-04 23:28:30 +01:00
* @param cron
* the cron
2017-12-28 04:21:54 +01:00
* @throws SchedulerException
2018-01-04 23:28:30 +01:00
* the scheduler exception
2018-09-21 22:16:09 +02:00
* @throws ParseException
2019-05-03 12:38:26 +02:00
* the parse exception
2017-12-28 04:21:54 +01:00
*/
2018-09-21 22:16:09 +02:00
public void addCron(final CronValue cron) throws SchedulerException, ParseException
2017-12-28 04:21:54 +01:00
{
2018-09-21 18:34:42 +02:00
{
2018-09-21 22:16:09 +02:00
final JobDetailImpl job = new JobDetailImpl();
job.setName(cron.getName());
job.setDescription(cron.getDescription());
job.setJobClass(NotifyJob.class);
2018-09-21 18:34:42 +02:00
2018-09-21 22:16:09 +02:00
JobDataMap data = new JobDataMap();
data.put("bot", this.bot);
data.put("cron", cron);
job.setJobDataMap(data);
2018-09-21 18:34:42 +02:00
2018-09-21 22:16:09 +02:00
CronTriggerImpl trigger = new CronTriggerImpl();
trigger.setName(cron.getName());
trigger.setDescription(cron.getDescription());
trigger.setCronExpression(cron.getCron());
2017-12-28 04:21:54 +01:00
2018-09-21 18:34:42 +02:00
this.scheduler.scheduleJob(job, trigger);
}
2018-01-03 02:51:35 +01:00
logger.info("Added cron value: " + cron.getName());
}
/**
* Gets the crons.
*
* @return the crons
*/
public CronSettings getCrons()
{
return this.crons;
2017-12-28 04:21:54 +01:00
}
/**
* 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();
}
}