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
- Host: GitHub
- URL: https://github.com/johnament/reactive-cdi-events
- Owner: johnament
- License: apache-2.0
- Created: 2017-10-16T01:42:05.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-10-16T01:55:24.000Z (over 8 years ago)
- Last Synced: 2025-02-25T00:47:16.459Z (over 1 year ago)
- Language: Java
- Size: 12.7 KB
- Stars: 2
- Watchers: 0
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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();
}
```