Quartz

Java Quartz Interview Questions

Quartz is an open source framework that specializes in scheduling, triggering, and managing job execution within a Java application. Below are some frequently asked interview questions on the topic of Java Quartz applications.

1. Introduction

The sample interview questions that follow cover some quartz topics that include scheduling, triggering, execution, threading, exceptions and persistence.

1.1 What are the three key components to consider when using Java quartz?

The scheduler, job and trigger. The scheduler coordinates the execution of the job and the trigger sets up the interval and frequency with which the job will run.

1.2. How do you begin a quartz process so that it will start executing jobs?

To begin a quartz process you must initiate all the components, scheduler, job and trigger, and then call the start method on the scheduler.

Trigger trigger = TriggerBuilder.newTrigger().withIdentity("jcgTriggerName", "group1")
  .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?")).build();
Scheduler scheduler;
scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
JobKey jobKey = new JobKey("jcgJobName", "group1");
JobDetail job = JobBuilder.newJob(MyJob.class).withIdentity(jobKey).build();
scheduler.scheduleJob(job, trigger);

1.3. How do I check the status of a running job?

The JobListener will allow you to check the status of a running job.

jcgScheduler.getListenerManager().addJobListener(jcgJobListener, KeyMatcher.jobKeyEquals(new JobKey("jcgJobName", "jcgJobGroup")));

1.4. How is the SimpleTrigger used?

A SimpleTrigger is used for a single execution or an execution that repeated a specific number of times over a given interval.

1.5. How do I customize thread management?

I set the thread pool class and corresponding thread pool properties that fall under org.quartz.threadPool. The default thread pool is org.quartz.simpl.SimpleThreadPool.

1.6. How is the CronTrigger used?

The CronTrigger is used to execute a job using a cron expression. A cron expression is a notation that represents a second, minute, hour, day, month, or year as well as the ways to express special characters like wildcards or ranges for the schedule.

1.7. How do you store job state?

The JobDataMap is used to store state that will be provided to the job when it executes.

1.8. How do you handle or prevent concurrent execution of jobs?

The DisallowConcurrentExecution annotation is used to prevent concurrent execution of that same instance of jobs. The instance definition is controlled by the identifier in JobDetail.

1.9. How do you update the JobDataMap between execution of jobs?

Use the PersistJobDataAfterExecution annotation to update the data in JobDataMap after the job executes successfully.

1.10. What types of exceptions are allowed from the job execute method?

The JobExecutionException is the only allowable exception from the job execute method

1.11. What happens when a scheduled job fails to trigger?

The misfire conditions is specific to each trigger. For the cron trigger, a misfire condition is specified in the job creation or will default to the smart policy. To customize the misfire condition specific to the cron trigger there are three options; withMisfireHandlingInstructionDoNothing, withMisfireHandlingInstructionFireAndProceed, withMisfireHandlingInstructionIgnoreMisfires.

You can also create a custom trigger by implementing the TriggerListener interface and define the triggerMisfired method.

public void triggerMisfired(Trigger trigger);

1.12. What are job stores?

Job stores control how data is provided to the scheduler, jobs, and triggers.

1.13. What are different types of Job Stores?

There are three different types of job stores provided by quartz; RAMJobStore, JDBCJobStore and TerracottaJobStore that provided persistent job data to quartz components.

1.14. How would you stop a running job?

You can stop a running job by calling interrupt on the scheduler and providing the job key. The job that you are interrupting must implement the InterruptableJob interface.

sched.interrupt(job.getKey());

1.15. What is the JobExecutionContext?

The JobExecutionContext is passed to the execute method of an invoked job by the scheduler and it contains a handle to the scheduler, a handle to the trigger and the JobDetail.

1.16. Name a few of the bundled Jobs that quartz provides?

A few of the jobs that quartz provides for users are FileScanJob, DirectoryScanJob, NativeJob, EJB3InvokerJob, SendQueueMessageJob, SendTopicMessageJob, JmxInvokerJob and SendMailJob. Documentation on these jobs exists in the JavaDoc for the quartz-jobs-*.jar.

2. Conclusion

You can find more information in the quartz Java docs or in the documentation on the quartz site, http://www.quartz-scheduler.org/.

Andy Beck

Andy is a Senior Software Engineer that has sixteen years of experience working on web, desktop and mobile applications. He holds a Bachelor and Master's degree in Computer Science from the University of Virginia and George Washington University respectively. He specializes in working with Java web services and has significant experience working web applications, databases and continuous integration and deployments. He is currently working as a technical lead at a financial technology organization where he supports mobile application services in Java.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
shaofeng
shaofeng
9 months ago

I just want to use “CronExpression#isSatisfiedBy” to do some time pattern checking. However i do not want to load whole quartz in my application service. Is there any way to config this?

Last edited 9 months ago by shaofeng
Back to top button