How do I fire an event?

We fire events with an instance of the parameterized Event interface, which can be obtained through injection as follows:

@Inject
Event<Book> bookEvent;

We then fire the event using the following:

bookEvent.fire(book);

This event can be consumed by any observer method that matches the event object type and does not specify additional qualifiers. In our case, that would be the first observer method from the previous section.

Note

If an exception is thrown within an observer method, Weld stops further calls to matching observer methods and the exception is rethrown by fire().

Get JBoss Weld CDI for Java Platform now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.