Results 1 to 10 of 10

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

  1. #1
    Join Date
    Mar 2006
    Posts
    10

    Default 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. #2
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    I'm pretty sure I saw another post that talked about this a while ago, I don't think there were any suggestions however.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

  3. #3
    Join Date
    Nov 2005
    Location
    Reutlingen, Germany
    Posts
    2,098

    Default

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

    Joerg
    This post can contain insufficient information.

  4. #4
    Join Date
    Sep 2004
    Posts
    1,086

    Default

    Something like:

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

  5. #5
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    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.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

  6. #6
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    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]
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

  7. #7
    Join Date
    Mar 2006
    Posts
    10

    Default 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?

  8. #8
    Join Date
    Aug 2006
    Posts
    382

    Default 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.
    Greg L. Turnquist (@gregturn), SpringSource/VMware
    Project Lead: Spring Python and author of Spring Python 1.1 and Python Testing Cookbook.
    Listen to Pond Jumpers, the international podcast for open source developers.
    These comments are my own personal opinions, and do not reflect those of my company.

  9. #9

    Default

    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.

  10. #10
    Join Date
    Jun 2005
    Posts
    4,231

    Default

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •