I need to propagate some context to downstream operators in Project Reactor, but it looks like they only allow to do so in a bottom-up fashion as described in the documentation.
Is there a way to propagate it from upstream operators to downstream? I can imaging a workaround with a wrapper like this, but I'd really like to avoid that:
private static class Contextualized<T> {
private final Map<Object, Object> context;
private final T content;
public Contextualized(T content) {
context = new HashMap<>();
this.content = content;
}
public Contextualized(Map<Object, Object> context, T content) {
this.context = context;
this.content = content;
}
public Map<Object, Object> getContext() {
return context;
}
public T getContent() {
return content;
}
}
public void someFunction() {
Mono.just(new Contextualized<>(1))
.map(it -> new Contextualized<>(Map.of("operation", "added 1"), it.getContent() + 1))
.doOnNext(it -> System.out.println(it.getContext().get("operation").toString()))
.subscribe();
...
}