Skip to main content

Creating Calendar Based Timers in Java EE 6

Java EE 6 allows developers to create application timers that are initialized when either a Stateless Session Bean, a Singleton Bean or a Message Driven Bean are deployed to the application server.

To indicate that a method on any of these beans is to be invoked on a timed basis, the method must be annotated with either the @Schedule annotation (for single timer schedules), or the @Schedules annotation (for multiple timer schedules).

The code below shows a very simple Stateless Session Bean configured with 2 scheduled timers.  The first timer is configured with one schedule whereas the second is configured with 2 schedules.

package com.acme.timer;

import javax.ejb.Schedule;
import javax.ejb.Schedules;
import javax.ejb.Stateless;
import javax.ejb.Timer;

@Stateless
public class CalendarTimer {

  @SuppressWarnings("unused")
  @Schedule(second = "*/10", minute = "*", hour = "8-17", dayOfWeek = "Mon-Fri", dayOfMonth = "*", month = "*", year = "*", info = "Scheduled Timer")
  private void scheduledTimeout(final Timer t) {
    System.out.println(t.getInfo().toString() + " called at: "
        + new java.util.Date());
  }

  @SuppressWarnings("unused")
  @Schedules({
      @Schedule(second = "15", minute = "*", hour = "8-17", dayOfWeek = "Mon-Fri", dayOfMonth = "*", month = "*", year = "*", info = "2nd Scheduled Timer"),
      @Schedule(second = "45", minute = "*", hour = "8-17", dayOfWeek = "Mon-Fri", dayOfMonth = "*", month = "*", year = "*", info = "2nd Scheduled Timer") })
  private void scheduledTimeout2(final Timer t) {
    System.out.println(t.getInfo().toString() + " called at: "
        + new java.util.Date());
    System.out.println();
  }
}

As can be seen, the first timer is annotated with the @Schedule annotation. This annotation takes several parameters that define the timer schedule:

secondNumber of seconds: 0 through 59
minuteNumber of minutes: 0 through 59
hourNumber of hours: 0 through 23
dayOfWeekDay of the week.  This can take textual values (Sun, Mon, Tue, Wed, Thu, Fri, Sat) or numerical values 0 through 7 (both 0 and 7 indicate Sunday)
dayOfMonthDay of the month. This can take textual values (1st, 2nd etc), or numeric values 1 through 31. Negative values can also be used to indicate days before the end of the month. The value Last can also be used to indicate the last day of the month.
monthMonth of the year. This can take textual values (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec) or numerical values 1 through 12
yearThe year. This can take numeric years in the format yyyy.
infoAdditional information passed to the timer function.

The table above shows the allowable values that can be used for each expression used to build up a schedule. These values can also be expanded into expressions to make more complex schedules.

Wildcard: A wildcard character (*) is used to indicate that the schedule will fire for every valid value of the specific operand. For example, setting the value second="0", minute="*" would cause a timer to be invoked every minute at 0 seconds.

Lists: Comma separated lists of values allow timers to occur at every value in the list rather than at all valid values as specified by the wildcard character.  For example second="0", minute="0, 15, 30, 45" would cause a timer to be invoked every quarter of an hour.

Ranges: Hypen separated ranges allow timers to occur within the specified range.  For example dayOfMonth="1-5" would cause a timer to be invoked every day for the first 5 days of each month.

Intervals: Intervals are defined in the format start/interval and are valid only for hours, minutes and seconds.  An interval is defined as the start value for a timer and then the interval at which a timer will be invoked.  For example hour="12/1" would cause a timer to be invoked on the hour, every hour in an afternoon. It's possible to combine the wildcard and interval expressions to cause a timer to be invoked every x hours, minutes or seconds.  For example  minute="*/10" would cause a timer to be invoked every 10 minutes.

The second method in the example above shows how 2 different schedules can be applied to a timer.  In this instance, the method is annotated with the @Schedules annotation rather than the @Schedule annotation.

Comments

Popular posts from this blog

Creating a Joke Application in React

Introduction I've recently started learning React. I've been a backend developer for a long time, but have started to love doing client side work. As they say, the best way to learn is to practise. So here is a simple joke application that I've written in React. The source code for the application can be found on GitHub at: https://github.com/doobrie/react-joke Creating the project Whilst practising, I've quite often created projects from scratch, but this is quite tedious as the same steps need to be taken for each project. I needed to create the project structure, configure Babel, write some control scripts etc. Instead of doing that this time, I've used the create-react-app tool to scaffold the basics of an application. npx create-react-app This sets up everything that you need to get started with a React app. Coding As this is a simple project, I've created one react component, function app() . I've created this as a functional c...

Changing Default Search Provider in Firefox on Linux Mint

On Linux Mint, the default version of Firefox is installed and configured to allow the following search engines to be queried directly from the address bar: Yahoo! Startpage DuckDuckGo Wikipedia Mint defines these as the default available set of search engines based upon 3 criteria (funding to Linux Mint, privacy support and whether the search engine is non-commercial). Other search engines such as Google, Bing or Twitter, etc. can easily be added into the default version of Firefox however. To add a different search provider, browse to Search Engines At the bottom of the page, click on the icon of the requested search engine, then click on the ... button in the URL bar and select the Add Search Engine option. You then have the option to change the default search engine within Firefox preferences to your new choice.

The new Eclipse logo

In a blog post today, Ian Skerrett has announced the new logo for Eclipse. Last fall we [The Eclipse Foundation] started a process to  update the Eclipse logo . The existing logo had not been change since Eclipse was launched and it was showing its age. I am happy to announce we have finished the process and am pleased to introduce the updated Eclipse logo. The new logo has a fresh modern look to it, which I think is a good improvement and will stand Eclipse in good stead for the future. The new logo will be included with the next Eclipse release train, Luna, and gradually rolled out across the Eclipse site and projects. What do you think?  Do you like the new logo?  Add your comments below.