Results 1 to 3 of 3

Thread: Hierarchical application context in J2EE app

  1. #1
    Join Date
    Aug 2004
    Location
    Carlisle, UK
    Posts
    184

    Default Hierarchical application context in J2EE app

    I have a J2EE application (ear) with a web module, several jar modules, and a sar module (service archive - maybe specific to JBoss?).

    The sar module starts up a JBoss scheduled task, and has its own Spring xml config file to create its own application context.

    Currently this is quite separate from the web application context, which is created when the DispatcherServlet starts up from its own Spring xml config files.

    This works OK, but there is some duplication between the two application contexts, and in particular I have to duplicate some properties files for the two contexts, which is clearly a Bad Thing.

    I would like to configure it so the web context is a child of the sar context (the sar module starts up first - I'm not aware that that is configurable).

    How can I do that? How can I make the context created by the sar module available to the DispatcherServlet? (the only idea I had was to register it with JNDI, but I read in a thread that that's illegal).
    And supposing I can get at it from the DispatcherServlet, how do I configure that to use it as a parent context?

    Thanks
    Chris Harris
    Carlisle, UK

  2. #2
    Join Date
    Aug 2004
    Location
    u.s.a
    Posts
    399

    Default

    I have a similar challenge. I have to get it to work on both weblogic and websphere (and I still have hair on my head).

    Anyway, I'm trying to use ContextSingletonBeanFactoryLocator and follow Colin's ejbtest 'example' and other resources.

  3. #3
    Join Date
    Aug 2004
    Location
    Carlisle, UK
    Posts
    184

    Default

    I've got something that seems to do what I want. I'll post the relevant bits of code here in the hope that
    a) If it's not good practice someone can tell me; and
    b) If it's OK, it may be a useful example.

    The constructor of the schdulable object in my sar:
    Code:
        public OrderUnload()
        {
            // do it this way to make the beanFactory available to web modules:
            BeanFactoryLocator bfl = SingletonBeanFactoryLocator.getInstance();
            BeanFactoryReference bfr = bfl.useBeanFactory("org.stl.wo.appcontext");
    
            setUnloader((OrderUnloader)bfr.getFactory().getBean("orderUnloader"));
            //to prevent classloader problems on hot redeploy:
            bfr.release();
        }
    The DespatcherServlet for my web module:
    Code:
    public class CustomContextDispatcherServlet extends DispatcherServlet {
        BeanFactoryReference bfr = null;
    
        protected WebApplicationContext initWebApplicationContext() {
    
            ContextLoader cl = new MyContextLoader();
            WebApplicationContext wac = cl.initWebApplicationContext(this.getServletContext());
            getServletContext().setAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.woapp", wac);
            return wac;
        }
    
        public void destroy() {
            // to prevent classloader problems on hot redeploy:
            if (bfr != null) {
                bfr.release();
            }
        }
    
        class MyContextLoader extends ContextLoader {
            protected ApplicationContext loadParentContext(ServletContext servletContext) {
                BeanFactoryLocator bfl = SingletonBeanFactoryLocator.getInstance();
                bfr = bfl.useBeanFactory("org.stl.wo.appcontext");
                return (ApplicationContext)bfr.getFactory();
            }
        }
    }
    I found the calls to release() on the BeanFactoryReference to be necessary to prevent classloader problems on hot redeploy of the application.
    Chris Harris
    Carlisle, UK

Similar Threads

  1. Replies: 6
    Last Post: Sep 29th, 2005, 04:25 AM
  2. Loosing my SecureContext
    By sklakken in forum Security
    Replies: 3
    Last Post: Jul 21st, 2005, 01:44 PM
  3. Stack Overflow
    By rayho222 in forum Container
    Replies: 6
    Last Post: May 17th, 2005, 03:42 AM
  4. Replies: 6
    Last Post: May 8th, 2005, 11:09 AM
  5. Questioning the core component
    By Martin Kersten in forum Swing
    Replies: 6
    Last Post: Feb 21st, 2005, 03:45 AM

Posting Permissions

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