We're running a Stateless Session Bean to retrieve some data from various locations.
What's the best way to achieve caching for this SLSB ?
Using interceptors ? Using JBossCache ?
We're using JBoss 5.0.1.
We're running a Stateless Session Bean to retrieve some data from various locations.
What's the best way to achieve caching for this SLSB ?
Using interceptors ? Using JBossCache ?
We're using JBoss 5.0.1.
A steteless session bean itself cannot hold data between invocations as the name says it is stateless. However. you can create a stateful session bean that holds your cache and pass this bean to your stateless bean method.
void someMethod(StateFulBean sfb){
Cache cahce = sfb.getCache();
...
}
When using EBJ 3.1 you can also make use of the Singleton annotation and inject the cache as a singleton into your stateless session bean.
Technically, you are allowed to retain state in a stateless session bean, it's just that the state may be cleared by the container between invocations. As as result, you should not keep the cache itself in the bean, since it may just vanish.
Since you're using JBoss Appserver already, JBossCache would seem like the clear winner here. You can configure JBossCache instances using jboss service descriptors, and your EJB can then look up the cache instance from JMX/JNDI. It's all included in JBossAS already, so no additional dependencies needed.
Using JBossCache also gives you the added bonus of a cache distributed across a cluster, if that's something of interest to you.