On 03/18/2015 04:14 PM, Pete Muir
wrote:
I think I have misunderstood the async model that is being
proposed. We are proposing that all the observers of an event
fired asynchronously are all run on a single thread? (Not the
thread that fires the event, another thread, but the same thread
for each observer).
No, the current proposal is not that specific at this point do
define a particular architecture. The one you mentioned is one of
the possible options.
It has certain advantages:
- the caller of fireAsync() does not wait for observers to complete
so it is async for the caller
- observer notification works the same as way as we know today
except that in a different thread
- the common pattern of using mutable event payload on which
observers contribute would work fine - the payload does not need to
be thread-safe - it would be safely published to the notification
thread by the CDI implementation and there are no races between
observers
- ordering (if needed) fits well
Disadvantages
- it does not eliminate the backward-compatibility problem
completely (different thread means different transaction context,
CDI contexts and thread locals)
- observers are not called in parallel
Another option would be to call the observers in parallel
Advantages:
- the caller of fireAsync() does not wait for observers to complete
so it is async for the caller
- observers are independent of each other
Disadvantages:
- it does not eliminate the backward-compatibility problem
completely (different thread means different transaction context,
CDI contexts and thread locals)
- the payload needs to be thread-safe
- ordering would not work here
- single event firing possibly results in multiple exceptions
(tricky for the caller to handle with CompletionStage API)
Another option is to combine those two and by default call the
observers in parallel. If for some reason an observer depends on a
different observer (by means of priorities) then the container
guarantees that the partial ordering of observer notification is
met.
Advantages:
- the caller of fireAsync() does not wait for observers to complete
so it is async for the caller
- observers are independent of each other
- priority works the same in sync and async
Disadvantages:
- it does not eliminate the backward-compatibility problem
completely (different thread means different transaction context,
CDI contexts and thread locals)
- the payload needs to be thread-safe
- single event firing possibly results in multiple exceptions
(tricky for the caller to handle with CompletionStage API)
This would be a somewhat odd limitation in my mind.
It’s certainly *not* something that is semantically intuitive if
you have something like @Observes @Asynchronously on event
observers, which definitely implies the event observation is
asynchronous, not the event firing.
In the current proposal, @Observes @Asynchronously is just an
enabler (for backward compatibility) of async event firing. It alone
does not mean anything.
I would definitely prefer an asynchronous
observation model, which I can’t see as being remotely
compatible with observer ordering. The classic approach of using
callbacks to identify when a method is done is inherently more
powerful, and better understood.
So basically kind of EJB's @Asynchronous methods applied to
observers + a callback on event.fire()? The downside I can see here
is that the event.fire() caller never knows whether the call will
block or not whereas with event.fireAsync() it would be guaranteed
not to block.
Perhaps I’ve missed this, but what are the use cases
for wanting to specify that the observation is done async on the
observer? And how does this relate to the event firing methods
ability to get a callback? I suspect you would need to retrofit
the entire firing system to always return a callback if you
wanted to allow the observer to control whether it was async or
not.
I tend to agree with Jozef. It’s more
consistent for user to allow ordering in async event
as in sync.
Antoine
2015-03-18
11:03 GMT+01:00 Antoine Sabot-Durand <antoine@sabot-durand.net>:
From user pov it’s async :
"I fire an event and get the control
back right away". The impl will make an
observer queue in an other thread, but
the user exp is totally different than
classic fire().
this
is discussable since it is not for 2
observers. If you use async it can be - often
- cause of a long processing, if you have 2
long computing methods then you sequentiallize
it which breaks the reason why you wanted
async, no?
Yes it was already
discussed, there will be 2 signatures
for fireAsync().
_______________________________________________
cdi-dev mailing list
cdi-dev@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/cdi-dev
Note that for all code provided on this list, the
provider licenses the code under the Apache License,
Version 2
(
http://www.apache.org/licenses/LICENSE-2.0.html). For
all other ideas provided on this list, the provider
waives all patent and other intellectual property rights
inherent in such information.
_______________________________________________
cdi-dev mailing list
cdi-dev@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/cdi-dev
Note that for all code provided on this list, the provider licenses the code under the Apache License, Version 2 (http://www.apache.org/licenses/LICENSE-2.0.html). For all other ideas provided on this list, the provider waives all patent and other intellectual property rights inherent in such information.