Announcement Announcement Module
Collapse
No announcement yet.
MDB & Spring (Newbie question) Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • MDB & Spring (Newbie question)

    Hi,
    I have a rather large collection of spring beans in my application and I would like to use a MDB in such a way that only ONE instance of the applicationContext is loaded because memory is at a premium.

    I understand (from spring docs) the way to do this is to create a beanRef.xml which has references to all the context files and then to use something like

    BeanFactoryLocator bfl = SingletonBeanFactoryLocator.getInstance();
    BeanFactoryReference bf = bfl.useBeanFactory("<bean id from beanref.xml");


    and I can use bf.getBean() inside my MDB.

    Also, I create the MDB in the usual way. Is this the right approach? I do not want to create some pitfalls or break any "best practices" in the process.

    Thanks

  • #2
    You could benefit from extending Spring's AbstractMessageDrivenBean support class which loads the Spring context from a location as provided to JNDI. It is described here: http://static.springframework.org/sp...implementation

    Alternatively, you might even consider using a message-driven POJO instead (see: http://blog.interface21.com/main/200...-driven-pojos/). Then, the MDP *is* a Spring bean and can be fully configured with dependency injection.

    Comment


    • #3
      I have got it working, well almost. Need a little more help.

      I have setup a web project (where the spring beans are) and an EJB project where the MDB resides. I created the applicationContext.xml and the beanRefContext.xml in the source directory of the web project.

      The beanRefContext.xml simply points to the applicationContext file like this,

      Code:
      <beans>
       <bean id="MyApp" class="org.springframework.context.support.ClassPathXmlApplicationContext">
         <constructor-arg>
           <list><value>applicationContext.xml</value></list>
         </constructor-arg>
       </bean>
      </beans>
      And my MDB looks like this,

      Code:
      public void setMessageDrivenContext(MessageDrivenContext messageDrivenContext)	throws EJBException {
      		  super.setMessageDrivenContext(messageDrivenContext);
      		  setBeanFactoryLocator(ContextSingletonBeanFactoryLocator.getInstance());
      		  setBeanFactoryLocatorKey("MyApp");
      
      	}
      
      	public void onMessage(Message arg0) {
      		TextMessage msg=(TextMessage)arg0;
      		try {
      			System.err.println(" Receive "+msg.getText());
      			BeanFactory f=getBeanFactory();
      			GreetingServiceImpl b=(GreetingServiceImpl) f.getBean("GreetingServiceImpl");
      			System.err.println("Message from Spring Bean="+b.getMessage());
      		} catch (JMSException e) {
      			// TODO Auto-generated catch block
      			e.printStackTrace();
      		}
      
      	}
      When I send a message to the queue, I get this exception:
      Code:
       <MessageDrivenBean threw an Exception in onMessage(). The exception was:
       java.lang.reflect.InvocationTargetException.

      I have a a feeling my project structure is not quite right. Where should the XML files (context & ref) reside, in the EJB project or in the spring (web) project? Any other thoughts?

      Thanks

      Comment

      Working...
      X