Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Timeout Policies for EJBs : How Do They Help…?

DZone's Guide to

Timeout Policies for EJBs : How Do They Help…?

· Java Zone
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

EJB 3.1 introduced timeout related annotations as a part of its API.

  • @AccessTimeout
  • @StatefulTimeout

Let’s quickly look at what they are and why are they important

@AccessTimeout

Specifies the time period after which a queued request (waiting for another thread to complete) times out.

@Singleton
@ConcurrencyManagement(ConcurrencyManagementType.CONTAINER) //this is actually by default
public class Singleton_With_Timeout{
    @AccessTimeout(5000,java.util.concurrent.TimeUnit.MILLISECONDS)
    @Lock(LockType.WRITE) //default configuration
    public void find(){
        //... business logic
    }
}
When your session bean instances are bombarded with concurrent requests, the EJB container ensures sanity by serializing these calls i.e. blocking other threads until the current thread finishes execution. You can refine this behavior further by using this annotation.

Which beans can leverage this annotation ?

This is applicable for

  • Stateful (@Stateful) beans and
  • Singleton beans (@Singleton) configured with container managed concurrency option (ConcurrencyManagementType.CONTAINER)

Why is it important ?

Since the EJB container serializes concurrent requests, having this annotation ensures that the potential (waiting) threads are not kept blocked for ever and helps define aconcurrency policy.

Where can I put this annotation?

  • On a class – globally applies to a all the methods
  • On a particular method only
  • On a particular method to override the settings of the class level annotation

How to use it ?

You can use the value and unit elements of this annotation to define its behavior

Here are a few options

  • @AccessTimeout(0) – this means that your method does not support concurrent access at all and the client would end up getting a java.ejb.ConcurrentAccessException
  • @AccessTimeout(-1) – your method will block indefinitely (I don’t think that’s good idea !)
  • @AccessTimeout(5000) – method will wait for 5000 ms (5 seconds) before the next thread in queue (if any) if given a chance

Few things to note

  • Default value for the unit element is java.util.concurrent.TimeUnit.MILLISECONDS
  • a timeout value of less than -1 is invalid

@StatefulTimeout

Defines the threshold limit for eviction of idle stateful session beans i.e. the ones which have not received client requests for a specific interval

@StatefulTimeout(15000,java.util.concurrent.TimeUnit.MILLISECONDS)
public class SFSB_With_Timeout{
    public void register(){
        //....business logic
    }
}
Why is it important ?

Imagine you have a stateful session bean handling a user registration workflow. The user is inactive for certain time interval (probably doing other stuff). How long would you want your stateful session bean active in the memory ? Configuring this annotation can help prevent inactive bean instances from hogging the main memory.

Where can I put this annotation?

Same rules as the @AccessTimeout annotation !

How to use it ?

You can use the value and unit elements of this annotation to define its behavior

Here are a few options

  • @StatefulTimeout(0) – this means that your bean instance will be removed immediately after the completion of the method which holds this annotation
  • @StatefulTimeout(-1) – your method will not be sensitive to time outs (man that’s stubborn !)
  • @StatefulTimeout(15000) – method will wait for 15000 ms (15 seconds) for client requests before it becomes a candidate for eviction

Few things to note

  • Default value for the unit element is java.util.concurrent.TimeUnit.MILLISECONDS
  • a timeout value of less than -1 is invalid

Cheers !

Download Modern Java EE Design Patterns: Building Scalable Architecture for Sustainable Enterprise Development.  Brought to you in partnership with Red Hat

Topics:

Published at DZone with permission of Abhishek Gupta, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}