4 Replies Latest reply on Jul 22, 2009 10:40 PM by Willy L

    Can an asynchronous method trigger an asychrnous event?

    Willy L Newbie

      Hi, I am very new to Seam. I have an asynchronous method which is long running process. I'm trying to use events.raiseAsynchronousEvent to trigger some asynchronous events in the middle of the process. But I'm not sure what scope my Observer component should be? I've tried page and session, it gives some error like seam component can not be created, scope is not active.



      Thanks,

        • 1. Re: Can an asynchronous method trigger an asychrnous event?
          Arbi Sookazian Master

          This is the relevant code from org.jboss.seam.Component class:


          private static Object getInstance(String name, boolean create, boolean allowAutoCreation, Object result) {
                Component component = Component.forName(name);
          
                create = create || (Init.instance().isAutocreateVariable(name) && allowAutoCreation);
          
                if (result==null && create)
                {
                  result = getInstanceFromFactory(name);
                  if (result==null)
                  {
                     if (component==null)
                     {
                        //needed when this method is called by JSF
                        if ( log.isTraceEnabled() ) log.trace("Seam component not found: " + name);
                     }
                     else if ( component.getScope().isContextActive() )
                     {
                        result = component.newInstance();
                     }
                     else
                     {
                        log.warn("Cannot create Seam component, scope is not active: " + name + "(" + component.getScope().name() + ")");
                     }
                  }
                }
          
                if (result!=null)
                {
                   if (component!=null)
                   {
                      if ( !component.isInstance(result) )
                      {
                         if ( component.hasUnwrapMethod() ) return result; ///best way???
                         throw new IllegalArgumentException( "value of context variable is not an instance of the component bound to the context variable: " + name  +
                                  ". If you are using hot deploy, you may have attempted to hot deploy a session or " +
                                  "application-scoped component definition while using an old instance in the session.");
                      }
                      result = component.unwrap(result);
                   }
                }
          
                return result;
          
             }




          Add a debug breakpoint at this line:


          result = getInstanceFromFactory(name);



          and find out why that method is returning null.

          • 2. Re: Can an asynchronous method trigger an asychrnous event?
            Willy L Newbie

            Thanks, I think I figured out why. The observer method tries to create a new instance of the component although it already exists in the session scope, but the asynchronous event observer can't see it because it's running a separate thread. 

            • 3. Re: Can an asynchronous method trigger an asychrnous event?
              Shervin Asgari Master

              The observer annotation also takes a boolean that will say that it should create component if it doesnt exist. But remember the observer will not run if you set auto-create to false and the component is not already created.

              • 4. Re: Can an asynchronous method trigger an asychrnous event?
                Willy L Newbie

                Shervin Asgari wrote on Jul 22, 2009 12:55:


                The observer annotation also takes a boolean that will say that it should create component if it doesnt exist. But remember the observer will not run if you set auto-create to false and the component is not already created.


                yeah, but it seems that when it comes to asynchronous event, when the auto-create is set to default(true), the observer component is created each time an event is raised, no matter whether it has been created before or not. When suto-create is set to false, the observer simply will not run even if the observer is created before. 


                Anyway, the exceptions I got is because I put the observer method inside a rather complicated component which retrieves some session information in the @create method. So I moved my observer method out to a simple component which can be created in separate threads many times.