How do I listen to all Seam contextual events with parameterized names?

那年仲夏 提交于 2020-01-04 05:23:06

问题


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:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!