How to Create a Scheduler Module in a Java EE 6 Application with TimerService
Many a time, in a Java EE application, besides the user-triggered transactions via the UI (e.g. from the JSF), there's a need for a mechanism to execute long running jobs triggered over time, e.g., batch jobs. Although in the EJB specs there's a Timer service, where Session Beans can be scheduled to run at intervals through annotations as well as programmatically, the schedule and intervals to execute the jobs have to be pre-determined during development time and Glassfish does not provide the framework and the means to do that out-of-the-box. So it is left to the developer to code that functionality or to choose a 3rd party product to do that.
In one of my previous projects using a different application server, I implemented a scheduler module for the application. So with that experience, I will discuss in this article how to create a simple scheduler called SchedulerApp in NetBeans IDE 6.8 that can be deployed in Glassfish v3. The example comes with a framework and the JSF2 PrimeFaces-based UI to schedule and manage (CRUD) your batch jobs implemented by Stateless Session Beans without having to pre-determine the time and interval to execute them during development time. Below is the Class Diagram to give you an overview of the application:
Through this exercise, I also hope that you will have a better understanding of the Timer Service in the EJB specs and how you can use it in your projects.
Note: If you cannot get your copy running, not to worry, you can get a working copy here.
Tutorial Requirements
Before we proceed, make sure you review the requirements in this section.
Prerequisites
This tutorial assumes that you have some basic knowledge of, or programming experience with, the following technologies.
- JavaServer Faces (JSF) with Facelets
- Enterprise Java Beans (EJB) 3/3.1 esp. the Timer Service
- Basic knowledge of using NetBeans IDE will help to reduce the time required to do this tutorial
Software needed for this Tutorial
Before you begin, you need to download and install the following software on your computer:
- NetBeans IDE 6.8 (Java pack), http://www.netbeans.org
- Glassfish Enterprise Server v3, https://glassfish.dev.java.net
- PrimeFaces Component Library, http://www.primefaces.org
Notes:
- The Glassfish Enterprise Server is included in the Java pack of NetBeans IDE, however, Glassfish can be installed separately from the IDE and added later into Servers services in the IDE.
- A copy of the working solution is included here if needed.
Creating the Enterprise Projects
The approach for developing the demo app, SchedulerApp, will be from the back end, i.e., the artifacts and services needed by the front-end UI will be created first, then working forward to the User Interface, i.e., the Ajax-based Web UI will be done last.
The first step in creating the application is to create the necessary projects in NetBeans IDE.
- Choose "File > New Project" to open the New Project Wizard. Under Categories, select Java EE; under Projects select Enterprise Application. Click Next.
- Select the project location and name the project, SchedulerApp, and click Next.
- Select the installed Glassfish v3 as the server, and Java EE 6 as the Java EE Version, and click Finish.
The above steps will create 3 projects, namely SchedulerApp (Enterprise Application project), SchedulerApp-ejb (EJB project), and SchedulerApp-war (Web project).
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)





Comments
Henk De Boer replied on Sun, 2010/01/10 - 3:27pm
Christopher,
I love the way you setup and describe all of this in so much detail, but I want to give you the same warning as I gave for your previous tutorial:
JobMBean is a session scoped backing bean, yet it holds a reference to an object as instance data upon which an action method acts.
In general this is a bad practice and a very brittle construct with respect to users having multiple windows open. Since you are using Java EE 6, I think you would be better off using CDI managed beans instead of the native JSF ones. That way you can take advantage of the conversation scope, which is specifically designed to transfer data between page requests. If you can't or won't want to use CDI and/or the conversation scope, then you could look into the Tomahawk savestate tag.
Andy Gibson replied on Sun, 2010/01/10 - 5:35pm
Weld, the CDI implementation used in Glassfish, doesn't quite work right with asynchronous timer services. When the method is called by the timer service the required contexts aren't active and it causes an error. The JIRA issue is here.
Christopher Lam replied on Sun, 2010/01/10 - 9:30pm
in response to:
Henk De Boer
I hear you and understand where you are coming from. Will consider your suggestion in my next tutorial. Thanks for the feedback :-)
Christopher
Cagatay Civici replied on Mon, 2010/01/11 - 8:32am
Eric Rich replied on Mon, 2010/01/11 - 12:46pm
Nice contribution, thanks!
Flux - Java Job Scheduler. File Transfer. Workflow.
Eirik Maus replied on Mon, 2010/01/11 - 3:51pm
Henk De Boer replied on Mon, 2010/01/11 - 5:46pm
in response to:
Eirik Maus
Uhm, java.util.Timer has quite a lot of problems really.
If you are afraid of something which is supposedly 'a little heavy', you could better take a look at Java SE's executor service.
Gareth Uren replied on Tue, 2010/01/19 - 10:01pm
Christopher,
According to the tutorial, the timers(jobs) are persistent. However, when restarting the Glassfish server, the Job list is empty. Also, investigating the logs reveals that no jobs are present.
Any ideas on this problem I'm having. btw - this is directly run from the example app you've provided.
Carla Brian replied on Wed, 2012/05/09 - 9:16am
Matt Coleman replied on Wed, 2012/07/11 - 2:31am
great tutorial as usual...this is very relevant
buffalo graphic design
Mateo Gomez replied on Wed, 2012/08/22 - 1:46am
in response to:
Eric Rich
very nice indeed..i am using this formula too Flux - Java Job Scheduler. File Transfer. Workflow.
chicken burrito recipe