-1

What is the difference between using an event bus for publishing events vs using CDI Event with @Observes?

I'm currently using the event bus for all async communications in Quarkus. Since the vert.x event bus does not support cluster communication in Quarkus (https://github.com/quarkusio/quarkus/issues/10889), I'm ok with local event publishing. However, in what cases would one choose to go with a CDI event instead of using the eventbus?

Jonathan S. Fisher
  • 8,189
  • 6
  • 46
  • 84
Romil Punetha
  • 31
  • 1
  • 3

1 Answers1

0

CDI Events occur "in thread", meaning essentially the event firing thread calls an unknown number over observers, which may be 0, may be 100. The key here is observers always run in the same thread as the firing thread. Read the details here: https://www.baeldung.com/cdi-event-notification

That may differ with Vert.x, which events may fire in new threads, or may fire in the same thread. This depends solely on how you write and configure your program. If you have a clustered Vert.x app, they definitely are firing in different threads if they launch on another cluster member.

The correct answer for your situation depends on the problem you're trying to solve.

Jonathan S. Fisher
  • 8,189
  • 6
  • 46
  • 84
  • 1
    A couple of minor comments: (a) CDI permits asynchronous events (https://jakarta.ee/specifications/cdi/3.0/apidocs/jakarta/enterprise/event/observesasync); (b) you can get full control over the `Executor` as well (https://jakarta.ee/specifications/cdi/3.0/apidocs/jakarta/enterprise/event/notificationoptions#getExecutor--). See https://jakarta.ee/specifications/cdi/3.0/jakarta-cdi-spec-3.0.html#firing_events_asynchronously and related. – Laird Nelson Apr 19 '22 at 20:48