Announcement Announcement Module
Collapse
No announcement yet.
WebApplicationContext init from MDB Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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

    Comment


    • #3
      Hi Alesj,

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

      regards,
      -Kristoffer

      Comment


      • #4
        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

        Comment


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

          Rgds, Ales

          Comment


          • #6
            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

            Comment


            • #7
              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

              Comment

              Working...
              X