Skip to content

Commit

Permalink
create API for simple asynchronous events support
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinesd committed Nov 26, 2014
1 parent bd3b141 commit 7a0c1d3
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
24 changes: 23 additions & 1 deletion api/src/main/java/javax/enterprise/event/Event.java
Expand Up @@ -18,7 +18,7 @@
package javax.enterprise.event;

import java.lang.annotation.Annotation;

import java.util.function.Consumer;
import javax.enterprise.util.TypeLiteral;

/**
Expand Down Expand Up @@ -98,6 +98,28 @@ public interface Event<T> {
*/
public void fire(T event);

/**
* <p>
* Fires an event with the specified qualifiers and notifies observers asynchronously.
* </p>
*
* @param event the event object
* @throws IllegalArgumentException if the runtime type of the event object contains a type variable

This comment has been minimized.

Copy link
@gastaldi

gastaldi Apr 29, 2015

Contributor

How about renaming this method to fireAsync for simplification purposes?

*/
public void fireAsynchronous(T event);

/**
* <p>

This comment has been minimized.

Copy link
@mkouba

mkouba Nov 27, 2014

Contributor

If any observer method throws exception the callback will not be called, right? And what about transactional observers?

This comment has been minimized.

Copy link
@antoinesd

antoinesd Nov 27, 2014

Author Contributor

@mkouba As you know this is only an API POC. We have to discuss all the spec aspect on the ML or in the event doc workshop.
To answer your point, we should perhaps introduce a callback for failed observers and as already discussed don't support transactional behaviour in asynchronous observers.

This comment has been minimized.

Copy link
@mkouba

mkouba Nov 27, 2014

Contributor

Yep, I'm just curious. Thanks for replies :-)

This comment has been minimized.

Copy link
@jharting

jharting Dec 3, 2014

Contributor

We should consider reusing https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletionStage.html or introducing something similar.
That not only gives you the ability to register callbacks for different cases but also allows multiple completion stages to be bound together (monads).

This comment has been minimized.

Copy link
@jharting

jharting Dec 3, 2014

Contributor

As for transactional observes this is what is done in sync dispatch:

  1. Transactional observers are registered with the transaction
  2. Non-transactional observers are executed

For asynchronous dispatch I therefore find it natural to:

  1. Register transactional observers with transaction
  2. Schedule non-transactional observers to be executed in a threadpool

In other words, transactional observers should IMO not be ignored in async dispatch.

This comment has been minimized.

Copy link
@jharting

jharting Dec 3, 2014

Contributor
* Fires an event with the specified qualifiers and notifies observers asynchronously.
* After all observers have been called the container perform the provided callback code
* </p>
*
* @param event the event object
* @param callback a callback code to be called after all observers
* @throws IllegalArgumentException if the runtime type of the event object contains a type variable
*/
public void fireAsynchronous(T event, Consumer<T> callback);

/**
* <p>
* Obtains a child <tt>Event</tt> for the given additional required qualifiers.
Expand Down
31 changes: 30 additions & 1 deletion api/src/main/java/javax/enterprise/inject/spi/BeanManager.java
Expand Up @@ -21,7 +21,7 @@
import java.lang.reflect.Type;
import java.util.List;
import java.util.Set;

import java.util.function.Consumer;
import javax.el.ELResolver;
import javax.el.ExpressionFactory;
import javax.enterprise.context.ContextNotActiveException;
Expand Down Expand Up @@ -228,6 +228,35 @@ public interface BeanManager {
*/
public void fireEvent(Object event, Annotation... qualifiers);

/**
* Fire an event and notify observers asynchronously.
*
* @param event the event object
* @param qualifiers the event qualifiers
* @throws IllegalArgumentException if the runtime type of the event object contains a type variable
* @throws IllegalArgumentException if two instances of the same qualifier type are given
* @throws IllegalArgumentException if an instance of an annotation that is not a qualifier type is given
* @throws IllegalArgumentException if the runtime type of the event object is assignable to the type of a container
* lifecycle event
*/
public void fireAsynchronousEvent(Object event, Annotation... qualifiers);


/**
* Fire an event and notify observers asynchronously.
*
* @param event the event object
* @param callback a callback code to be called after all observers
* @param qualifiers the event qualifiers
* @throws IllegalArgumentException if the runtime type of the event object contains a type variable
* @throws IllegalArgumentException if two instances of the same qualifier type are given
* @throws IllegalArgumentException if an instance of an annotation that is not a qualifier type is given
* @throws IllegalArgumentException if the runtime type of the event object is assignable to the type of a container
* lifecycle event
*/
public void fireAsynchronousEvent(Object event, Consumer callback, Annotation... qualifiers);


/**
* Return the set of observers for an event.
*
Expand Down
Expand Up @@ -59,4 +59,10 @@ public interface EventMetadata {
* Get the type representing runtime class of the event object with type variables resolved.
*/
public Type getType();

/**
*
* @return true if the event was fired asynchronously
*/
public boolean isAsynchronous();
}

0 comments on commit 7a0c1d3

Please sign in to comment.