EJB Annotations cheatsheet for JBoss AS or WildFly

There is a vast number of annotations you can use when running EJB applications, spanning from Cache configuration to Clustering and Security. Let's see them more in detail in this tutorial.

Configuring the Stateless EJB Pool size

You can use the following annotation to configure the EJB pool size, by referring to one of the available pool configurations (See also Custom pool configuration for your EJBs on JBoss AS 7/WildFly ):

@org.jboss.ejb3.annotation.Pool(value="my-strict-max-pool")
public class ExampleSLSB { }

Configuring Passivation

Since EJB 3.2 there is a portable way for disabling passivation of Stateful Beans. This is the corresponding annotation which can be applied at Class level:

@javax.ejb.Stateful(passivationCapable=false)
public class ExampleSFSB { }

Configuring Timeout for Stateful Beans and Singleton methods

The timeout value and the time unit can be specified using the @javax.ejb.AccessTimeout annotation on the SFSB/Singleton method. It can be specified on the session bean (which applies to all the bean’s methods) and on specific methods to override the configuration for the bean.

The following singleton has a default access timeout value of 60 seconds, specified using the TimeUnit.SECONDS constant:

@Singleton
@AccessTimeout(value=60, timeUnit=SECONDS)
public class StatusSingletonBean { ... }

Stateful timeout

The Java EE 6 annotation @javax.ejb.StatefulTimeout can be used to configure how long a stateful bean can be idle before it is eligible for removal by the container.

@StatefulTimeout(value = 3, unit=java.util.concurrent.TimeUnit.MINUTES) 
@Stateful
public class StatefulEJB  {
  ...
}
  • A value >0 indicates a timeout value in the units specified by the unit element.
  • A value of 0 means the bean is immediately eligible for removal.
  • A value of -1 means the bean will never be removed due to timeout.

Stateful Cache configuration

You can switch to one of the available Cache aliases directly from your EJBs using the @org.jboss.ejb3.annotation.Cache as in the following example which switches to the default non-passivation cache:

@org.jboss.ejb3.annotation.Cache("NoPassivationCache")
@Stateful

public class No PassivationStatefulEJB {
...
}

Clustering

In JBoss AS 7 and WildFly 8 you need to specify if an EJB will be clusterable or not. This is done through the @org.jboss.ejb3.annotation.Clustered which is used to trigger clustering services, as in the following example:

@Stateful
@Clustered
public class ClusteredStatelefulBean { ... }

Please note that this annotation is deprecated in WildFly 9/10 and JBoss EAP 7 and will be thus ignored by the application server. As a matter of fact EJBs are automatically clustered in WildFly 9/10 and JBoss EAP 7 when deployed to an "ha" profile.

Security

In order to secure EJB applications you use the @javax.annotation.security.RolesAllowed combined with JBoss annotations like @org.jboss.ejb3.annotation.SecurityDomain to specify the Security Domain which will be used to protect your applications. In the following example, we are restricting access to the EJB named SafeEJB using the role Manager that we have earlier defined:

import org.jboss.ejb3.annotation.SecurityDomain;
import javax.annotation.security.RolesAllowed;
@Stateless
@SecurityDomain("other")
@RolesAllowed( { "Manager" })
public class SafeEJB { }

If you want to check annotations which are specific to Message Driven Beans, check for the following tutorial: 10 Annotations you can use on your MDBs

Follow us on Twitter