Java + Cron Job = Quartz

Quartz Enterprise Job SchedulerOne of my favorite, often least used, open source tools for Java/J2EE applications is the Cron Job scheduling tool Quartz. Anyone who’s ever administered Linux or a web server is probably familiar with creating and modifying cron jobs to run a process at a specific time of the day/week/month. For example, you may need a nightly clean job for a data directory, or you may need to generate reports automatically at the end of the week. What I like about Quartz is that it’s simple to use, works in both Java and J2EE server-based applications, and is easy to install.

Java with operating-system cron jobs
Despite the availability of Quartz and similar Java-based tools, some developers still choose to use the operating system crontab and set it up to call Java methods directly. Although this can work well in practice, it’s not a very stable solution. For example, if the Java home variable changes, the cron job could break. Also, it’s not portable, since each operating system has a slightly different scheduling tool. Most importantly, though, the application is more vulnerable to attack since it requires input from a process outside the JVM.

What is Quartz?
Quartz is an open source scheduling module written entirely in Java, which lives inside the JVM. It has complete support for creating jobs based on crontab-like syntax, such as using the string “0 4 * * * ?” to run a job every day at 4am. It also supports a more rigorous non-crontab syntax for schedules that can’t be specified in a single string. Anytime a developer needs to write a process that runs in the background, whether its run once a day or every 5 minutes, they should consider Quartz for their scheduling needs.

Creating a Job
Even though you probably only have one job you want to schedule, all Quartz applications start by creating a scheduler that can support any number of jobs using the following code:

SchedulerFactory schedulerFactory = new org.quartz.impl.StdSchedulerFactory();
Scheduler scheduler = schedulerFactory.getScheduler();
scheduler.start();

From there, we can create our 4am job schedule by defining the job, defining the schedule, and then tying the two together by adding them to our scheduler instance, as below:

JobDetail job = new JobDetail("myJob",MyClass.class);
CronTrigger schedule = new CronTrigger("mySchedule",Scheduler.DEFAULT_GROUP,"0 4 * * * ?");
scheduler.scheduleJob(job,schedule);

Finally, you create a job class, in this case MyClass, that implements the Job interface and has a method quite similar to a main method:

public class MyClass implements Job {
   public void execute(JobExecutionContext context) {
    ... // Perform job
   }
}

Keep in mind that this code to create the scheduler and job can be in any class. The only class-level restriction is that the job itself has to implement the Job interface. How and where the job is created is up to you.

J2EE: How to apply?
J2EE servers often run for long periods of time, therefore they are a natural fit for Quartz scheduling. For example, you can use Quartz to create reports out of large sets of data in the middle of the night when usage is low. There are literally dozens of ways to integrate Quartz with J2EE, but the two main ways I prefer to use are:

  • Job calls a session bean method
  • Job creates a message and sends it to a JMS queue

In both cases, the job itself is *never* more than a page of code. It just picks up what it was called for and executes a J2EE call. In this manner, you might have a bean called ReportBean with a method on the bean call generateNightlyReport(). The Quartz job would be a short segment of code that connects to the bean and executes the session bean command.

My favorite method, though, is to have a job create a message and send it to a JMS queue, since the Quartz process can return without waiting for the actual job to finish. Also, the job does not require a transaction or context since it’s going to a queue instead of executing a bean directly. As long as you have a messaging bean watching the queue, the job will get executed soon after the Quartz scheduler has finished processing the request.

Some tips
Hopefully this article has given you a taste for Quartz as a scheduling tool. While I am aware there are other scheduling tools in Java, Quartz has always worked right out of the box for me with very little effort, so to be honest I’ve never had a reason to try another. Here are some tips I recommend to write good Quartz applications:

  • Keep your job class under a page. If you find yourself writing a very large job class, extract the useful code into a separate class and have the job code call that class. In this manner, there’s very little code actually tied to your scheduler and you can reuse the class outside the context of scheduler.
  • If your schedule executes often or your jobs are quite long, there’s the distinct possibility a job could be started while the last job is running. For example, if a job runs every 2 minutes and the first job is taking 3 minutes, Quartz won’t block the second job from starting so you will have multiple instances of the same job running at once. While there are probably ways to prevent this within Quartz, one sanity check I like to enforce is a semaphore lock that prevents two threads from executing the same code at the same time. In the case a second job is started while the first is running, the second should just exit instead of waiting for the first to finish. In Java, you can do this atomically by setting an int to 0 or 1.
  • Quartz is often included in a number of J2EE server packages, so you may have it without the need to import the libraries. Keep in mind, though, that the existing version installed with the J2EE server may be older than the one you want to use. In that case, you may want to import your own Quartz jar into the application.

product review: infinitest

Want to run your JUnit tests as soon as you change the code?  That’s what infinitest strives to do.  The starting point is infinitetest.org which directs you to the corporate site.

What does it do?

Infinitest installs as a plugin to your IDE.  In Eclipse, it adds a bar to the bottom of your screen.  I’d say I lost about an inch of real estate.  Not much, but if you have a lot of other plugins, this could be a problem.

Every time you change Java code, it sees which unit tests should be re-run and changes the bar to the appropriate color:

  • black – waiting for changes – when open workspace and haven’t done anything yet
  • green – all is well
  • yellow – no tests were affected by this change
  • red – one or more tests are failing (could be from this change or a previous change)

The bar, also tells you how many “test cases” were run.  They mean the number of test classes rather than the actual number of tests so this number isn’t as useful as it sounds.

Does it require a lot of configuration?

No.  The entire user guide is two screens long.  Configuring it took me less than two minutes – which were spent looking up the name of my integration test runner and package so infinitest didn’t run them.   I also looked at the preferences and saw it had a default of half a second before giving up on a test.  That’s a good length.  No unit test should take that long.

What do I need to run/install it?

Infinitest runs as an Eclipse or IntelliJ plugin.  I tried it in Eclipse.  You install from an update site.  My only concern is they don’t provide a manual zip file style local update.  Now granted, neither do many other products, but they take the time to write:

If you are behind a firewall, you may have limited access to the Internet. This can cause problems accessing the update site. If you use a web proxy to connect to the Internet, make sure that you have correctly entered your proxy settings into Eclipse under Preferences->General->Network Connections.

If it’s enough of a problem to document, I think it’s enough of a problem to realize you have customers that aren’t allowed do download any plugins without having an approved body do it.

How much does it cost?

The initial install is for a 30 day trial.  After that, you have to decide if you are an “individual” or “corporate” user.  Once you decide you either fill out a form saying you are an individual or pay $150 per seat (perpetual license) and a license is e-mailed to you.  Just copy/paste the license into your IDE and you are done.

individual – you own the copyright.  In other words you aren’t selling what you write.  From talking to the vendor, it sounds like open source and code I write for JavaRanch fall into this category.

corporate – code you write for someone else to make money – also known as your job.

How well did it work?

I have three Java projects in the workspace I used to test:

  1. JForum – The JavaRanch fork of JForum.  About 200 classes and a bunch of unit tests.
  2. PickWinners – The program JavaRanch uses to pick winners in the book promotion.  About a dozen classes and about the same number of tests.  Both unit and integration tests.
  3. Play – this has one class and no unit tests.  I use it to test out “what does this code do”

My attempts:

  1. Edited a file in my Play project – it ran all the test cases in JForum and PickWinners.  At first, it looked like it wasn’t using dependencies, but I think this was because it was the first run and it though nothing had run yet.
  2. Edited the file in Play project again. This time it correctly realized no tests were affected.
  3. PickWinners now has 5 errors of type “Infinitest Test Failure” – All are errors accessing the database from my integration tests.
  4. After adding infinitest.filters, I had to clean the project to remove the failures.  (This was documented in the manual page.)  It does re-run tests that are still unfiltered.  It took me to passes to get the right filters.
  5. Made another change to PickWinners – it’s still running JForum tests
  6. Cooked and ate dinner (in other words, a bunch of time passed)
  7. Tried again – all of a sudden it  started picking up only the truely dependent tests.  Maybe there was a learning curve?  Cool.
  8. Almost everything I tried after dinner did pick up the correct tests based on dependencies.  It (usually) found tests that use the class indirectly.  It got faster and finding dependencies when I repeatedly edited the same class.  I assumed it cached the result.  Which is good because when doing real development, this is what happens.

What problems did I encounter?

  1. The JForum project doesn’t show the test failures as compiler errors.  The PickWinners project does.  The JForum project also complains sporadically about a class not being found that is in the same project.  Note: this problem temporarily went away after I restarted my workspace.  It came back after I introduced a failing test and corrected it.
  2. As I wrote above it usually finds the relevant tests.  For example, I changed the implementation of post.setText() to use the value “hi” instead of the passed in value.  I expected a whole gaggle of tests like the following to run and fail.  None of them did.

    @Test
    public void noCodeTags() {
    post.setText(“test”);
    PostCommon.processText(post);
    String actual = post.getText();
    assertEquals(“transformed post”, “test”, actual);
    }

  3. I have to assume this won’t work for reflection based code.  I don’t have any on hand.  While I could write some, it doesn’t sound like it work from the vendor’s description.  And if the vendor admits it won’t work, I’m inclined to believe them.

Final thoughts

I like the emphasis on “keeping the bar green.”  If you always see it on the screen, it reminds you it exists.  I haven’t had it installed for long, but I’m already used to it being there and relying on it.  Even if it isn’t perfect, it’s great to have present.

It worked well enough to be useful on my home projects.  I’m not sure how well it would function in my work environment where compiles involve a lot more code.  The “find the proper tests phase” was noticeable at home.  It could be better at work (faster machine) or worse (much bigger workspace.)

not sure how well it would work in my work environment where compiles involve a lot more code

SCJP Java Programmer Plus Certification delayed

What happened to the SCJP Java Programmer Plus Certification?
It got delayed.  As Campbell noted late this year is likely overly optimistic.

Announcement e-mail follows:


Update!
Sun Java Programmer Plus Certification Beta

Dear Jeanne,

Thank you for your interest in the Sun Java Programmer Plus Certification – Sun’s first performance based Java Certification exam.

We wanted to inform you that the full beta for the new Sun Java Programmer Plus Certification has been delayed to later in this year based on the results from initial beta testing and to align more closely with the upcoming release of JDK 7. We will get back in touch with you as soon as we have revised dates for the full beta test.

If you have any questions or feedback, please send a message to sunlearninglink@sun.com

Thank you,
Sun Microsystems