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.
Before we proceed, make sure you review the requirements in this section.
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
- 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).