Results 1 to 7 of 7

Thread: WebApplicationContext init from MDB

  1. #1
    Join Date
    Nov 2004
    Posts
    12

    Default WebApplicationContext init from MDB

    Hi guys,

    We have an j2ee application which have an entry point as an MDB. The application does not have a web application. I have tried to initalize the WebApplicationContext through the ClassPathXmlApplicationContext by passing it the path of applicationContext.xml, but it cant seem to find it. (This is done on setMessageDrivenContext() in the MDB, by the way) I have a deployable ejb jar which contains all the classes and i placed the xml file in the package structure so it should be accessible from classpath.

    I tried to use ClassPathXmlApplicationContext from a web application (placing applicationContext.xml in WEB-INF) and that works fine...

    I this a known issue or am i doing something wrong here? Maybe there is better way of initalizing the ApplicationContext from the ejb container?

    best regards,
    -Kristoffer

  2. #2
    Join Date
    Aug 2004
    Location
    Vrhnika, Slovenia
    Posts
    133

    Default

    You should probably be looking at something like this:
    - AbstractMessageDrivenBean
    - BeanFactoryLocator

    Look at both of this classes in Spring and how they are connected through 'loadBeanFactory()' method.

    Rgds, Ales

  3. #3
    Join Date
    Nov 2004
    Posts
    12

    Default

    Hi Alesj,

    Thanks for the quick answer. I have now implemented the MDB like the classes you described and everything works well!

    regards,
    -Kristoffer

  4. #4
    Join Date
    Nov 2004
    Posts
    12

    Default

    Hi again,

    I have done some load testing on the MDB and what i notice is that spring will instansiate itself for every bean? Is there a way of getting my "messageService" bean only once from spring and then just resuse it on every MDB instance? Or is it a bad idea?

    Here's some code from the MDB:

    Code:
    public static final String BEAN_FACTORY_PATH_ENVIRONMENT_KEY = "java:comp/env/ejb/BeanFactoryPath";
        private BeanFactoryLocator itsBeanFactoryLocator;
        private String itsBeanFactoryLocatorKey;
        private BeanFactoryReference itsBeanFactoryReference;
        private Service itsService;
    
        public void ejbCreate() throws EJBException
        {
            loadBeanFactory();
        }
    
        void loadBeanFactory() throws BeansException
        {
            if (this.itsBeanFactoryLocator == null)
            {
                this.itsBeanFactoryLocator = new ContextJndiBeanFactoryLocator();
            }
    
            if (this.itsBeanFactoryLocatorKey == null)
            {
                this.itsBeanFactoryLocatorKey = BEAN_FACTORY_PATH_ENVIRONMENT_KEY;
            }
    
            this.itsBeanFactoryReference = this.itsBeanFactoryLocator.useBeanFactory(this.itsBeanFactoryLocatorKey);
    
            ApplicationContext factory = (ApplicationContext) itsBeanFactoryReference.getFactory();
            this.itsService = (Service) factory.getBean("messageService");
        }
        public void ejbRemove() throws EJBException
        {
            if (this.itsBeanFactoryReference != null)
            {
                this.itsBeanFactoryReference.release();
                this.itsBeanFactoryReference = null;
            }
        }
    any sugesstions?

    regards,
    -Kristoffer

  5. #5
    Join Date
    Aug 2004
    Location
    Vrhnika, Slovenia
    Posts
    133

    Default

    This is what you are probably looking:
    http://www.springframework.org/docs/...ryLocator.html

    Rgds, Ales

  6. #6
    Join Date
    Nov 2004
    Posts
    12

    Default

    That one worked good aswell, thanks :-)

    I have just one question left...

    I want to add a servlet into my j2ee app. Right now im using the previous mentioned SingletonBeanFactoryLocator for the MDB and for the servlet i use the ContextLoaderListener the pick up the ApplicationContext from the ServletContext. That require me to put applicationContext.xml in two places, WEB-INF of the packaged war and in the ejb.jar.

    My question is,
    Is there a way of centralizing the handling of the applicationContext.xml? Ie, so i dont have to put the applicationContext in two diffrent jars?

    regards,
    -Kristoffer

  7. #7
    Join Date
    Aug 2004
    Location
    Vrhnika, Slovenia
    Posts
    133

    Default

    When using EJB container and web container you want to divide your workflow into bussines logic and presentation (3 or 4 tier app.). Where bussines runs inside EJB's and presentation in MVC framework. So normally you don't have one application context. Split your current application context into two - EJB dependant and web dependant. If you have a lot of common objects in your app. context used in both modules - sounds like a bad design to me 8) .

    Rgds, Ales

Similar Threads

  1. HttpServletBean init() question
    By rcui in forum Web
    Replies: 5
    Last Post: Oct 25th, 2006, 08:02 AM
  2. init method in HttpServletBean set as final
    By james_a_woods in forum Web
    Replies: 2
    Last Post: Jun 13th, 2005, 07:23 PM
  3. Replies: 8
    Last Post: Jun 9th, 2005, 10:02 AM
  4. Replies: 4
    Last Post: May 26th, 2005, 05:44 PM
  5. customized init method
    By novotny in forum Container
    Replies: 1
    Last Post: Aug 30th, 2004, 04:04 PM

Posting Permissions

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