1 Reply Latest reply on Feb 19, 2005 9:15 AM by jamesstrachan

    global config file design pattern

    kevliang

      hey guys,

      i know this topic has been visited a few times in this forum. i did a quick search and i have read through all of them. however, most seem to be pretty outdated (before 2002) and i would like to repost this topic to poll people's design pattern and choices.

      i m currently using the latest and greatest of jboss (4.0.1) and i would like to do the following:

      - read from a config datasource (could be a LDAP file or a db)
      - do this on start up, and cache it somehow since it will be accessed many times.
      - able to be referenced by any beans in jboss (my session beans, mainly) as well as my web-tier beans (servlets, etc).
      - read only, no edit needed



      the approach i have came up with (and this is open for critique, but be gentle.. =) is to have serviceMBean to start off by initializing a singleton class that will do the logic of loading the configuration file. i can then use this singleton anywhere and can be put into tree-cache if i end up clustering my jboss servers. but this solution doesnt seem elegant and i m not even sure if web-tier classes can access serviceMBean.

      my questions are as follows:
      1. is there a more elegant way to do this? i dont like singletons, especially in j2ee since they are pretty odd in a distributed, multi-apps environment. but it doesnt seem like there is any other way

      2. can i register this singleton as a jndi resource? that way, both ejb-tier and web-tier classes can access this singleton.

      3. can serviceMBean be access by web-tier classes? if so, then i can just make the serviceMBean the "singleton" class.

      4. what is the performance penality for accessing serviceMBean methods (since i will access these pretty often, throughout my code)?

      5. is there a more efficient way of caching these config values so that i can access these variables without too much performance penalty?

      if anyone can give me more direction/critique to my design, or can suggest a better one, that would be awesome.

      any comments are also welcome.

      thanks.

        • 1. Re: global config file design pattern
          jamesstrachan

          kevliang,

          I think that everything you suggest will work, except that I don't know whether you can register an mbean with JNDI.

          I have found it possible to use an entity bean as a singleton by defining a single constant value as the primary key. The constant value is defined somewhere in the package :-

          public static String CACHE_SINGLETON_KEY = "CacheControl";
          

          and then all other EJB and web applications can reference the singleton and access data through business methods.

          This works well and fits the J2EE model architecture. It strongly implies that your cached data is loaded from a database.

          The ejbLoad() method may be called before each business method is executed. It is probably advisable to use BMP and to reduce the number of database reads as shown below :-

           public void ejbLoad() {
          
           if ( System.currentTimeMillis() > cacheExpiryTime ) {
           doRealLoad();
           cacheExpiryTime += RELOAD_INTERVAL;
           }
          
           }
          

          where the private method doRealLoad() performs the actual work, and the constant RELOAD_INTERVAL is used to determine the interval between loads.

          Hope this helps.

          James