To cluster a stateless session bean in EJB 3 all you need to do is to annotate the bean class withe the @Cluster annotation. You can pass in the load balance policy and cluster partition as parameters to the annotation.

The default load balance policy is org.jboss.ha.framework.interfaces.RandomRobin and the default cluster is DefaultPartition.

Clustering a Stateless Session Bean:

As we said earlier all you have to do is tagging the EJB with the @Stateless annotation before the class declaration. Here's a sample:

package com.sample;

import java.io.Serializable;
import javax.ejb.*;
import org.jboss.annotation.ejb.Clustered;

@Stateless
@Clustered
public class HelloBean implements HelloBeanItf {
  int counter = 0;

  public String doSomething() {
     return "HelloBean Called"; 
  }

}

and here's the interface:  

package com.sample;

public interface HelloBeanItf {

   public String doSomething(); 

}

Not very difficult, isn't it ?? 

Clutering Stateful Session Beans

To cluster stateful session beans in EJB 3.0, you need to tag the bean implementation class with the @Cluster annotation, just as we did with the EJB 3.0 stateless session bean earlier.

@Stateful
@Clustered


public class HelloBean implements HelloBeanItf,Serializable {
int counter = 0;
  public void doSomething() {

     counter++;
     System.out.println("Value of counter is " + counter);

  }

So who takes care of session state replication ? the answer is JBoss Cache. JBoss Cache provides distributed cache and state replication services for the JBoss cluster. A JBoss cluster can have multiple JBoss Cache MBeans (known as the TreeCache MBean), one for HTTP session replication, one for stateful session beans, one for cached entity beans, etc.

The related MBean service is defined in the ejb3-clustered-sfsbcache-service.xml file in the deploy directory. The contents of the file are as follows.

<server>
   <mbean code="org.jboss.ejb3.cache.tree.PassivationTreeCache"
 name="jboss.cache:service=EJB3SFSBClusteredCache">
 
       <attribute name="IsolationLevel">READ_UNCOMMITTED</attribute>
       <attribute name="CacheMode">REPL_SYNC</attribute>
       <attribute name="ClusterName">SFSB-Cache</attribute>
       <attribute name="ClusterConfig">
           ... ...
       </attribute>

       <attribute name="SyncReplTimeout">10000</attribute>
       <attribute name="LockAcquisitionTimeout">15000</attribute>

       <attribute name="EvictionPolicyClass">org.jboss.ejb3.cache.tree.StatefulEvictionPolicy</attribute>

       <attribute name="EvictionPolicyConfig">
          <config>
             <attribute name="wakeUpIntervalSeconds">1</attribute>
            <name>statefulClustered</name>
            <region name="/_default_">
               <attribute name="maxNodes">1000000</attribute>
               <attribute name="timeToIdleSeconds">300</attribute>
            </region>
         </config>
      </attribute>

      <attribute name="CacheLoaderFetchPersistentState">false</attribute>
      <attribute name="CacheLoaderFetchTransientState">true</attribute>
      <attribute name="FetchStateOnStartup">true</attribute>
      <attribute name="CacheLoaderClass">org.jboss.ejb3.cache.tree.StatefulCacheLoader</attribute>
      <attribute name="CacheLoaderConfig">location=statefulClustered</attribute>
   </mbean>
</server>

JBoss AS 6 and 7 don't use any more JBoss Cache for providing distributed and replicated cache. The new provider is Infinispan which exposes a data grid platform and exposes and a JSR-107 (JCACHE) compatible Cache interface

0
0
0
s2smodern

Related articles available on mastertheboss.com

JBoss Clustering a Web Application

Please Note: This article cover JBoss AS 4/5/6 releases. If you w

JBoss monitoring HTTP Session replication

In this article we'll show how to monitor HTTPSession replication

How do I change multicast address of JBoss cluster ?

Since JBoss AS 4.0.3, the jboss.partition.udpGroup property can b

JBoss farming service

What is the farming service ? this article explains about it, als

JBoss HTTP Session replication

HTTP session replication is used to replicate the state associate

How do I know the Http Session replication size?

You need to add the <SIZE /> tag into your Cluster configur

W

i

l

d

F

l

y

 

c

h

e

a

t

s

h

e

e

t