How to migrate the Stateful EJB cache configuration from JBoss EAP 5 / 4 to JBoss EAP 6
Environment
- Red Hat JBoss Enterprise Application Platform (EAP)
- 6.x
Issue
- We are migrating our application from EAP 5 to EAP 6, and we try to migrate jboss.xml to jboss-ejb3.xml. So, where shoud I migrate the "container-cache-conf" tag which were configed in EAP 5 as below:
<container-cache-conf>
<cache-policy>org.jboss.ejb.plugins.LRUStatefulContextCachePolicy</cache-policy>
<cache-policy-conf>
<min-capacity>50</min-capacity>
<max-capacity>200</max-capacity>
<remover-period>1800</remover-period>
<max-bean-life>1320</max-bean-life>
<overager-period>300</overager-period>
<max-bean-age>1260</max-bean-age>
</cache-policy-conf>
</container-cache-conf>
Resolution
First, create a cache configuration in the JBoss profile xml under the ejb3 subsystem. For example, we will call it my-cache as shown below. Next create a file-passivation-store in the passivation-stores section. The cache passivation-store-ref will reference the file-passivation-store name, for the example below my-cache-file. In the my-cache-file, the max-size will be set to the value that you had for max-capacity and the idle-timeout set to the value you had for max-bean-age and the idle-timeout-unit to SECONDS.
<subsystem xmlns="urn:jboss:domain:ejb3:1.4">
...
<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="1260" idle-timeout-unit="SECONDS" max-size="200"/>
</passivation-stores>
...
</subsystem>
Next, create a jboss-ejb3.xml inside of your ejb jar's META-INF directory to configure your ejbs to use the cache you created:
<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>
Finally, you need to configure the stateful-timeout, which corresponds to the max-bean-life that you had previously.
The JavaEE 6 specification added the ability to configure the stateful-timeout, so it can be configured via EJB3 annotations or ejb-jar.xml for EJB3 and can be configured via ejb-jar.xml for EJB2.
EJB 3.1 Annotations
@StatefulTimeout(value = 1320, unit=java.util.concurrent.TimeUnit.SECONDS)
@Stateful
@Remote(MyStatefulEJBRemote.class)
public class MyStatefulEJB implements MyStatefulEJBRemote {
...
}
Configuring EJB3 or EJB2 via 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>1320</timeout>
<unit>Seconds</unit>
</stateful-timeout>
</session>
</enterprise-beans>
</ejb-jar>
Configuring EJB3 or EJB2 via jboss-ejb3.xml
The jboss-ejb3.xml extends the ejb-jar.xml, so you can configure everything via the jboss-ejb3.xml if you prefer such as:
<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">
<enterprise-beans>
<session>
<ejb-name>HelloBean</ejb-name>
<session-type>Stateful</session-type>
<stateful-timeout>
<timeout>1320</timeout>
<unit>Seconds</unit>
</stateful-timeout>
</session>
</enterprise-beans>
<assembly-descriptor>
<c:cache>
<ejb-name>*</ejb-name>
<c:cache-ref>my-cache</c:cache-ref>
</c:cache>
</assembly-descriptor>
</jboss:ejb-jar>
Unavailable Configuration Items in EAP 6
EJB cache policy "LRUStatefulContextCachePolicy" has been changed in EAP 6 so it is impossible to have 1-to-1 configuration mapping in EAP 6.
-
In EAP 6 we can setup following cache properties:
-
Bean life time (with @StatefulTimeout spec. in EJB 3.1)
-
When to passivate bean to disk file ("idle-timeout" in "file-passivation-store")
-
Max size in passivation store ("max-size" in "file-passivation-store")
-
What we CANNOT setup in EAP 6:
-
Min / Max numbers in memory cache
-
Min numbers in passivation store
-
the following *-period configurations which control the frequency of cache operations
- remover-period
- overager-period
- resizer-period
- max-cache-miss-period
- min-cache-miss-period
Related Articles
See this article for more information:
How to configure the Stateful EJB cache in JBoss EAP 6
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.