Oracle Logo

Observer Ordering in CDI 2.0

 

CDI 2.0 Observer Ordering Introduction

Before the actual observer notification, the container determines an order in which the observer methods for a certain event are invoked. The priority of an observer method may be declared by annotating the event parameter with @Priority annotation. If a @Priority annotation is declared on an event parameter of an asynchronous observer method, non-portable behavior results.

The order of more than one observer with the same priority is undefined and the observer methods are notified therefore in a non-predictable order.

Description of the Example

This example demonstrates the Observer ordering feature in CDI 2.0. When you click the button, an event is fired. Then two observers will print two different messages in accordance with the order of the values of @Priority. "This is the first message." statement is displayed first and then the second one.

Use This Example