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

Marius Bogoevici (JIRA) jira-events at lists.jboss.org
Tue May 10 13:45:18 EDT 2011


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

Marius Bogoevici commented on CDI-4:
------------------------------------

I have a comment about the syntax.I quite like the idea of using @Before and @After coupled with qualifiers for ordering event handlers and IMO partial ordering can go a pretty long way in solving the issues that we have.

But, rather than using @Before and @After with the event qualifiers, I think that qualifiers should be placed on event handlers themselves.

So, this may work:
{code}
public void myFirstObserver(@Observes @After(Initialization.class) MyEvent event) {
...
}

public void mySecondObserver(@Observes @Initialization MyEvent event) {
...
} 
{code}

but what if I want to order two observers for @Initialization MyEvent ? I can do:
{code}
public void myFirstObserver(@Observes @Initialization @After(Initialization.class) MyEvent event) {
...
}

public void mySecondObserver(@Observes @Initialization MyEvent event) {
...
} 
{code} 
but that does not do a great favour to readability. On the other hand, what do we do with observers that do not have any qualifier at all? Mike suggested an @OrderingQualifier, but mixing it up with the rest of the qualifiers does not improve readability. On the other hand, ordering is relative to the method not to the event being observed, so why not qualify observer methods themselves (note how I stole the idea of placing the @Before annotation from Christian)? 
{code}
@Initialization
public void myFirstObserver(@Observes MyEvent event) {
...
}

@Before(Initialization.class)
public void mySecondObserver(@Observes  MyEvent event) {
...
} 
{code}

@Initialization can be a special type of annotation, or can be a simple @Qualifer. I also think that the idea can be reused in scenarios such as interceptor/decorator ordering, if we ever consider automatic interceptor/decorator enablement.




> 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