Skip to content

Commit

Permalink
Add ability to run Quartz jobs from CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
ar committed Nov 20, 2024
1 parent 6d5cf0f commit ef1dc0a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
32 changes: 21 additions & 11 deletions modules/quartz/src/main/java/org/jpos/q2/QuartzAdaptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String,JobDetail> jobs = new HashMap<>();

@Override
protected void initService() throws Exception {
Expand All @@ -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();
Expand All @@ -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;
Expand Down
26 changes: 26 additions & 0 deletions modules/quartz/src/main/java/org/jpos/q2/cli/RUNJOB.java
Original file line number Diff line number Diff line change
@@ -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 <cronname> <jobid>");
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");
}
}
}

0 comments on commit ef1dc0a

Please sign in to comment.