An open API service indexing awesome lists of open source software.

https://github.com/johnament/reactive-cdi-events

A simple prototype of using Project Reactor with CDI Events
https://github.com/johnament/reactive-cdi-events

Last synced: 12 months ago
JSON representation

A simple prototype of using Project Reactor with CDI Events

Awesome Lists containing this project

README

          

# Project Reactor Meets CDI Events

Sometimes a good weekend in the mountains gets the creative juices flowing.

So what happens when you mix [Project Reactor](https://projectreactor.io/) with the CDI event programming model? You get to `Flux` your way through events!

The built in event model in CDI only supports `void` methods or async invocations. You can convert the resulting `CompletionStage` to a `Mono` using an approach like this

```java
CompletionStage stringCompletionStage = seContainer.getBeanManager().getEvent().fireAsync("");
Mono mono = Mono.fromCompletionStage(stringCompletionStage);
```

But that only allows you to ever look at one result. I wanted to beable to get all of the results. There is quite a bit of code required to make it work, but I can fully support an observer method pattern with injected dependencies dynamically.

To get started, inject a `ReactorEvent` where `S` is the sender type, and `R` is the return type.

```java
@Inject
private ReactorEvent event;

public void fluxTheCapacitor() {
event.fire("my incoming msg")
.publishOn(Schedulers.single())
.log("com.foo.bar", Level.SEVERE)
.subscribe(System.out::println);
}
```

This sends an event with payload `my incoming msg` and will print out any results it receives. You can register an observer

```java
public Flux handle(@ObservesReactor String msg) {
return Flux.just("bob");
}
public String another(@ObservesReactor String msg, FooBarBean fooBarBean) {
return "ralph "+msg+fooBarBean.toString();
}
```