diff --git a/modules/quartz/src/main/java/org/jpos/q2/QuartzAdaptor.java b/modules/quartz/src/main/java/org/jpos/q2/QuartzAdaptor.java index 0042d5bf9..f6d6e5996 100644 --- a/modules/quartz/src/main/java/org/jpos/q2/QuartzAdaptor.java +++ b/modules/quartz/src/main/java/org/jpos/q2/QuartzAdaptor.java @@ -31,14 +31,14 @@ import org.quartz.*; import org.quartz.impl.StdSchedulerFactory; -import java.util.Date; -import java.util.Properties; -import java.util.UUID; +import java.time.LocalDate; +import java.util.*; @SuppressWarnings("unused") public class QuartzAdaptor extends QBeanSupport implements XmlConfigurable { protected Scheduler scheduler; private Element config; + private Map jobs = new HashMap<>(); @Override protected void initService() throws Exception { @@ -62,33 +62,39 @@ protected void initService() throws Exception { jobData.put("Q2", adaptor); JobDetail job = JobBuilder.newJob(j.getClass()) - .withIdentity(e.getAttributeValue("id"), getName()) + .withIdentity(jobId, getName()) .usingJobData(jobData) .build(); + jobs.put(jobId, job); + CronTrigger trigger; try { String cronExpression = Environment.get(e.getAttributeValue("when")); + if (cronExpression == null) + cronExpression = "0 0 0 31 12 ? " + String.valueOf(LocalDate.now().getYear()+100); trigger = TriggerBuilder.newTrigger() - .withIdentity(e.getAttributeValue("id"), getName()) - .withSchedule(CronScheduleBuilder.cronSchedule(cronExpression)) - .build(); + .withIdentity(jobId, getName()) + .withSchedule(CronScheduleBuilder.cronSchedule(cronExpression)) + .build(); Date ft = scheduler.scheduleJob(job, trigger); evt.addMessage(job.getKey() + " (" + obj.getClass() + ") has been scheduled to run at: " + ft - + " and repeat based on expression: " - + trigger.getCronExpression()); + + " and repeat based on expression: " + + trigger.getCronExpression()); } catch (Exception e1) { evt.addMessage(String.format("%s: %s --- Exception follows", jobId, e1.getMessage())); evt.addMessage(e1); } } - NameRegistrar.register(getName(), this); - Logger.log(evt); } + public Scheduler scheduler() { + return scheduler; + } + @Override protected void startService() throws SchedulerException { scheduler.start(); @@ -107,6 +113,10 @@ protected void destroyService() { public void setConfiguration(Element config) throws ConfigurationException { this.config = config; } + + public JobDetail getJob (String id) { + return jobs.get(id); + } public class Q2Adaptor implements LogSource, Configurable { Configuration cfg; Logger logger; diff --git a/modules/quartz/src/main/java/org/jpos/q2/cli/RUNJOB.java b/modules/quartz/src/main/java/org/jpos/q2/cli/RUNJOB.java new file mode 100644 index 000000000..5fdb09ecc --- /dev/null +++ b/modules/quartz/src/main/java/org/jpos/q2/cli/RUNJOB.java @@ -0,0 +1,26 @@ +package org.jpos.q2.cli; + +import org.jpos.q2.CLICommand; +import org.jpos.q2.CLIContext; +import org.jpos.q2.QuartzAdaptor; +import org.jpos.util.NameRegistrar; +import org.quartz.JobDetail; + +public class RUNJOB implements CLICommand { + @Override + public void exec(CLIContext cli, String[] args) throws Exception { + if (args.length != 3) { + cli.println ("Usage: RUNJOB "); + return; + } + try { + QuartzAdaptor adaptor = NameRegistrar.get(args[1]); + JobDetail job = adaptor.getJob(args[2]); + if (job != null) { + adaptor.scheduler().triggerJob(job.getKey()); + } + } catch (NameRegistrar.NotFoundException e) { + cli.println("QBean '" + args[1] + "' not found"); + } + } +}