Over a million developers have joined DZone.

JSR 365 Update: Digging Into CDI 2.0

DZone 's Guide to

JSR 365 Update: Digging Into CDI 2.0

CDI 2.0 is coming with support for Java SE, observer ordering, and asynchronous events. Let's look at the particularly interesting asynchronous events part.

· Java Zone ·
Free Resource

Contexts and Dependency Injection 2.0 (JSR 365), is an update to CDI 1.2, which is currently part of the Java EE 7 platform. It is currently in Public Review stage. For those of you who are not very familiar with CDI, it defines a powerful set of complementary services that act as a gel that helps to improve the coordination and structure of application code. For more details, please visit the specification page.

CDI 2.0 expands the usability of the Contexts and Dependency Injection services to Java SE as well, as it will now target both the Java SE and Java EE platforms. The CDI specification has been organized into three parts, Part I - Core CDI, Part II - CDI in Java SE, and Part III - CDI in Java EE. The major changes for CDI 2.0 are as follows:

  • Better alignment with Java SE 8
  • API for booting CDI within a Java SE application
  • Ability to provide observer ordering
  • Ability to fire asynchronous events
  • New Configurators Interfaces for Service Provider Interface (SPI) elements
  • Ability to configure or veto an observer method in ProcessObserverEvent event
  • Support for inline instantiation of specific annotation types
  • Addition of the InterceptionFactory interface, which allows us to create a wrapper instance whose method invocations are intercepted by method interceptors and forwarded to a provided instance.

I encourage you to take a look at the CDI 2.0 Public Review Draft for more details on the specifics of each enhancement listed, as well as a complete listing of new features. Read through the draft and provide feedback to the expert group. All of the pertinent details for providing feedback can be found on the JSR 365 page. To get started testing, create a new maven-based Java EE application in your favorite IDE, and add the following dependencies:


In this post, we will dig into one of the new features to get you started working with the API. Let's take a look at asynchronous events. Until CDI 2.0, events could only be fired in a synchronous manner.  They've been enhanced in this latest iteration for asynchronous processing. Here's how it works:

  1. Create an event of some type.

  2. Fire the event in an asynchronous manner.

  3. Handle accordingly once the event is complete.

In this example, I have created a MovieEvent class, which will be utilized whenever a persist event occurs for a Movie object. The MovieEvent class is as follows:

public class MovieEvent {
    private String message;
    private Movie movie;

    public MovieEvent(String message, Movie movie){
        this.message = message;
        this.movie = movie;

    public String getMessage(){
        return this.message;

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

    public Movie getMovie(){
        return this.movie;

    public void setMovie(Movie movie){
        this.movie = movie;

In the following scenario, we are firing an event when a new Movie is persisted. The following code resides within a MovieController CDI bean of an example JSF application:

public class MovieController implements Serializable {

    private com.mycompany.cditest.session.MovieFacade ejbFacade;
    private List items = null;
    private Movie selected;

    Event<MovieEvent> movieEvents;
. . .
   private void persist(PersistAction persistAction, String successMessage) {
        if (selected != null) {
            try {
                if (persistAction != PersistAction.DELETE) {
                    movieEvents.fireAsync(new MovieEvent("New Movie Released", selected))
                            .whenComplete((event, throwable) -> {
                                if(throwable != null){
                                    System.out.println("Error has occurred: " + throwable.getMessage());
                                } else {
                                    System.out.println("Successful Movie Processing...");
                } else {
            } catch (Exception ex) {
                Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
                JsfUtil.addErrorMessage(ex, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
. . .

When the event is fired, it creates a new MovieEvent object, and if it successfully completes then a message is printed to indicate success.  In this example, a stream is used to process the "whenComplete" action.

Feel free to clone the repository located at https://github.com/juneau001/CdiTest and take CDI 2.0 test for a spin. This example is just one simple test of CDI 2.0. You can clone and utilize this test project as a starting point to work with other aspects of the specification. Keep an eye on JSR 365, which is currently in the Public Review Ballot stages.

cdi 2.0 ,java ,asynchronous events ,tutorial ,java se

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}