[cdi-dev] [JBoss JIRA] Commented: (CDI-4) Need a way to provide ordering for Event observers (@Observes)

Aslak Knutsen (JIRA) jira-events at lists.jboss.org
Mon May 9 09:54:18 EDT 2011

    [ https://issues.jboss.org/browse/CDI-4?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12600691#comment-12600691 ] 

Aslak Knutsen commented on CDI-4:

In Arquillian Core we had the same issue. 

Previously multiple Observers were observing the BeforeClass, and the ordered mattered:

- ContextCreator (sets up the context so the TestClass can be found)
-- Listen to BeforeClass
- DeploymentGenerator (use the TestClass to generate the Archive)
-- Listen to BeforeClass
- ArchiveExporter (Export the Archive)
-- Listen to BeforeClass
- Deployer (Deploy the Archive)
-- Listen to BeforeClass

>From my perspective, if Order matters, they have a coupling so why not show it directly. 

Two features were added to help with this: 

- Event interceptor
If you need to setup/process a event indirectly, e.g. setup some evn, then you can Intercept the event in a AroundInvoke style interceptor. You can then Observe EventContext<BeforeClass>, setup your 'stuff' and continue the chain with event.proceed() to invoke the next Interceptors or Observers.

- Nested Events
Instead of all coupled Observers, based on order, being registered on the same Event, use 'sub events' to limit the scope and show the coupling. DeploymentGenerator fires a DeploymentGenerated event, Deployer fires BeforeDeploy and AfterDeploy events etc.

The new observer chain then becomes:

- ContextCreator
-- Listen to EventContext<BeforeClass> - Around Invoke interceptor of the whole Event
- DeploymentGenerator 
-- Listen to BeforeClass -> fire DeploymentGenerated
- Deployer
-- Listen to DeploymentGenerated -> fire BeforeDeploy & AfterDeploy
- ArchiveExporter
-- Listen to BeforeDeploy

This has the added bonus that 'others' can reuse some of the chains logic by firing more concrete events, e.g. Trigger only a deployment by firing a DeploymentGenerated event instead of having the other side effects of the more coarse grained BeforeClass event.

Just wanted to throw the idea of Intercepting a Event and not the Observer out there, just my 2 cents.

> Need a way to provide ordering for Event observers (@Observes)
> --------------------------------------------------------------
>                 Key: CDI-4
>                 URL: https://issues.jboss.org/browse/CDI-4
>             Project: CDI Specification Issues
>          Issue Type: Feature Request
>          Components: Events, Portable Extensions
>    Affects Versions: 1.0
>         Environment: All
>            Reporter: Lincoln Baxter III
>            Assignee: Mike Brock
>             Fix For: TBD
> There needs to be a way to specify some kind of ordering for Event observers. 
> Understandably, this is somewhat counter-intuitive to the general concept of observing an event, but there is going to be need for this in an upcoming JBoss project. While it can be done manually, it might be nice to have a built-in API.

This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

More information about the cdi-dev mailing list