How to configure the Stateful EJB cache in JBoss EAP 6

Solution Unverified - Updated

Environment

  • Red Hat JBoss Enterprise Application Platform (EAP)
    • 6.x

Issue

  • How to configure the Stateful EJB cache in JBoss EAP 6 ?
  • How to configure timeouts for stateful session beans ?
  • Is it possible to configure an Infinispan cache for clustered EJBs on a per EJB class basis?
  • How to disable the SFSB passivation for some of the stateful session beans (at bean level, not server wide) ?
  • How to disable SFSB passivation ?
  • How to configure EJB stateful default-access-timeout in JBoss EAP 6.3?
  • Is there any rule need to follow to configure session bean time out in JBoss EAP 6.3?
  • On jboss 5 we did set the max bean life of SFSB. How could we modify it for jboss eap 6?

Resolution

    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. 

Values less than -1 are not valid. 

Note: The default "timeunit" is MINUTES. JBoss defaults this to "-1".

If "StatefulTimeout=0" : the bean is immediately eligible for removal
If "StatefulTimeout=-1" means the bean will never be removed due to timeout, but if you are using a passivating cache configuration in JBoss EAP 6, and the bean is idle for idle-timeout, then JBoss will passivate the bean instance to the passivation-store
If "StatefulTimeout > 0", then the bean is eligible for removal after the StatefulTimeout, but if you are using a passivating cache configuration in JBoss EAP 6 and the idle-timeout specified on the passivation-store is less than the StatefulTimeout, then JBoss will passivate the bean if it is idle for idle-timeout and then if it is idle for StatefulTimeout, then it is eligible for removal.

@StatefulTimeout(value = 5, unit=java.util.concurrent.TimeUnit.MINUTES) 
@Stateful
@Remote(MyStatefulEJBRemote.class)
public class MyStatefulEJB implements MyStatefulEJBRemote {
  ...
}
  • The StatefulTimeout can also be set via the ejb-jar.xml:
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd"
         version="3.1">
    <enterprise-beans>
        <session>
            <ejb-name>HelloBean</ejb-name>
            <session-type>Stateful</session-type>
            <stateful-timeout>
              <timeout>30</timeout>
              <unit>Minutes</unit>
            </stateful-timeout>
        </session>
    </enterprise-beans>
</ejb-jar>
  • The default Stateful Session Bean Cache if not specified via the annotation "@org.jboss.ejb3.annotation.Cache" or via "jboss-ejb3.xml" is simple which is a "NoPassivationCache".

The passivating / SimpleStatefulCache will passivate Stateful Session beans to the file system as configured in passivation-stores, which has options of idle-timeout, idle-timeout-unit and max-size.

<subsystem xmlns="urn:jboss:domain:ejb3:1.4">
    <session-bean>
        <stateless>
            <bean-instance-pool-ref pool-name="slsb-strict-max-pool"/>
        </stateless>
        <stateful default-access-timeout="5000" cache-ref="simple"/>
        <singleton default-access-timeout="5000"/>
    </session-bean>
...
    <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="my-cache" passivation-store-ref="my-cache-file" aliases="my-custom-cache"             
    </caches>
    <passivation-stores>
        <file-passivation-store name="file" idle-timeout="120" idle-timeout-unit="SECONDS" max-size="500"/>
        <cluster-passivation-store name="infinispan" cache-container="ejb"/>
        <file-passivation-store name="my-cache-file" idle-timeout="10" idle-timeout-unit="MINUTES" max-size="350"/>
    </passivation-stores>
...
</subsystem>
<subsystem xmlns="urn:jboss:domain:infinispan:1.4">
...
    <cache-container name="ejb" aliases="sfsb sfsb-cache" default-cache="repl" module="org.jboss.as.clustering.ejb3.infinispan">
        <transport lock-timeout="60000"/>
        <replicated-cache name="repl" mode="ASYNC" batching="true">
            <eviction strategy="LRU" max-entries="10000"/>
            <file-store/>
        </replicated-cache>
        <!--
          ~  Clustered cache used internally by EJB subsytem for managing the client-mapping(s) of
          ~                 the socketbinding referenced by the EJB remoting connector 
          -->
        <replicated-cache name="remote-connector-client-mappings" mode="SYNC" batching="true"/>
        <distributed-cache name="dist" mode="ASYNC" batching="true" l1-lifespan="0">
            <eviction strategy="LRU" max-entries="10000"/>
            <file-store/>
        </distributed-cache>
    </cache-container>
...
</subsystem>

Defining a custom cache configuration
See my-cache in the example above.

The cache name my-cache would be configured via an annotation or jboss-ejb.xml as shown below for all Stateful EJBs that you want to use the my-cache configuration.

<cache name="my-cache" passivation-store-ref="my-cache-file" aliases="my-custom-cache"             

The passivation-store-ref references the file-passivation-store with the given name and specifies the idle-timeout and max-size you would like to use.

<file-passivation-store name="my-cache-file" idle-timeout="10" idle-timeout-unit="MINUTES" max-size="350"/>

Using Annotations

@org.jboss.ejb3.annotation.Cache("my-cache")
@StatefulTimeout(value = 5, unit=java.util.concurrent.TimeUnit.MINUTES) 
@Stateful
@Remote(MyStatefulEJBRemote.class)
public class MyStatefulEJB implements MyStatefulEJBRemote {
  ...
}
cache-ref This attribute is used to set the default cache for non-clustered beans. It can be overridden by jboss-ejb3.xml, or via the org.jboss.ejb3.annotation.Cache annotation.

Using xml

Note: this will override the annotations

This content is not included.jboss-ejb3.xml

<jboss:ejb-jar xmlns:jboss="http://www.jboss.com/xml/ns/javaee"
                  xmlns="http://java.sun.com/xml/ns/javaee"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xmlns:c="urn:ejb-cache:1.0"
                  xsi:schemaLocation="http://www.jboss.com/xml/ns/javaee http://www.jboss.org/j2ee/schema/jboss-ejb3-2_0.xsd http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd"
                  version="3.1"
                  impl-version="2.0">

  <assembly-descriptor>
    <c:cache>
      <ejb-name>*</ejb-name>
      <c:cache-ref>my-cache</c:cache-ref>
    </c:cache>
  </assembly-descriptor>
</jboss:ejb-jar>

Disable Passiavtion of SFSB

  • Since simple stands for NoPassivationCache in the standalone.xml, using <c:cache-ref>simple</c:cache-ref> for a particular SFSB in the above jboss-ejb3.xml will disable the SFSB passivation for that bean.
  • Similary, you can disable the passivation of the SFSB by using the annotation @org.jboss.ejb3.annotation.Cache("NoPassivationCache")

Clustered Cache

The clustered cache will specify a passivation-store-ref of infinispan and a cache-container. The cache-container value will match up to the cache-container in the infinispan subsystem.

Related Articles

Components
Category

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.