2 Replies Latest reply on Dec 27, 2011 10:51 AM by edwardpig

    Caching objects

    edwardpig

      I've written a proof-of-concept session bean which wraps some legacy Java code.  The POC seems to be doing what we want it to do, and now it's time to make the bean production-ready.

       

      Each instance of the bean loads configuration from an XML document.  There are several dozen XML documents available; the decision about which XML document is loaded depends on what we want the particular bean instance to do.

       

      For the POC, I have simply dropped the XML documents in the servlet 'WEB-INF' directory.  At runtime, my POC servlet obtains the appropriate XML document from the ServletContext as an InputStream, and hands the InputStream to the session bean.  However, the XML documents in question can be up to a few MB in size, so we don't want to parse them repeatedly.  I think there are two requirements for how we would like to handle things:

       

      1) Load and parse each XML document into an in-memory DOM tree at startup, and retain that DOM tree collection in memory 'forever' (until JBoss shuts down).

       

      2) These XML documents rarely change --- in fact, they may never change --- but it should still be possible to update them if necessary.

       

      So one question I have is: what is the best way to achieve the loading at startup and in-memory caching?  For my POC, I put a static Map in my bean which held references to each DOM, but that's not optimal because the XML isn't parsed at JBoss startup/bean deployment, and the static Map goes away if the bean ClassLoader is ever garbage collected.

       

      Another question I have is: Where should the source XML documents reside?  I don't think delivering them in the WEB-INF directory of my web app is a good idea, because I want to be able to update the XML without redeploying the web app.  Perhaps I should parse each DOM tree, then store the whole tree as a CLOB in my database?

       

      Suggestions are appreciated.

        • 1. Re: Caching objects
          wdfink

          Hi,

           

          with EJB3.1 I would use a SingletonStatelessSessionBean with a @Startup annotation [1] to cache the parsed XML.

          The XML should be placed in a DB as CLOB or VARCHAR, this will be also better if you cluster the application.

          Refreshing might be done by calling the SLSB or you store a checksum/version/date with the XML and let the SLSB check this by using @Schedule

           

           

          [1] http://java.sun.com/developer/technicalArticles/JavaEE/JavaEE6Overview_Part3.html

          • 2. Re: Caching objects
            edwardpig

            Thanks for the link.  All of this sounds wonderful, but my bean needs to be deployed in JBoss 5.1.0, which does not appear to be EJB 3.1-compliant.  I have tried to work with the @Singleton annotation, but perhaps there is no way to use it in an EJB 3.0 setting.  It seems that no matter what I do, I end up with an error similar to the following:

             

            DEPLOYMENTS MISSING DEPENDENCIES:

              Deployment "jboss.j2ee:ear=scpoc-0.1.ear,jar=scpoc-ejb.jar,name=ScoreComputerReportBridge,service=EJB3" is missing the following dependencies:

                Dependency "<UNKNOWN jboss.j2ee:ear=scpoc-0.1.ear,jar=scpoc-ejb.jar,name=ScoreComputerReportBridge,service=EJB3>" (should be in state "Described", but is actually in state "** UNRESOLVED Demands 'Class:com.pearson.assessments.qlocal.XMLLoader' **")

              Deployment "jboss.j2ee:ear=scpoc-0.1.ear,jar=scpoc-ejb.jar,name=ScoreComputerReportBridge,service=EJB3_endpoint" is missing the following dependencies:

                Dependency "jboss.j2ee:ear=scpoc-0.1.ear,jar=scpoc-ejb.jar,name=ScoreComputerReportBridge,service=EJB3" (should be in state "Configured", but is actually in state "PreInstall")

             

            DEPLOYMENTS IN ERROR:

              Deployment "<UNKNOWN jboss.j2ee:ear=scpoc-0.1.ear,jar=scpoc-ejb.jar,name=ScoreComputerReportBridge,service=EJB3>" is in error due to the following reason(s): ** UNRESOLVED Demands 'Class:com.pearson.assessments.qlocal.XMLLoader' **

             

            Any suggestions?