2 Replies Latest reply on Mar 11, 2005 12:20 PM by jamesstrachan

    Reloading stateless session beans programmatically in JBoss

    Ganesh Subrahmanya Newbie

      I use stateless session beans and deploy it in a JBoss application server. I hold some read only state in the session beans. Normally this data held in instance variables (state) doesn't change. But very rarely it can change in which case the state becomes stale. So whenever this data changes I need to obtain the fresh set of data. Inorder to achieve this across all bean instances I need to make the application server reload the stateless session beans.

      Can any body tell me how to programmatically reload the stateless session beans in JBoss ?

        • 1. Re: Reloading stateless session beans programmatically in JB
          Kris Newbie

          I'm not sure whether it works for Session Beans, but you could try something like the Sepuku Pattern which can be used to force reloads of Entity beans when using Commit Option A: if you throw a RuntimeException from your bean, the J2EE specification says the bean must be discarded. I guess (but haven't checked) that the same rule of discarding applies to Stateless Session Beans.

          However, I'm not sure how you could ensure all beans in the pool were recycled this way. Perhaps you could have a timestamp field on your bean and use that to determine whether the instance needed to be removed.

          • 2. Re: Reloading stateless session beans programmatically in JB
            jamesstrachan Newbie

            There isn't a good way to reload stateless Session Beans, and there isn't meant to be.

            Suggestions :-

            a) Move the read only data from the Session Beans to a singleton Java class within the application Server. Reload data in the Singleton either when a timestamp expires or by touching the singleton from a servlet.

            b) Do something very similar using an Entity Bean with a single, constant key to hold the read only data. Refresh using the same techniques.

            You could use an Mbean to force reload in either alternative but a servlet will be quicker to develop and test.

            Code for alternative (a)

            public class ReadOnlyCache {
             private ReadOnlyCache mySingleton;
             private HashMap properties;
             private ReadOnlyCache() {}
             public ReadOnlyCache getInstance() {
             if ( mySingleton == null ) {
             mySingleton = new ReadOnlyCache();
             return mySingleton();
             public String getProperty( String key ) {
             return properties.get( key );
             public void refreshProperties() {
             private void loadProperties() {
             // Load the cached data from a properties file or whatever.

            This example is deliberately simplified but shows a possible pattern to use. The servlet (code omitted) just gets the singleton instance and calls refreshProperties().