Java EE 8: Sending asynchronous CDI 2.0 events with ManagedExecutorService

Java EE 8 with CDI 2.0 introduced asynchronous CDI Events. The Event interface was extended with the fireAsync method and an optional NotificationOptions parameter.

Now you can send events asynchronously, also using the injected ManagedExecutorService instance:

import java.time.LocalTime;
import java.util.concurrent.CompletionStage;
import javax.annotation.Resource;
import javax.ejb.Schedule;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.enterprise.concurrent.ManagedExecutorService;
import javax.enterprise.event.Event;
import javax.enterprise.event.NotificationOptions;
import javax.inject.Inject;

public class EventSource {

    Event<String> fireAlarm;
    ManagedExecutorService threadPool;

    @Schedule(second = "*/5", minute = "*", hour = "*")
    public void send() {
        String event = "fire " + System.currentTimeMillis();
        CompletionStage<String> completion = this.fireAlarm.
        fireAsync(event, NotificationOptions.ofExecutor(threadPool)); // returns immediately

    void eventDelivered(String event) {
        //...receipt after delivery
Asynchronous events are received with the @ObservesAsync annotation:

import java.time.LocalTime;
import javax.enterprise.event.ObservesAsync;

public class EventSink {

    public void onFireNotification(@ObservesAsync String event) {


See you at Java EE 8 on Java 9, at Munich Airport, Terminal 2

NEW online workshop: Web Components training (online)

Airport MUC workshops: Java EE 8, Java 9: Bootstrap, Effective, Architectures, Single Page Apps, Progressive Web Apps, HTML 5, ES 6, CSS 3 and Microservices

Podcast: and newsletter:

A book about rethinking Java EE Patterns


Post a Comment:
  • HTML Syntax: NOT allowed
Online Workshops
...the last 150 posts
...the last 10 comments