3

My question is whether or not Flux has the ability to behave like an Observable or BehaviorSubject. I think I get the gist of what a Flux does and how, but every tutorial I see creates a Flux of static content, i.e. some pre-existing array of numbers which are finite in nature.

However, I want my Flux to be a stream of unknown values over time... like an Observable or BehaviorSubject. With those, you can create a method like setNextValue(String value), and pump those values to all subscribers of the Observable/BehaviorSubject etc.

Is this possible with a Flux? Or does the Flux have to be composed of an Observable type stream of values first?

Update

I answered my own question with an implementation down below. The accepted answer will lead down same path probably, but slightly complicated.

cyberguy
  • 233
  • 3
  • 10

2 Answers2

1

every tutorial I see creates a Flux of static content, i.e. some pre-existing array of numbers which are finite in nature.

You'll see this because most tutorials focus on how to manipulate & use a Flux - but the implication here (that you can just use a Flux with static, fixed-length content) is both unfortunate, and wrong. It's much more powerful than that, and using it with such static content is almost certainly not how you see it used in the real-world.

There's essentially 3 different ways of instantiating a Flux to emit elements dynamically as you describe:

However, I want my Flux to be a stream of unknown values over time... like an Observable or BehaviorSubject. With those, you can create a method like setNextValue(String value), and pump those values to all subscribers of the Observable/BehaviorSubject etc.

Absolutely - have a look at Flux.push(). This exposes an emitter, and emitter.next(value) can be called whenever you wish. This stream can go on for as long as you want it to (infinitely, if desired.) Flux.create() is essentially the multi-threaded variant of Flux.push(), which may also be of use.

Flux.generate() may also be worth a look - this is a bit like an "on-demand" version of Flux.push(), where you only emit the next element via a callback when the downstream consumer requests it, rather than emitting whenever you want to. This isn't always practical, but it makes sense to use this method if the use-case makes it feasible, as it respects backpressure and thus can be guaranteed not to overwhelm the consumer with more requests than it can handle.

Michael Berry
  • 70,193
  • 21
  • 157
  • 216
  • Thanks, I had seen those recently, but trying to find a good example of how to use them. Essentially I am trying to create a StatusService which exposes a Flux of the most recent status (which would be updated by a method accepting a String and emitting that value to the Flux which is subscribed to by whatever other components/classes). So I will try to make that happen with those methods. – cyberguy Jun 19 '20 at 19:55
1

This can be achieved like this:

private EmitterProcessor<String> processor;
private FluxSink<String> statusSink;
private Flux<String> status;

public constructor() {
    this.processor = EmitterProcessor.create();
    this.statusSink = this.processor.sink(FluxSink.OverflowStrategy.BUFFER);
    this.status = this.processor.publish().autoConnect();
}

public Flux<String> getStatus() {
    return this.status;
}

public void setStatus(String status) {
    this.statusSink.next(status);
}
cyberguy
  • 233
  • 3
  • 10