Java EE 8 By Example
  • Introduction
  • Overview
    • Example Codes
  • JSF 2.3
    • Activating CDI in JSF 2.3
    • Run applications in JSF 2.2 compatible mode
    • CDI alignment
    • CDI compatible @ManagedProperty
    • Inject support in Converter, Validator and Behavor
    • Websocket support
    • UIData improvements
    • Class level bean validation with f:valdiateWholeBean
    • Java 8 DateTime support
    • PostRenderViewEvent: publising events after view is rendered
    • Search expression framework
  • CDI 2.0
    • Java SE support
    • Event Priority
    • Async Events
    • Register Beans dynamicially
    • Configurators and Intercept Producers
  • JPA 2.2
    • Java 8 Datetime support
    • Return Stream based result from Query
    • More CDI Alignments
  • JSON-B 1.0
  • JSON-P 1.1
  • Bean Validation 2.0
  • JAXRS 2.1
    • Async improvements
    • Server Sent Event
    • Reactive Client
  • Java EE Security API 1.0
    • HttpAuthenticationMechanism
    • IdentityStore
    • SecurityContext
  • Servlet 4.0
    • Server Push
    • Runtime Discovery of Servlet Mappings
    • Http Trailer
  • MVC 1.0
    • Getting started with MVC
    • Handling form submission
    • Exception handling and form validation
    • Processing PUT and DELETE methods
    • Page navigation
    • MVC and CDI
    • Security
    • Bean parameter conversion
    • View engine
Powered by GitBook
On this page

Was this helpful?

  1. CDI 2.0

Async Events

CDI 2.0 add the capability of firing an CDI event asynchronously, this will free the client from long-awaited blocking request.

Create a simple backing bean.

@ViewScoped
@Named("eventBean")
public class EventBean implements Serializable {

    private static final Logger LOG = Logger.getLogger(EventBean.class.getName());

    @Inject
    Event<Message> event;

    private String message;

    private String notification;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getNotification() {
        return notification;
    }

    public void setNotification(String notification) {
        this.notification = notification;
    }


    public void fireEvent() {
        LOG.log(Level.INFO, "fire event async...");
        event.fireAsync(new Message(this.message))
                .thenAccept((m) -> this.setNotification(m+ " was sent")); 
    }

    public void fireEventOptions() {
        LOG.log(Level.INFO, "fire event async...");
        event.fireAsync(
                new Message(this.message),
                NotificationOptions.builder()
                        .set("weld.async.notification.timeout", 1000)
                        .build()
        );
    }
}

CDI 2.0 added two methods to fire a CDI event asynchronously, fireAsync will return a CompletionStage, it could be used in further steps, fireAsync has a variant which an extra NotificationOptions argument.

@ApplicationScoped
public class EventHandler implements Serializable {

    public static final Logger LOG = Logger.getLogger(EventHandler.class.getName());

    public void onMessage(@ObservesAsync Message message) {
        LOG.log(Level.INFO, "observes event:{0}", message);
    }
}

In the EventHandler, use @ObservesAsync to observe the incoming event.

PreviousEvent PriorityNextRegister Beans dynamicially

Last updated 4 years ago

Was this helpful?

Grab the from my GitHub account, and have a try.

source codes