views:

664

answers:

2

Seam will fire different kinds of events that relate to particular scopes, tasks, or processes and appends the name of the scope, task or process to the end of the event.

How do I listen to all the events of a type?

E.g. for any <name> I'd like to listen to events such as these:

  • org.jboss.seam.createProcess.<name> — called when the process is created
  • org.jboss.seam.endProcess.<name> — called when the process ends
  • org.jboss.seam.initProcess.<name> — called when the process is associated with the conversation
  • org.jboss.seam.startTask.<name> — called when the task is started
  • org.jboss.seam.endTask.<name> — called when the task is ended

I need to do this despite not knowing the list of valid names up front... :-(

I hope to be using @Observer to create the observer, or something similar, and I'll listen to up to two event classes in the same component.

+1  A: 

You can easily do this by replacing Seam's Events class with your own implementation. Then look for events that are raised that start with a particular string:

@Scope(ScopeType.STATELESS)
@BypassInterceptors
@Name("org.jboss.seam.core.events")
@Install(precedence=APPLICATION)
public class Events extends org.jboss.seam.core.Events
{
   @Override
   public void raiseEvent(String type, Object... parameters )
   {
       super.raiseEvent( type, parameters );

       if ( type.startsWith( "org.jboss.seam.createProcess" ) )
       {
           super.raiseEvent( "org.jboss.seam.createProcess", parameters );
       }
       //etc.
   }
}

You can now observe "org.jboss.seam.createProcess" to get all createProcess events.

mtpettyp
+1  A: 

Inside the if, you must write super.raiseEvent(...) otherwise you'll get an infinite loop.

Thanks! I've updated my answer
mtpettyp
Voted down, may seem harsh but it pains me to see a new(ish) user getting rep for posting an answer that should have been a comment.
Simon Gibbs
Yes this should have been a comment
Shervin