Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to use and manage the EJB Timer Services

The EJB timer service is a container-provided service that enables the bean provider to register enterprise beans for timer call backs. In this article we will see how to use and manage it.

The EJB 2.1 specification was the first EJB specification to introduce timer service functionality to enterprise beans.

The container provides a reliable, transactional notification service for timed events. The timer service is intended for modeling long-lived business processes. Therefore, timers survive container crashes, server shutdowns, and so on. Even though the timer service is structured around millisecond units, it is not intended to be used for real-time notifications.

Timer Functionality

An enterprise bean can create a timer to occur based on a calendar-based schedule, at a specified time, after a specified elapsed time, or at specified intervals.Timers can be created programmatically, by interacting with the TimerService, or automatically through annotations.Using the timer functionality, an enterprise bean developer can do the following:

  • Create a timer callback notification.
  • Process a timer callbacknotification.
  • Interrogate a timer callback notification object.
  • Obtain a list of outstanding timer notifications.
  • Cancel a timer notification.

Using the timer functionality, an enterprise bean developer can do the following:

  • Create a timer callback notification.
  • Process a timer callback notification.
  • Interrogate a timer callback notification object.
  • Obtain a list of outstanding timer notifications.
  • Cancel a timer notification.
  • Timer Notification

When a timer expires, a timer notification callback method is invoked by the EJB container on the enterprise bean. Timernotificationcallback methods can be designated using one of the following options:

Annotate the method with the Timeoutannotation.

Implement the javax.ejb.TimedObjectinterface. This results in the timer invoking the ejbTimoutmethod.

Annotate the method with the @Scheduleannotation.

Timers can be created for stateless session beans, singleton session beans, and message-driven beans.

Stateless and singleton session beans

A timer created by a stateless or singleton session bean is associated with the session bean type. When the timer expires, the container invokes the designated timer callback method on an instance of the specific session bean selected from the pool.

Message-driven beans

A timer created by a message-driven bean is associated with the message-driven bean type. When the timer expires, the container invokes a designated timer callback method on an instance of the specific message-driven bean selected from the pool.

Creating a Timer Callback Notification

This section examines the types of timers available to enterprise beans and describes how to create a timer callback notification.

Timers are created by interacting with the TimerService. An enterprise bean developer can access the TimerServiceeither through its context or using dependency injection.

Examining Types of Timers

Java EE application servers support three types of timer notifications:

Non-interval timer notificationNon-interval notification timers can be represented using an absolute time in the future (using a java.util.Dateobject) or a delay interval. Non-interval timers result in a single notification of the timer’s callback method.

Interval timer notificationInterval notification timers result in multiple notifications of a timer’s callback method, at specified intervals. An interval notification timer can be created using an absolute time or a delay.

Calendar-based schedule timer notificationCalendar-based schedules are similar to UNIX® cronjobs and support a highly-customizable schedule. Calendar-based schedule timers were introduced as part of the EJB 3.1 specification.

Non-interval Notification Timers

Non-interval notification timers are single event timers. Once a single event timer expires, it is no longer considered a valid Timerwithin the context of a TimerService. This means, that after a single event timer expires, it cannot be rescheduled or cancelled.

Absolute Time Single Event Timer

Bellow Figure shows an absolute time, single event notification timer. You use this type of timer when the timer callback needs to be invoked at a specific time in the future.

Absolute Time Single Event Notification Timer

Figure 1: Absolute Time Single Event Notification Timer

To create an absolute time notification timer, you use the TimerServiceinterface method: createTimer(Date expiration, Serializable info).

Note: You use the serializableinfo parameter to store information in the timer object for use when the timer event expires. You can use this information to assist with the identification of the timer or as data to process the timer event.

Relative Time Single Event Timer

Y

ou use this type of timer when the timer callback needs to be invoked after a specific time in the future.

To create a relative time notification timer, you use the TimerServiceinterface method: createTimer(long duration, Serializable info). You specify the durationparameter in milliseconds.

A

ctivity Creating an Automatic Timer

In this exercise, you will create an automatic timer. Remember, automatic timers are created on your behalf by the container.

This exercise contains the following sections:

  • “Task 1 -Create a Singleton Bean”
  • “Task 2 -Create an Automatic Timer”
  • “Task 3 -Test the Timer”

Task 1 - Create a Singleton Bean

In this task, you will create a singleton bean. The singleton bean will function as a timer.Complete the following steps:

  • Right-click on EJB31-ejb and create a singleton session bean named AutomaticSayHelloBean:

Package: helloworld.beans.
Session Type: Singleton.
Create Interface: .

  • Modify the bean to support eager initialization at startup:
    • Import javax.ejb.Startup.
    • Annotate the EJB with @Startup.

Task 2 - Create an Automatic Timer

In this task, you will modify the AutomaticSayHelloBean. The EJB will cause the container to automatically create a timer based on a schedule.

The schedule will cause a timer notification to occur ever 5 seconds. Complete the following steps:

Declare a reference to SingletonHelloWorldBean:

  • Right-click > Insert Code > Call Enterprise Bean
  • Select SingletonHelloWorldBean.
  • Choose the “No Interface” reference type.

Implement a sayHellomethod:

  • Right-click > Insert Code > Add Business Method
  • Specify sayHelloas the method name.
  • Specify void as the return type.
  • Click the OK button.
  • Retrieve a GreetingRequestfromSingletonHelloWorldBeanGreetingRequest request = singletonHelloWorldBean.sayHello();

Modify the sayHellomethod to support timer notification:

  • Change the parameter list to include a Timer.
  • Annotate the sayHellomethod with @Schedule.
  • Define a schedule so the timer expires every five seconds.@Schedule(second="*/5", minute="*", hour="*")public void sayHello(Timer timer) {GreetingRequest request = singletonHelloWorldBean.sayHello(); System.out.println("AutomaticSayHelloBean.sayHello: - " + request);}

Import the following:

  • importjavax.ejb.Schedule;
  • importhelloworld.vo.GreetingRequest;
  • importjavax.ejb.Timer;

Task 3 - Test the Timer

In this task, you will test the automatic timer.You do not need to write a client to schedule the timer with the timer service. The creation of the timer will automatically happen on start up.

Complete the following steps:

  • Build and deploy the application to the server.
  • Click the GlassFish v3 Domain tab to examine the output.

You should see a message from the AutomaticSayHelloBeanevery 5 seconds.

Summary

This module describes how you can implement enterprise beans that receive notification from the timer service. The key points can be summarized as follows:

The TimerServiceis a container-managed service.

The TimerServicecan be accessed by an enterprise bean through its context or dependency injection.

There are three Timertypes that can be created programmatically or automatically:

  • Interval
  • Non-interval
  • Calendar-based schedule

Calendar-based schedule timers have the most flexibility and model the UNIX cron utility.

Notification of a timer’s expiration is performed using a callback facility:

  • ejbTimeoutfor enterprise beans that implement the TimedObjectinterface
  • Methods annotated with programmatic timer creation using @Timeout
  • Methods annotated with automatic timer creation using @Schedule

Timers can be persistent (surviving a server crash) or non-persistent.

We learnt about the process to use and manage the Timer services. We saw how EJB timer service enables the bean provider to register enterprise beans for timer call-backs. See you with another article soon.



I am a software developer from India with hands on experience on java, html for over 5 years.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login