JBoss AS 7 custom caches configuration

User Rating: 4 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Inactive
 

In this example we will learn how to configure a Custom Cache for your Stateful Session Beans for your JBoss AS 7 applications.

Custom caches can be configured in JBoss AS 7 using the cache definitions which are part of the ejb3 subsystem in domain/standalone.xml.  Within the definition of a cache, you have the option to specify a  passivation store, which can optionally define a max-size.

  Please note: JBoss AS 7 does not uses any more the @org.jboss.ejb3.annotation.cache.tree.CacheConfig annotation to reference your custom Cache. Take note when migrating from older JBoss applications!

Simple case: File passivation store

In the following example, we have defined a cache named "custom-cache" which refers to a file store named "custom-store":

 <subsystem xmlns="urn:jboss:domain:ejb3:1.2">
 . . . .
            <caches>
                <cache name="simple" aliases="NoPassivationCache"/>
                <cache name="passivating" passivation-store-ref="file" aliases="SimpleStatefulCache"/>
                <cache name="clustered" passivation-store-ref="infinispan" aliases="StatefulTreeCache"/>
                <cache name="custom-cache" passivation-store-ref="custom-store"/>
            </caches>
            <passivation-stores>
                <file-passivation-store name="file"/>
                <file-passivation-store name="custom-store" idle-timeout="160" idle-timeout-unit="SECONDS" max-size="500"/>
                <cluster-passivation-store name="infinispan" cache-container="ejb"/>
            </passivation-stores>
      . . . .
        </subsystem>
In order to use it in your applications you can add the org.jboss.ejb3.annotation.Cache to your application as in the following example:

 

import javax.ejb.*;

@org.jboss.ejb3.annotation.Clustered 
@org.jboss.ejb3.annotation.Cache("custom-cache")
 
@Stateful
@Remote(StatefulItf.class) 
public class Stateful implements StatefulItf {
     
}

Note: you can additionally include the @javax.ejb.StatefulTimeout in your EJB in order to specify the timeout for your stateful EJBs:
@StatefulTimeout(value = 360)
@org.jboss.ejb3.annotation.Clustered 
@org.jboss.ejb3.annotation.Cache("custom-cache")
 
@Stateful
@Remote(StatefulItf.class) 
public class Stateful implements StatefulItf {
     
}

Advanced case: Cluster passivation store

Besides simple file stores, you can configure a more advanced passivation strategy by refering a cluster-passivation-store:

 <caches>
   . . . .
   <cache name="custom-cluster" passivation-store-ref="custom-cluster"/>
</caches>

<passivation-stores>
   . . . .
   <cluster-passivation-store name="custom-cluster" cache-container="dist-ejb"/>
</passivation-stores>

In this example, we are referring a cluster passivation store named "custom-cluster" which in turns refers to a cache container which we have added in the infinispan subsystem as a distributed cache:
<cache-container name="dist-ejb" default-cache="dist-cache">
    <distributed-cache name="dist-cache" mode="ASYNC" batching="true">
       <eviction strategy="LRU"/>
       <file-store/>
    </distributed-cache>
</cache-container>

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

Clustering EJB 3 with JBoss AS

To cluster a stateless session bean in EJB 3 all you need to do i

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

Follow us on Twitter