CDI - Events and Observers

CDI Events and Observers for Low Coupling and High Cohesion


This is the Part 3 of the CDI Series in Java that contains:

  • Part 1: Factory in CDI with @Produces annotation
  • Part 2: CDI Qualifiers
  • Part 3: CDI Events and Observers for Low Coupling and High Cohesion
  • Part 4: CDI and Lazy Initialization
  • Part 5: Interceptors in CDI
  • Part 6: CDI Dependency Injection and Alternatives
  • Part 7: Working with CDI Decorators

In this part of the CDI Series, we'll explore Events and Observers used in CDI to have a code with more Low Coupling and High Cohesion.

Let's contextualize our application problem.

  • We must have a Checkout class
  • We must send an Email when a checkout is finished
  • We must send a Metric when a checkout is finished

Let's code!

Step 1 - Creating the entire scenario

This first class will use CDI 2.0 that allows us to use CDI in a standalone mode:

This code won't compile, since we don't have the Order class yet.

Next we have this Order class with a few fields:

As you can see, an Order has a Buyer. Let's create this class:


It's time to create the class that sends Metrics. This is a good cohesion, since this class is responsible for just send metrics:

Following the same thought, we'll create the EmailSender class:

Finally, it's time to create the Checkout class. In the finishCheckout() we're receiving an Order as a parameter and, with this object, we send an email and metrics:


So far we have a good cohesion and low coupling because the Checkout class just knows the methods to send email and metrics but it doesn't know how.

But what if we need to do much more things? For example:

  • Send a Push Notification
  • Send Kafka messages for other systems
  • Send Events to Amazon SQS for 3rd-Party integrations
  • etc, etc, etc

Can we follow the same pattern as above? Can we just Inject the responsible class in the Checkout class?

As you can see, we fastly can fall into a bunch of Injections that don't matter to the Checkout itself.

Everytime that we need to use the Order in Checkout, we should open the Checkout class to add an @Inject annotation with the new object.

To follow a good cohesion in this case, I'd like to see just code that are related to a checkout being finished.

Let's refactor this code with more CDI!

Step 2 - Refactoring to use CDI Events and Observers

Do you remember the Observer Pattern? So, we will use that to notify that one action has happened.

We'll create a class called CheckoutEvent that will indicate the checkout event.

Now it's time to get help from CDI. We must use the Event Interface to notify all subscribers interested in the checkout event:

As you can see, we've used the method fire() to trigger the action to notify all subscribers in the event

Step 3 - Observing the CDI Event

When a Checkout action is taken, we would like to have two actions being executed:

  • Send an email
  • Create a metric

We should indicate that the EmailSender class is interested in the checkout event. We can do that by using the @Observes annotation from CDI, as you can see below:

Of course we can subscribe more interested classes as we will see with the MetricCreator class below:

Excellent! Notice that your Checkout class doesn't know about the EmailSender and MetricCreator classes. The finishCheckout() method is just worried about its logic and will notify all the guys that are interested in this event, but without knowing them. Pretty cool!

That's it! I hope that this part of the CDI series is useful to you!

In the next post we're going to explore Lazy Initialization in CDI: Part 4: CDI and Lazy Initialization


Alexandre Gama

Alexandre Gama

Hacking Code Founder

Hacking Code Founder and Writer, Passionate Senior Software Engineer and Team Leader at @Elo7, Teacher of several courses at Caelum, Speaker at many conferences and terrible guitar player.
CDI - Factories with @Produces Annotation - Hacking Code Java Tutorial

CDI - Factories with @Produces Annotation

Do you want to create powerful and easy Factories in CDI? Let's see how!

CDI Qualifiers Tutorial - Java CDI Guide by Hacking Code

CDI Qualifiers

Working with CDI Qualifiers
CDI Observers and Events Tutorial - Java CDI Guide by Live Coding

CDI Events and Observers

Let's see why and how to use CDI Events and Observers
CDI Lazy Initialization Tutorial - Java CDI Guide by Live Coding

CDI Lazy Initialization Tutorial

Did you know that we can initialize CDI beans lazily?
CDI Interceptors Tutorial - Java CDI Guide by Live Coding

CDI Interceptors

How to use CDI Interceptors from scratch
CDI Alternatives Tutorial - Java CDI Guide by Live Coding

CDI Alternatives Tutorial

Lear how to use Argument Matcher in its Fundamental way

CDI Decorator Tutorial - Java CDI Guide by Live Coding

CDI Decorator Tutorial

CDI Decorator is a powerful feature to deal with business Decoration!



  1. CDI – Qualifiers | Hacking Code - […] 5 […]

Leave a Reply

Share This
Subscribe To Our Newsletter

Subscribe To Our Newsletter

Join our mailing list to receive the latest news and updates from our team.

You have Successfully Subscribed!

%d bloggers like this: