Announcement Announcement Module
Collapse
No announcement yet.
Cannot get Spring bean in MDB Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Cannot get Spring bean in MDB

    Hi

    I have a MDB and I get an exception, when I try useing a bean method Spring bean method in onMessage() method.

    At first I setBeanFactoryLocator and setBeanFactoryLocatorKey in setMessageDrivenContext() method and in the onMessage() method, when I call the getBeanFactory(), I get a nullPointerException.

    Here is my MDB:

    Code:
    public class CreateContactMDBBean extends AbstractJmsMessageDrivenBean {
    
            private static final long serialVersionUID = -5433833960999988768L;
    	private ContactServiceImpl contactService; //the Spring bean
    
    	@Override
    	public void setMessageDrivenContext(MessageDrivenContext messageDrivenContext){
    		super.setMessageDrivenContext(messageDrivenContext);
    		setBeanFactoryLocator(ContextSingletonBeanFactoryLocator.getInstance());
    		setBeanFactoryLocatorKey("applicationContext");
    		
    	}
    
    	public void onMessage(Message msg) {
    		
    		System.out.println("------ Message Recieved -------");
    		
    		BeanFactory f=getBeanFactory(); //line #51
    		contactService = (ContactServiceImpl) f.getBean("contactServiceTarget");
    		
    
    		Document document;
    		String firstname, lastname, email;
    		
    		try {
    			//Get firstname, lastname and email from message
    
    			Contact c = new Contact();
    			c.setFirstname(firstname);
    			c.setLastname(lastname);
    			c.setEmail(email);
    			
    			contactService.insertNewContact(c);
    			
    			
    		} catch (JMSException e) {
    			System.out.println("JMSException:");
    			e.printStackTrace();
    		}
    	}
            // empty onEjbCreate() and ejbRemove()
    }
    In my classpath there is beanRefContext.xml that has a bean named "applicationContext".

    Here is the exception:

    Code:
    [7.02.07 13:13:52:906 EET] 0000003a SystemOut     O JMS producer starts sending the message
    [7.02.07 13:13:52:921 EET] 0000003a SystemOut     O JMS producer has finnished sending the message
    [7.02.07 13:13:52:953 EET] 0000003f SystemOut     O ------ Message Recieved -------
    [7.02.07 13:13:52:953 EET] 0000003f ExceptionUtil E   CNTR0020E: EJB threw an unexpected (non-declared) exception during invocation of method "onMessage" on bean "BeanId(CommonSampleEar#CommonSampleBusiness.jar#CreateContactMDB, null)". Exception data: java.lang.NullPointerException
    	at org.springframework.ejb.support.AbstractEnterpriseBean.getBeanFactory(AbstractEnterpriseBean.java:154)
    	at com.tietoenator.sample.fw.ejb.jms.CreateContactMDBBean.onMessage(CreateContactMDBBean.java:51)
    	at com.ibm.ejs.container.MessageEndpointHandler.invokeMdbMethod(MessageEndpointHandler.java:992)
    	at com.ibm.ejs.container.MessageEndpointHandler.invoke(MessageEndpointHandler.java:725)
    	at $Proxy10.onMessage(Unknown Source)
    	at com.ibm.ws.sib.api.jmsra.impl.JmsJcaEndpointInvokerImpl.invokeEndpoint(JmsJcaEndpointInvokerImpl.java:201)
    	at com.ibm.ws.sib.ra.inbound.impl.SibRaDispatcher.dispatch(SibRaDispatcher.java:708)
    	at com.ibm.ws.sib.ra.inbound.impl.SibRaSingleProcessListener$SibRaWork.run(SibRaSingleProcessListener.java:584)
    	at com.ibm.ejs.j2c.work.WorkProxy.run(WorkProxy.java:497)
    	at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1510)
    Additional information about that nullpointer exception from WebSphere logs:
    Code:
    ------Start of DE processing------ = [7.02.07 12:27:25:421 EET] , key = java.lang.NullPointerException com.ibm.ejs.container.MessageEndpointHandler.invokeMessageListenerMethod 1010
    Exception = java.lang.NullPointerException
    Source = com.ibm.ejs.container.MessageEndpointHandler.invokeMessageListenerMethod
    probeid = 1010
    Stack Dump = java.lang.NullPointerException
    	at com.tietoenator.sample.fw.ejb.jms.CreateContactMDBBean.onMessage(CreateContactMDBBean.java:70)
    	at com.ibm.ejs.container.MessageEndpointHandler.invokeMdbMethod(MessageEndpointHandler.java:992)
    	at com.ibm.ejs.container.MessageEndpointHandler.invoke(MessageEndpointHandler.java:725)
    	at $Proxy3.onMessage(Unknown Source)
    	at com.ibm.ws.sib.api.jmsra.impl.JmsJcaEndpointInvokerImpl.invokeEndpoint(JmsJcaEndpointInvokerImpl.java:201)
    	at com.ibm.ws.sib.ra.inbound.impl.SibRaDispatcher.dispatch(SibRaDispatcher.java:708)
    	at com.ibm.ws.sib.ra.inbound.impl.SibRaSingleProcessListener$SibRaWork.run(SibRaSingleProcessListener.java:584)
    	at com.ibm.ejs.j2c.work.WorkProxy.run(WorkProxy.java:497)
    	at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1510)
    
    Dump of callerThis = 
    Object type = com.ibm.ejs.container.MessageEndpointHandler
    com.ibm.ejs.container.MessageEndpointHandler@26962696
    
    ==> Performing default dump from com.ibm.ejs.container.EJBContainerDiagnosticModule  = Wed Feb 07 12:27:25 EET 2007
    >EJB DM: Object array. = null
    Dump of EJBContainer Internals = 
    Object type = com.ibm.ejs.container.EJSContainer
    com.ibm.ejs.container.EJSContainer@10ea10ea
    
    +Data for directive [default] obtained. = 
    Dump of ComponentMetaData = 
                                     *** START ComponentMetaData fields ***
                                     J2EEName       = CommonSampleEar#CommonSampleBusiness.jar#CreateContactMDB
                                     EJB Type       = MESSAGE_DRIVEN
                                     TX Type        = CONTAINER_MANAGED_TX
                                     Module Version = EJB 2.1
                                         ******* ResRefList ******* 
                                     ResRefList size=0
                                           ****** LOCAL-TRANSACTION *******
                                     Boundary=BEAN_METHOD
                                     Resolver=APPLICATION
                                     UnResolvedAction=ROLLBACK
                                     *** END ComponentMetaData fields  ***
    +Data for directive [defaultcomponentmetadata] obtained. = 
    Dump of ContainerTx = 
    
    *** Start ContainerTx Dump    ---> ContainerTx@28b428b4#tid=1
    
      Tx Key                  = com.ibm.ws.Transaction.JTA.TransactionImpl@35d835d8#tid=1
      State                   = Active
      Entered beforCompletion = false
      Marked Rollback Only    = false
      Method Began            = true
      Isolation Level         = TRANSACTION_NONE
      Callback BeanO          = MessageDrivenBeanO(BeanId(CommonSampleEar#CommonSampleBusiness.jar#CreateContactMDB, null), state = IN_METHOD)
    
      --- Dump of Enlisted Beans : 0 ---------------------- 
    
    
      --- Dump of Access Intents : 0 ---------------------- 
    
    *** ContainerTx Dump Complete ---> ContainerTx@28b428b4#tid=1
    
    +Data for directive [defaultcontainertx] obtained. = 
    ==> Dump complete for com.ibm.ejs.container.EJBContainerDiagnosticModule  = Wed Feb 07 12:27:25 EET 2007
    Last edited by Jux; Feb 7th, 2007, 06:44 AM.

  • #2
    OK I solved my problem. I used the AbstractJmsMessageDrivenBean wrong.
    My IDE generated an empty ejbCreate() method that overrided the one present in the superclass, so if anyone else faces this problem, make sure you override only 2 methods:

    1. setMessageDrivenContext
    2. onEjbCreate

    Comment


    • #3
      Yes, this is a nasty gotcha. The ideal approach would be for EJB lifecycle methods that Spring needs to use to be marked as final to prevent accidental overriding subverting Spring's workflow. However the EJB specification does not allow this, as EJB containers can choose to do everything by subclassing. Thus sadly we are powerless to prevent this problem...

      Comment


      • #4
        Is it possible to write an AspectJ aspect which enforces this? I understand people don't compile their code with AspectJ often but perhaps they should. I'm sure Ramnivas would back me up on this one

        Comment

        Working...
        X