I'd like to use quartz for a project, but it seems to be missing some functionality. What I need is very simple, to fire another job after one finishes execution. This ideally would be done with a stored trigger, but quartz only considers time-based triggers, and no other kind. I'm looking for some kind of a job dependency system for quartz.
In reality there should be lots of triggers, such as other jobs finishing, files being modified, database tables being updated, etc.
Here's a job that waits for 5 seconds:
public class JobA implements Job, JobListener {
public static final String LISTENER_NAME = "JobA";
public void execute(JobExecutionContext arg0) throws JobExecutionException {
System.out.println("Job A, Waiting for 5 seconds");
try {
Thread.sleep(5L * 1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Done waiting");
}
public String getName() {
return LISTENER_NAME;
}
public void jobExecutionVetoed(JobExecutionContext context) {
//TODO
}
public void jobToBeExecuted(JobExecutionContext context) {
String jobName = context.getJobDetail().getKey().toString();
System.out.println("Job : " + jobName + " is going to start...");
}
public void jobWasExecuted(JobExecutionContext context,
JobExecutionException jobException) {
// System.out.println("jobWasExecuted");
String jobName = context.getJobDetail().getKey().toString();
System.out.println("Job : " + jobName + " is finished...");
if (!jobException.getMessage().equals("")) {
System.out.println("Exception thrown by: " + jobName
+ " Exception: " + jobException.getMessage());
}
JobKey jobKey = new JobKey("JobA", "group1");
try {
context.getScheduler().triggerJob(jobKey);
} catch (SchedulerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
The context.getScheduler().triggerJob(jobKey) line simply doesn't work. I've tried adding full jobs, recursive jobs, and can't seem to get anything to work. How can I get another job to fire while using this listener?
Related
I am new to working with ExecutorService, Future, and Runnable in java to set up timeouts on threads. I am working on a program where my main thread will call another thread to parse an XML file and (for security purposes) time out after a certain amount of time. I have been googling for hours and read many StackOverFlow threads and I just cannot seem to get the main thread to interrupt the secondary thread at all. When I run this program, the xml parser will go on forever parsing ridiculously large files, and I cannot seem to get it to be interrupted. Any help would be greatly appreciated. My code for both threads is below.
public class xmlParser{
private static class Parse implements Runnable {
private final String xmlFile;
public Parse(String xmlFile) {
this.xmlFile = xmlFile;
}
#Override
public void run() {
try {
while (!Thread.interrupted()) {
XMLReader xmlReader = XMLReaderFactory.createXMLReader();
xmlReader.setContentHandler(new MyContentHandler());
xmlReader.parse(new InputSource(xmlFile));
}
}
catch (Exception e) {
System.err.println("TIMEOUT ERROR: Took too long to parse xml file.");
e.printStackTrace();
}
}
}
public static void main(String[] args) throws InterruptedException {
ExecutorService executor = Executors.newSingleThreadExecutor();
Future future = executor.submit(new Parse(args[0]));
try {
future.get(1, TimeUnit.SECONDS);
}
catch (Exception e) {
future.cancel(true);
}
finally {
executor.shutdownNow();
}
}
}
Note: I am aware of the multiple types of exceptions that future.get(long timeout, TimeUnit unit) will throw and will handle that later. Currently, I simply want my main thread to interrupt the Parse thread after 1 second of running.
I tried to reproduce with a simpler job:
static class FiveSecJob implements Callable<String> {
#Override
public String call() {
long t0 = System.currentTimeMillis();
try {
Thread.sleep(5000);
return "success";
} catch (Exception e) {
System.out.println("interrupted after " + (System.currentTimeMillis() - t0) / 1000d + "s: " + e);
return e.getMessage();
}
}
}
#Test
public void testTimeout() {
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<String> future = executor.submit(new FiveSecJob());
String s = "initial value";
try {
s = future.get(1, TimeUnit.SECONDS);
} catch (Exception e) {
System.out.println("cancelling future (" + e + ")");
future.cancel(true);
} finally {
executor.shutdownNow();
}
System.out.println("s: " + s);
}
It seems to cancel the job like intendend. The output is:
cancelling future (java.util.concurrent.TimeoutException)
s: initial value
interrupted after 1.0s: java.lang.InterruptedException: sleep interrupted
I need to perform some action 50 million items. I have written below code
AtomicInteger failCounter = new AtomicInteger(0);
long start = System.currentTimeMillis();
ExecutorService es = Executors.newFixedThreadPool(30);
List<String> allids = getItems();//50 million items from db
log.info(getAction() + " Total items found: " + allids.size());
allids.stream().forEach(s -> {
es.execute(new MyRunnable(s, failCounter));
});
es.shutdownNow();
try {
if (!es.awaitTermination(100, TimeUnit.SECONDS)) {
System.out.println("Still waiting...");
System.exit(0);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Exiting normally...");
log.info("counter: " + failCounter.get());
public class MyRunnable implements Runnable {
private final String id;
private final AtomicInteger failCounter;
RollupRunnable(String id, AtomicInteger failCounter) {
this.id = id;
this.failCounter = failCounter;
}
#Override
public void run() {
try {
//perform some action
} catch (Exception exception) {
failCounter.getAndIncrement();
log.error(
"Error in calling " + getAction() + " for id: " + id + " of :" + this.getClass()
.getSimpleName(),
exception);
}
}
}
But executor exists after processing first 30 items.
Am I doing something wrong.
Instead of es.shutdownNow(); use es.shutdown();
shutDownNow() halts the processing of all the tasks including the ones that are not even executed.
That's the reason why not all of the items are executed by the Executor framework.
I wrote this method to remove a job from Quartz JDBC
public boolean removeJob(String jobName) {
try {
JobKey jobKey = JobKey.jobKey(jobName);
try {
Scheduler sched = schedulerFactoryBean.getScheduler();
logger.info("RESULT: " + sched.deleteJob(jobKey));
} catch (Exception e) {
throw new RuntimeException(e);
}
return true;
} catch (Exception ex) {
logger.error(ex.getMessage());
return false;
}
}
deleteJob is always returning false. So the job is not getting removed from the JDBC tables in mysql. What am I doing wrong. I only want to completely remove this job from the scheduler
Have you defined a job group during job creation? Then you may need to call jobKey(jobName, group). You can also check if job exists with scheduler.checkExists(jobKey) method which will be good for debugging.
JobKey jobKey = jobKey(jobName, group);
if (scheduler.checkExists(jobKey)) {
logger.info("job found with key: {}", jobKey);
scheduler.deleteJob(jobKey);
}
Hi this is my Cron Scheduler
public class CronListener implements ServletContextListener {
Scheduler scheduler = null;
#Override
public void contextInitialized(ServletContextEvent servletContext) {
System.out.println("Context Initialized");
try {
// Setup the Job class and the Job group
JobDetail job = newJob(CronJob.class).withIdentity("CronQuartzJob",
"Webapp").build();
// This is what I've tried as well
/*
* JobDataMap jdm = new JobDataMap(); jdm.put("targetDAO",
* targetDAO);
*/
// Create a Trigger that fires every X minutes.
Trigger trigger = newTrigger()
.withIdentity("CronQuartzJob", "Sauver")
.withSchedule(
CronScheduleBuilder.cronSchedule
("0 0/1 * 1/1 * ? *")).build();
// Setup the Job and Trigger with Scheduler & schedule jobs
scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(job, trigger);
} catch (SchedulerException e) {
e.printStackTrace();
}
}
#Override
public void contextDestroyed(ServletContextEvent servletContext) {
System.out.println("Context Destroyed");
try {
scheduler.shutdown();
} catch (SchedulerException e) {
e.printStackTrace();
}
}
}
And here's the Cron Job itself
public class CronJob implements org.quartz.Job {
static Logger log = Logger.getLogger(CronJob.class.getName());
#Autowired
TargetDAO targetDAO;
#Override
public void execute(JobExecutionContext context)
throws JobExecutionException {
try {
targetDAO.getAllTargets();
} catch (Exception e1) {
e1.printStackTrace();
}
log.info("webapp-rest cron job started");
try {
Utils.getProcessed();
} catch (Exception e) {
e.printStackTrace();
}
}
What I'm trying to do is getting a DAO class to call some data into it and call a function through it, every few hours.
But when I call data through the DAO, it always returns empty.
What I've found is that I must map the DAO somehow, I've seen in xml-based cron jobs, but I am unable to map it in this one.
This not exactly an answer, but a workaround,
What I did was made a new class
#EnableScheduling
#Component
public class SpringCronJob {
private static Logger log = Logger.getLogger(SpringCronJob.class.getName());
#Autowired
TargetDAO targetDAO;
#Scheduled(fixedRate = 15000)
public void getPostedTargets() {
try {
log.info(targetDAO.getAllTargets());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
It doesn't need anything else, no scheduler, you just need to add it to your component scan
This is what lead me to it
http://howtodoinjava.com/spring/spring-core/4-ways-to-schedule-tasks-in-spring-3-scheduled-example/
I am adding a job to quartz scheduler. After that I call the attached debug print jobs function. It does not list the job. Is the function call to getCurrentlyExecutingJobs() maybe limited to return max. of 10 jobs?
public void scheduleManagementEmail(ManagementEmailConfig managementEmailConfig, Scheduler scheduler) throws SchedulerException
{
logger.debug("Scheduling Management Email " +
managementEmailConfig.getManagementEmailConfigId());
String jobKey = "SendManagementEmailJob_" +
managementEmailConfig.getManagementEmailConfigId();
Class<? extends Job> jobClass = SendManagementEmailJob.class;
JobDetail job = JobBuilder.newJob(jobClass).withIdentity(new JobKey(jobKey)).build();
Trigger trigger = sendManagementEmailJob.getTriggerWithSchedule(managementEmailConfig);
trigger.getJobDataMap().put("managementEmailConfigId", managementEmailConfig.getManagementEmailConfigId());
if (!scheduler.checkExists(new JobKey(jobKey)))
{
scheduler.scheduleJob(job, trigger);
}
debugPrintJobs();
}
public void debugPrintJobs() {
try {
logger.debug("Quartz Jobs");
Scheduler s_scheduler = this.getJobScheduler();
List<JobExecutionContext> currentJobs = s_scheduler.getCurrentlyExecutingJobs();
for (JobExecutionContext jobCtx : currentJobs) {
JobKey jobKey = jobCtx.getJobDetail().getKey();
JobDetail jobDetail = s_scheduler.getJobDetail(jobKey);
List<? extends Trigger> triggers = s_scheduler.getTriggersOfJob(jobKey);
Date nextFireTime = null;
if (triggers.size() > 0)
{
nextFireTime = triggers.get(0).getNextFireTime();
}
logger.debug("Name= "+ jobKey.getName() + " Group=" + jobKey.getGroup() + " NextFireTime=" + nextFireTime);
}
} catch (Exception e) {
logger.debug("debugPrintJobs:" + e.getMessage());
}
The method getCurrentlyExecutingJobs() will return only the jobs that are running, not every scheduled job.
To get every scheduled job, you should do something like this:
Scheduler scheduler = getScheduler();
try {
// All scheduled jobs
for (String groupName : scheduler.getJobGroupNames()) {
for (JobKey jobKey : scheduler.getJobKeys(GroupMatcher.jobGroupEquals(groupName))) {
JobDetail jobDetail = scheduler.getJobDetail(jobKey);
final List<? extends Trigger> triggersOfJob = scheduler.getTriggersOfJob(jobKey);
// Do something with the info you just got
// ...
}
}
} catch (SchedulerException e) {
log.error("Retrieving jobs", e);
}