Chapter 23. Clustered Session EJBs

Session EJBs provide remote invocation services. They are clustered based on the client-side interceptor architecture. The client application for a clustered session bean is the same as the client for the non-clustered version of the session bean, except for some minor changes. No code change or re-compilation is needed on the client side. Now, let us check out how to configure clustered session beans in EJB 3.0 and EJB 2.x server applications respectively.

23.1. Stateless Session Bean in EJB 3.0

Clustering stateless session beans is probably the easiest case since no state is involved. Calls can be load balanced to any participating node (i.e. any node that has this specific bean deployed) of the cluster.
To cluster a stateless session bean in EJB 3.0, simply annotate the bean class with the @Clustered annotation. This annotation contains optional parameters for overriding both the load balance policy and partition to use.
public @interface Clustered
{
   String partition() default "${jboss.partition.name:DefaultPartition}";
   String loadBalancePolicy() default "LoadBalancePolicy";
}
  • partition specifies the name of the cluster the bean participates in. While the @Clustered annotation lets you override the default partition, DefaultPartition, for an individual bean, you can override this for all beans using the jboss.partition.name system property.
  • loadBalancePolicy defines the name of a class implementing org.jboss.ha.client.loadbalance.LoadBalancePolicy, indicating how the bean stub should balance calls made on the nodes of the cluster. The default value, LoadBalancePolicy is a special token indicating the default policy for the session bean type. For stateless session beans, the default policy is org.jboss.ha.client.loadbalance.RoundRobin. You can override the default value using your own implementation, or choose one from the list of available policies:
    org.jboss.ha.client.loadbalance.RoundRobin
    Starting with a random target, always favors the next available target in the list, ensuring maximum load balancing always occurs.
    org.jboss.ha.client.loadbalance.RandomRobin
    Randomly selects its target without any consideration to previously selected targets.
    org.jboss.ha.client.loadbalance.aop.FirstAvailable
    Once a target is chosen, always favors that same target; i.e. no further load balancing occurs. Useful in cases where "sticky session" behavior is desired, e.g. stateful session beans.
    org.jboss.ha.client.loadbalance.aop.FirstAvailableIdenticalAllProxies
    Similar to FirstAvailable, except that the favored target is shared across all proxies.
Here is an example of a clustered EJB 3.0 stateless session bean implementation.
@Stateless
@Clustered
public class MyBean implements MySessionInt
{
   public void test()
   {
      // Do something cool
   }
}
Rather than using the @Clustered annotation, you can also enable clustering for a session bean in jboss.xml:
 
<jboss>    
   <enterprise-beans>
      <session>
         <ejb-name>NonAnnotationStateful</ejb-name>
         <clustered>true</clustered>
         <cluster-config>
            <partition-name>FooPartition</partition-name>
            <load-balance-policy>org.jboss.ha.framework.interfaces.RandomRobin</load-balance-policy>
         </cluster-config>
      </session>    
   </enterprise-beans>
</jboss>

Note

The <clustered>true</clustered> element is really just an alias for the <container-name>Clustered Stateless SessionBean</container-name> element in the conf/standardjboss.xml file.
In the bean configuration, only the <clustered> element is necessary to indicate that the bean needs to support clustering features. The default values for the optional <cluster-config> elements match those of the corresponding properties from the @Clustered annotation.