问题
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 createdorg.jboss.seam.endProcess.<name>
— called when the process endsorg.jboss.seam.initProcess.<name>
— called when the process is associated with the conversationorg.jboss.seam.startTask.<name>
— called when the task is startedorg.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:
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.
回答2:
Inside the if, you must write super.raiseEvent(...) otherwise you'll get an infinite loop.
来源:https://stackoverflow.com/questions/553157/how-do-i-listen-to-all-seam-contextual-events-with-parameterized-names