I’ve been playing around with scheduled jobs a bit. I’ve mostly used Quartz in the past, but I thought I’d try out Automatic Timers from Java EE 6. They are pretty simple and can be defined either in an annotation or the ejb-jar.xml file. For instance, I might want to run two different jobs; one every 5 seconds and one every 10 with something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.util.Date;
import javax.ejb.Schedule;
import javax.ejb.Singleton;

@Singleton
public class SimpleJobs {

  @Schedule(second="*/5", minute="*", hour="*", persistent=true, info="job #1")
  public void jobOne() {
    System.out.println("Job one fired on " + new Date());
  }

  @Schedule(second="*/10", minute="*", hour="*")
  public void jobTwo() {
    System.out.println("Job two fired on " + new Date());
  }
}

The second, minute, and hour values all default to 0, so you need to provide wild cards for them in this case. The “*/5″ and “*/10″ basically means for every interval of X seconds, fire the job. Of course, there are values for day, week, month and year. There is support for ranges and starting time, so you can get as complicated or simple as you need. The persistent element is true by default and tells the server if the timer should remain after a restart or not. The info tag gives a simple name for programmatic display purposes.

I think these automatic timers seem like a nice addition and could come in handy in certain spots. I’m curious about their reliability, mutability, and accessibility though as I haven’t gotten a chance to play with them much. How will jobs react in a cluster or during a server crash? Is there an easy api interface to edit a running schedule? How can I know when jobs are being fired and when they aren’t? I’m going to write some more code and play with them a bit. I need to consolidate the status of all my scheduled jobs into one place, so we’ll see how these fit in. I’m sure a bit of magic pixie dust is all I’ll need.