8 Replies Latest reply on Apr 5, 2008 9:28 AM by Daniel Hinojosa

    No application context active

    Tony Mai Apprentice

      I run into an exception with No application context active. I think it caused by my JMS subscriber trying to gain access to a Seam component when a new JMS message arrives.



      @Scope(ScopeType.APPLICATION)
      @Name("tdOrdersListener")
      @Startup(depends={"myComp"})
      public class OrdersListenerAlt {
          @In
          private MyComp myComp;
      
          private class ExListener implements MessageListener {
           @Override
           public void onMessage(Message msg) {
                  ...
                  if (myComp == null) {
                      myComp = (MyComp) Contexts.getSessionContext().get("myComp"); //This code threw 'No application context active'
                      myComp = (MyComp) Component.getInstance("myComp", true); // Still won't work
                  }
              }
          }



      How can I create/obtain an application context manually? Is there a different way?


      I am running Seam 2.0.0GA under JBoss 4.2.1GA.


      Regards,
      tony

        • 1. Re: No application context active
          Daniel Hinojosa Master
          MyComp myComp = (MyComp) Contexts.getApplicationContext().get("myComp");
          



          I haven't done JMS stuff in Seam yet (I will this week). But I know Contexts have a getApplicationContext method

          • 2. Re: No application context active
            Tony Mai Apprentice

            That too was not available (Contexts.getApplicationContext() returned null).

            • 3. Re: No application context active
              Daniel Hinojosa Master

              1. What does MyComp look like?
              2. Is it installed during deployment or explosion (as seen in the app server console)?
              3. If MyComp doesn't have configuration annotations, how are they configured in components.xml?

              • 4. Re: No application context active
                Tony Mai Apprentice

                MyComp is defined as followed:


                @Scope(ScopeType.APPLICATION)
                @Name("myComp")
                @Startup
                public class MyComp implements Serializable {
                ...
                   @Create
                   public void init() {
                   ...
                   }
                

                • 5. Re: No application context active
                  Daniel Hinojosa Master

                  http://jira.jboss.org/jira/browse/JBIDE-1574


                  Is there room for redesign and see if that works?  Supposedly there is a fix for the latest release.

                  • 6. Re: No application context active
                    Tony Mai Apprentice

                    Refactored and still Contexts.getApplicationContext() returned null and Component.getInstance(myComp, true) threw No application context active.


                    I think I still need to somehow tap onto Seam's application context. Unfortunately org.jboss.seam.contexts.ApplicationContext does not offer such getInstance() method.


                    My only reason to create this JMS listener was that I could not get my MDB to work with remote queue (see my other post on Seam MDB with remote topic). With plain JMS, I was able to get the messages. Yet my MDB just sit there idling. :(


                    Any idea?


                    Thanks

                    • 7. Re: No application context active
                      Richard Ombati Newbie

                      To get the contexts try:



                      import org.jboss.seam.contexts.Lifecycle;





                      boolean createContexts = !Contexts.isEventContextActive() && !Contexts.isApplicationContextActive();
                                      if (createContexts) {
                                          Lifecycle.beginCall();
                                      }
                      



                      • 8. Re: No application context active
                        Daniel Hinojosa Master

                        Something strange, I tried it out and I had to make my MDB as stateless. If it truly is an MDB. So you wouldn't make a lot of sense for an application scope MDB. Without having it as an MDB, all gets injected fine and it works.


                        The code I used is yours with some necessities.


                        @Scope(ScopeType.APPLICATION)
                        @Name("myComp")
                        @Startup
                        public class MyComp implements Serializable {
                        
                            @Create
                            public void init() {
                        
                            }
                        }
                        



                        and this is what I changed, as you can see an application scoped and stateless bean really doesn't make sense.


                        import org.jboss.seam.Component;
                        import org.jboss.seam.ScopeType;
                        import org.jboss.seam.annotations.In;
                        import org.jboss.seam.annotations.Name;
                        import org.jboss.seam.annotations.Scope;
                        import org.jboss.seam.annotations.Startup;
                        import org.jboss.seam.contexts.Contexts;
                        
                        import javax.jms.Message;
                        import javax.jms.MessageListener;
                        import javax.ejb.MessageDriven;
                        import javax.ejb.ActivationConfigProperty;
                        import javax.ejb.Stateless;
                        
                        @Stateless
                        @Scope(ScopeType.APPLICATION)
                        @Name("tdOrdersListener")
                        @Startup(depends = {"myComp"})
                        @MessageDriven(activationConfig = {
                                @ActivationConfigProperty(propertyName = "destinationType",
                                        propertyValue = "javax.jms.Queue"),
                                @ActivationConfigProperty(propertyName = "destination",
                                        propertyValue = "queue/tolltag") })
                        public class OrdersListenerAlt implements MessageListener {
                            @In
                            private MyComp myComp;
                        
                            public MyComp getMyComp() {
                                return myComp;
                            }
                        
                            public void setMyComp(MyComp myComp) {
                                this.myComp = myComp;
                            }
                        
                            @Override
                            public void onMessage(Message msg) {
                                if (myComp == null) {
                                    myComp = (MyComp) Contexts.getSessionContext().get("myComp"); //This code threw 'No application context active'
                                    myComp = (MyComp) Component.getInstance("myComp", true); // Still won't work
                                }
                            }
                        }