I'm have obtained the following certifications to date:

  • Sun Certified Developer for Java Web Services 5
  • Sun Certified Specialist for NetBeans IDE
  • Sun Certified Solaris 10 System Administrator
  • Sun Certified Developer for Java 2 platform
  • Sun Certified Enterprise Architect for the Java Platform, Enterprise Edition 5
  • Sun Certified Programmer for Java 2 platform 1.4
  • Sun Certified Enterprise Architect for J2EE 1.4 Technology
  • IBM Certified SOA Solution Designer
  • IBM Certified Solution Designer – WebSphere MQ V5.3
  • IBM Certified Deployment Professional – WebSphere Studio Application Monitor V3.2
  • IBM Certified Application Developer – Rational Application Developer for WebSphere V6
  • IBM Certified Advanced System Administrator – WebSphere Application Server 5.0
  • IBM Certified System Administrator – WebSphere Application Server 5.0
Christopher has posted 2 posts at DZone. View Full User Profile

How to Create a Scheduler Module in a Java EE 6 Application with TimerService

01.10.2010
| 63127 views |
  • submit to reddit

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:


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.

  1. Choose "File > New Project" to open the New Project Wizard. Under Categories, select Java EE; under Projects select Enterprise Application. Click Next.

  2. Select the project location and name the project, SchedulerApp, and click Next.

  3. 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).

 

Published at DZone with permission of its author, Christopher Lam.

(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

Henk,

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

Just an update, upcoming version of PrimeFaces2 does not require p:resources as it places resources with JSF2 APIs using h:head automatically. Additionally PrimeFaces Resource Servlet is auto-registered in GlassFish v3 so no need to define that as well.

Eric Rich replied on Mon, 2010/01/11 - 12:46pm

Christopher,

Nice contribution, thanks!

Flux - Java Job Scheduler. File Transfer. Workflow.

Eirik Maus replied on Mon, 2010/01/11 - 3:51pm

OK, but TimerService is actually a little heavy (read: cumbersome, hard to test and, thus, error prone). My experience (from big-money transfer enterprise systems) is that JavaSE's java.util.Timer is much easier to use and never ever fails (never never, ever ever). Use that instead if you want reliability and simplicity. Just because it's big and heavy doesn't mean it does a better job at running your code - it only means it will spend more time executing something else than your code).

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

The timer service of the enterprise bean container enables you to schedule timed notifications for all types of enterprise beans except for stateful session beans. - Incredible Discoveries

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

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.