diff --git a/src/org/april/hebdobot/bot/Hebdobot.java b/src/org/april/hebdobot/bot/Hebdobot.java index 09f03c1..51e9ba3 100644 --- a/src/org/april/hebdobot/bot/Hebdobot.java +++ b/src/org/april/hebdobot/bot/Hebdobot.java @@ -21,6 +21,7 @@ package org.april.hebdobot.bot; import java.io.File; import java.io.IOException; +import java.text.ParseException; import java.time.LocalTime; import org.apache.commons.lang3.StringUtils; @@ -402,6 +403,10 @@ public class Hebdobot extends PircBot { throw new HebdobotException("Error in cron settings.", exception); } + catch (ParseException exception) + { + throw new HebdobotException(exception); + } } /** diff --git a/src/org/april/hebdobot/cli/HebdobotConfigFile.java b/src/org/april/hebdobot/cli/HebdobotConfigFile.java index c7df8ab..40d78da 100644 --- a/src/org/april/hebdobot/cli/HebdobotConfigFile.java +++ b/src/org/april/hebdobot/cli/HebdobotConfigFile.java @@ -90,6 +90,7 @@ public class HebdobotConfigFile extends Properties for (String notifyName : getNotifyNames()) { + String notifyDescription = getNotifyCron(notifyName); String notifyCron = getNotifyCron(notifyName); String notifyIrcMessage = getNotifyIrcMessage(notifyName); String notifyTwitterMessage = getNotifyTwitterMessage(notifyName); @@ -105,7 +106,7 @@ public class HebdobotConfigFile extends Properties notifyImageFile = new File(notifyImageFileName); } - CronValue cronValue = new CronValue(notifyName, notifyCron, notifyIrcMessage, notifyTwitterMessage, notifyImageFile); + CronValue cronValue = new CronValue(notifyName, notifyDescription, notifyCron, notifyIrcMessage, notifyTwitterMessage, notifyImageFile); result.add(cronValue); } diff --git a/src/org/april/hebdobot/cron/CronListener.java b/src/org/april/hebdobot/cron/CronListener.java deleted file mode 100644 index 1fb2986..0000000 --- a/src/org/april/hebdobot/cron/CronListener.java +++ /dev/null @@ -1,126 +0,0 @@ -/** - * 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 org.apache.commons.lang3.StringUtils; -import org.april.hebdobot.bot.Hebdobot; -import org.quartz.JobExecutionContext; -import org.quartz.Trigger; -import org.quartz.Trigger.CompletedExecutionInstruction; -import org.quartz.TriggerListener; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * The Class Job. - */ -public class CronListener implements TriggerListener -{ - private static final Logger logger = LoggerFactory.getLogger(CronListener.class); - - private CronValue cron; - private Hebdobot bot; - - /** - * Instantiates a new cron listener. - * - * @param bot - * the bot - * @param cron - * the cron - */ - public CronListener(final Hebdobot bot, final CronValue cron) - { - if (cron == null) - { - throw new IllegalArgumentException("Null parameter cron."); - } - else if (bot == null) - { - throw new IllegalArgumentException("Null parameter bot."); - } - else - { - this.bot = bot; - this.cron = cron; - } - } - - /* (non-Javadoc) - * @see org.quartz.TriggerListener#getName() - */ - @Override - public String getName() - { - return this.cron.getName(); - } - - /* (non-Javadoc) - * @see org.quartz.TriggerListener#triggerComplete(org.quartz.Trigger, org.quartz.JobExecutionContext, org.quartz.Trigger.CompletedExecutionInstruction) - */ - @Override - public void triggerComplete(final Trigger trigger, final JobExecutionContext context, final CompletedExecutionInstruction triggerInstructionCode) - { - } - - /* (non-Javadoc) - * @see org.quartz.TriggerListener#triggerFired(org.quartz.Trigger, org.quartz.JobExecutionContext) - */ - @Override - public void triggerFired(final Trigger trigger, final JobExecutionContext context) - { - logger.info("CRON FIRE " + context.getTrigger().getDescription()); - - if (StringUtils.isBlank(this.cron.getIrcMessage())) - { - logger.info("No IRC message so no notify."); - } - else - { - this.bot.notifyIrc(this.cron.getIrcMessage()); - } - - if (StringUtils.isBlank(this.cron.getTwitterMessage())) - { - logger.info("No Twitter message so no notify."); - } - else - { - this.bot.notifyTwitter(this.cron.getTwitterMessage(), this.cron.getImageFile()); - } - } - - /* (non-Javadoc) - * @see org.quartz.TriggerListener#triggerMisfired(org.quartz.Trigger) - */ - @Override - public void triggerMisfired(final Trigger trigger) - { - } - - /* (non-Javadoc) - * @see org.quartz.TriggerListener#vetoJobExecution(org.quartz.Trigger, org.quartz.JobExecutionContext) - */ - @Override - public boolean vetoJobExecution(final Trigger trigger, final JobExecutionContext context) - { - return false; - } -} diff --git a/src/org/april/hebdobot/cron/CronManager.java b/src/org/april/hebdobot/cron/CronManager.java index b9dcfc6..711aa35 100644 --- a/src/org/april/hebdobot/cron/CronManager.java +++ b/src/org/april/hebdobot/cron/CronManager.java @@ -19,23 +19,15 @@ */ package org.april.hebdobot.cron; -import java.util.Date; +import java.text.ParseException; import org.april.hebdobot.bot.Hebdobot; -import org.quartz.CronScheduleBuilder; -import org.quartz.JobBuilder; -import org.quartz.JobDetail; +import org.quartz.JobDataMap; 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.quartz.impl.triggers.CronTriggerImpl; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -59,8 +51,9 @@ public class CronManager * the crons * @throws SchedulerException * the scheduler exception + * @throws ParseException */ - public CronManager(final Hebdobot bot, final CronSettings crons) throws SchedulerException + public CronManager(final Hebdobot bot, final CronSettings crons) throws SchedulerException, ParseException { this.bot = bot; this.crons = crons; @@ -70,6 +63,7 @@ public class CronManager { addCron(cron); } + this.scheduler.start(); } /** @@ -79,41 +73,27 @@ public class CronManager * the cron * @throws SchedulerException * the scheduler exception + * @throws ParseException */ - public void addCron(final CronValue cron) throws SchedulerException + public void addCron(final CronValue cron) throws SchedulerException, ParseException { { - final JobDetailImpl jobDetail = new JobDetailImpl(); - jobDetail.setName("Mon job"); - jobDetail.setJobClass(CronFooJob.class); + final JobDetailImpl job = new JobDetailImpl(); + job.setName(cron.getName()); + job.setDescription(cron.getDescription()); + job.setJobClass(NotifyJob.class); - JobDetail job = JobBuilder.newJob(CronFooJob.class).withIdentity(cron.getName()).build(); + JobDataMap data = new JobDataMap(); + data.put("bot", this.bot); + data.put("cron", cron); + job.setJobDataMap(data); - { - 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(); + CronTriggerImpl trigger = new CronTriggerImpl(); + trigger.setName(cron.getName()); + trigger.setDescription(cron.getDescription()); + trigger.setCronExpression(cron.getCron()); 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()); diff --git a/src/org/april/hebdobot/cron/CronValue.java b/src/org/april/hebdobot/cron/CronValue.java index 91f67a5..412f365 100644 --- a/src/org/april/hebdobot/cron/CronValue.java +++ b/src/org/april/hebdobot/cron/CronValue.java @@ -26,6 +26,7 @@ import java.io.File; public class CronValue { private String name; + private String description; private String cron; private String ircMessage; private String twitterMessage; @@ -45,9 +46,11 @@ public class CronValue * @param imageFile * the image file */ - public CronValue(final String name, final String cron, final String ircMessage, final String twitterMessage, final File imageFile) + public CronValue(final String name, final String description, final String cron, final String ircMessage, final String twitterMessage, + final File imageFile) { this.name = name; + this.description = description; this.cron = cron; this.ircMessage = ircMessage; this.twitterMessage = twitterMessage; @@ -59,6 +62,11 @@ public class CronValue return this.cron; } + public String getDescription() + { + return this.description; + } + public File getImageFile() { return this.imageFile; @@ -84,6 +92,11 @@ public class CronValue this.cron = value; } + public void setDescription(final String description) + { + this.description = description; + } + public void setImageFile(final File imageFile) { this.imageFile = imageFile; diff --git a/src/org/april/hebdobot/cron/CronFooJob.java b/src/org/april/hebdobot/cron/NotifyJob.java similarity index 59% rename from src/org/april/hebdobot/cron/CronFooJob.java rename to src/org/april/hebdobot/cron/NotifyJob.java index 3dc0a6e..956e8b5 100644 --- a/src/org/april/hebdobot/cron/CronFooJob.java +++ b/src/org/april/hebdobot/cron/NotifyJob.java @@ -19,18 +19,21 @@ */ package org.april.hebdobot.cron; +import org.apache.commons.lang3.StringUtils; +import org.april.hebdobot.bot.Hebdobot; import org.quartz.Job; +import org.quartz.JobDataMap; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * The Class Job. + * The Class NotifyCronJob. */ -public class CronFooJob implements Job +public class NotifyJob implements Job { - private static final Logger logger = LoggerFactory.getLogger(CronFooJob.class); + private static final Logger logger = LoggerFactory.getLogger(NotifyJob.class); /* (non-Javadoc) * @see org.quartz.Job#execute(org.quartz.JobExecutionContext) @@ -38,6 +41,28 @@ public class CronFooJob implements Job @Override public void execute(final JobExecutionContext context) throws JobExecutionException { - logger.debug("CRON JOB"); + logger.info("CRON FIRE " + context.getTrigger().getDescription()); + + JobDataMap map = context.getJobDetail().getJobDataMap(); + Hebdobot bot = (Hebdobot) map.get("bot"); + CronValue cron = (CronValue) map.get("cron"); + + if (StringUtils.isBlank(cron.getIrcMessage())) + { + logger.info("No IRC message so no notify."); + } + else + { + bot.notifyIrc(cron.getIrcMessage()); + } + + if (StringUtils.isBlank(cron.getTwitterMessage())) + { + logger.info("No Twitter message so no notify."); + } + else + { + bot.notifyTwitter(cron.getTwitterMessage(), cron.getImageFile()); + } } }