Announcement Announcement Module
Collapse
No announcement yet.
Is it possile to 'observe' the loading of an application context? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Is it possile to 'observe' the loading of an application context?

    Is there some sort of observer pattern implementation that will let me monitor the status of an application context while it is initialising all the beans?

    What would be a good start is if an application context, while it is being initialised, could report the number of beans defined in the context (XML file) early on, and then let me observe how many of those it has instantiated and initialised as it initialises them while it is starting up.

    I know there is the ApplicationListener in spring-context, but that only tells me when the whole comntext has finished loading

    -- Vihung

  • #2
    I'm pretty sure I saw another post that talked about this a while ago, I don't think there were any suggestions however.

    Comment


    • #3
      Since there is an ApplicationListener it seems everything is already "there", but I have no idea how you can really use it.

      Joerg

      Comment


      • #4
        Something like:

        Code:
        ctx = ClasspathApplicationcontext(ctxFiles, false);
        ctx.addListener(myListener);
        ctx.refresh();

        Comment


        • #5
          Acegi uses the listener approach and has quite a few examples, I'm not sure however that you are able to get the kind of information the original author wanted from it.

          Comment


          • #6
            This is all I get from it.
            24-Aug-2007 06:47:51 org.springframework.core.CollectionFactory <clinit>
            INFO: JDK 1.4+ collections available
            24-Aug-2007 06:47:51 org.springframework.beans.factory.xml.XmlBeanDefin itionReader loadBeanDefinitions
            INFO: Loading XML bean definitions from class path resource [simpleBean.xml]
            24-Aug-2007 06:47:52 org.springframework.context.support.AbstractRefres hableApplicationContext refreshBeanFactory
            INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlAp plicationContext;hashCode=3827495]: org.springframework.beans.factory.support.DefaultL istableBeanFactory defining beans [simpleBean]; root of BeanFactory hierarchy
            24-Aug-2007 06:47:52 org.springframework.context.support.AbstractApplic ationContext refresh
            INFO: 1 beans defined in application context [org.springframework.context.support.ClassPathXmlAp plicationContext;hashCode=3827495]
            24-Aug-2007 06:47:52 org.springframework.context.support.AbstractApplic ationContext initMessageSource
            INFO: Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMess ageSource@192b996]
            24-Aug-2007 06:47:52 org.springframework.context.support.AbstractApplic ationContext initApplicationEventMulticaster
            INFO: Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicatio nEventMulticaster@12a1e44]
            24-Aug-2007 06:47:52 org.springframework.beans.factory.support.DefaultL istableBeanFactory preInstantiateSingletons
            INFO: Pre-instantiating singletons in factory [org.springframework.beans.factory.support.DefaultL istableBeanFactory defining beans [simpleBean]; root of BeanFactory hierarchy]
            org.springframework.context.event.ContextRefreshed Event[source=org.springframework.context.support.ClassPa thXmlApplicationContext: display name [org.springframework.context.support.ClassPathXmlAp plicationContext;hashCode=3827495]; startup date [Fri Aug 24 06:47:51 BST 2007]; root of context hierarchy]

            Comment


            • #7
              ApplicationListener not enough

              I have a GUI, Swing-based, application (as opposed to a Web App or Applet) that uses a classpath application context. There are about 80 beans defined in it. Most of these beans are singletons and have init methods defined, and some of them take a long time to initialise.

              Since this is a client app, I would like to display some meaningful feedback to the user when they launch the applicaton. I am already showing a splash screen, but would like to show a progress bar or a status message saying 'Starting Up... n% Loaded" or something similar.

              As far as I can tell, the application context instatiates the beans serially - one after another. It occured to me that if I could a) query the application context for the total number of beans definied (up front - before they are initialised) and b) listen in on the progress of the beans being initialised by the context, I would be able to display such feedback.

              The ApplicationListener listens for all ApplicationEvents. However, the ApplicationContext only throws events on either the context being closed, or the context being refreshed. It does not notify when each bean is initialised.

              Does anyone know of a) a way in which the ApplicationEvent model could be used for getting notification on each bean being initialised, or b) any other way to achieve this?

              Comment


              • #8
                What about lazy initialization?

                One tricky thing is the fact that not necessarily every bean is going to get instantiated. I haven't read the code myself, but does Spring IoC itself know precisely how many beans are getting created before it embarks on that?

                Sounds like you might be able to get a signal every time a bean was created, but that wouldn't feed well into a status indicator if you don't have a total count. However, the fall back, is to show 1 loaded...2 loaded...3 loaded... like a warm fuzzy with no absolutes.

                Comment


                • #9
                  What if you created a BeanPostProcessor, registered it in the context which would have a static synchronized count of total beans instantiated, and would get invoked after the construction of each bean in the context?

                  http://static.springframework.org/sp...-extension-bpp

                  See their example for InstantiationTracingBeanPostProcessor.

                  Comment


                  • #10
                    That would work up to a point. I think the issue is that a BeanFactory can tell you how many BeanDefinitions it has, but not how many will be instantiated. You could get an estimate at least - probably good enough for a status indicator, as long as there aren't a huge number of uninitialised lazy beans at the end of the refresh.

                    Comment

                    Working...
                    X