5 Replies Latest reply on Nov 27, 2007 4:39 PM by Siarhei Dudzin

    MDB initialized too early?

    Siarhei Dudzin Apprentice

      Hi All,

      I have a MDB that tries to use a stateless session bean (which is also a). And I see the following error:

      javax.ejb.EJBTransactionRolledbackException: java.lang.IllegalStateException: Attempted to invoke a Seam component outside the an initialized application
       at org.jboss.ejb3.tx.Ejb3TxPolicy.handleInCallerTx(Ejb3TxPolicy.java:87)
       at org.jboss.aspects.tx.TxPolicy.invokeInCallerTx(TxPolicy.java:130)
       at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:195)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:62)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.mdb.MessagingContainer.localInvoke(MessagingContainer.java:249)
       at org.jboss.ejb3.mdb.inflow.MessageInflowLocalProxy.delivery(MessageInflowLocalProxy.java:268)
       at org.jboss.ejb3.mdb.inflow.MessageInflowLocalProxy.invoke(MessageInflowLocalProxy.java:138)
       at $Proxy111.onMessage(Unknown Source)


      Now, I've found a solution here: http://www.jboss.org/?module=bb&op=viewtopic&t=100946

      But it looks like it's a vendor specific solution. Is there other (vendor independent) solution?

      Thanks in advance!

        • 1. Re: MDB initialized too early?
          Pete Muir Master

          Post your MDB and SLSB

          • 2. Re: MDB initialized too early?
            Siarhei Dudzin Apprentice

            Thanks for the quick response! Here are the sources (I had to 'censor' it for obvious reasons):

            MDB:

            @MessageDriven(name = "Processor", activationConfig = {
             @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
             @ActivationConfigProperty(propertyName = "destination", propertyValue = "myqueue"),
             @ActivationConfigProperty(propertyName = "providerAdapterJNDI", propertyValue="java:/TIBCOJMSProvider")
            })
            public class Receiver implements MessageListener {
            
             @Resource
             private MessageDrivenContext context;
            
             /**
             * Controller does the actual processing
             * of the incoming message
             */
             @EJB
             private Creator creator;
            
             /**
             * @see javax.jms.MessageListener#onMessage(javax.jms.Message)
             */
             public void onMessage(Message msg) {
             try {
             TextMessage txtMsg = (TextMessage)msg;
             String text = txtMsg.getText();
             creator.process(text);
             } catch (JMSException e) {
             e.printStackTrace();
             context.setRollbackOnly();
             } catch (ValidationException e) {
             e.printStackTrace();
             context.setRollbackOnly();
             }
             }
            
             /**
             * @param creator the controller to set
             */
             public void setCreator(Creator creator) {
             this.creator = creator;
             }
            
             /**
             * @param context the context to set
             */
             public void setContext(MessageDrivenContext context) {
             this.context = context;
             }
            
            }
            


            SLSB Local interface:
            @Local
            public interface Creator {
            
             /**
             * Processed an incoming message bus from a message bus
             * and stores it in database.
             *
             * @param text
             * @throws ValidationException
             * @return an instance of saved entity
             */
             public Entiteit process(String text) throws ValidationException;
            
             /**
             * Sets entity manager
             * @param em
             */
             public void setDatabase(EntityManager em);
            
             /**
             * Retrieves entity manager
             * @return entity manager instance
             */
             public EntityManager getDatabase();
            
            }



            SLSB itself:
            /**
             * Processes entities
             *
             */
            @Stateless
            @Name("Creator")
            public class CreatorBean implements Creator {
            
             private EntityManager em;
            
             /**
             * Sets entity manager
             * @param em
             */
             @PersistenceContext
             public void setDatabase(EntityManager em){
             this.em = em;
             }
            
             /**
             * Retrieves entity manager
             * @return entity manager instance
             */
             public EntityManager getDatabase() {
             return this.em;
             }
            
             /**
             * Processes the entities from xml and stores it in database.
             * It also performs checking on existing records end replaces them if found.
             * @throws ValidationException
             */
             public Entiteit process(String xmlText) throws ValidationException {
             Entiteit en = null;
            
             try {
             IBindingFactory bfact = BindingDirectory.getFactory(Entiteit.class);
             IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
            
             StringReader sr = new StringReader(xmlText);
            
             en = (Entiteit) uctx.unmarshalDocument( sr, null);
            
             em.persist(en);
            
            
             } catch (JiBXException e) {
             throw new ValidationException("Error processing incoming entity", e);
             }
            
             return en;
             }
            
            }
            


            • 3. Re: MDB initialized too early?
              Siarhei Dudzin Apprentice

              Is there a problem with the way I use it or should I create a JIRA issue?

              • 4. Re: MDB initialized too early?
                Alexander Torstling Newbie

                We are seeing this aswell. This particular problem also has one big implication: Since seam uses its own entity manager, and this affects caching etc, we have decided to use the seam managed entity manager in all DAO:s. Therefore, it is critical that seam is initialized before any component of the application is triggered. It seems like seam dependencies spread just like const-correctness :D

                Is there any other solution then to wait for seam to initialize in onMessage? This seems to work, but is a big hazzle, considering transaction timeouts etc.

                Regards