Home > Java > Create a Flux that can publish arbitrary events in the future

Create a Flux that can publish arbitrary events in the future

PHPz
Release: 2024-02-09 11:50:24
forward
528 people have browsed it

php editor Apple will introduce you to an exciting development tool-Flux. Flux is an application architecture for building user interfaces that focuses on the concept of unidirectional data flow, making state management simple and predictable. By creating a Flux architecture, developers can easily manage the state of their applications and be able to publish arbitrary events in the future, enabling more flexible application development. Flux's simplicity and extensibility make it ideal for modern front-end development. Next, we’ll dive into how Flux works and how to use it to build great user interfaces.

Question content

I want to create a custom flux that can publish arbitrary events in the future.

For example:

@RestController
public class EventController {
    @GetMapping(path = "/event/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> eventStream() {
       // how to create a flux here which I can publish arbitrary events to in future?
    }
}
Copy after login

I saw the flux.from(...) and flux.generate(...) methods in the flux javadocs, but these don't seem to meet my needs

Solution

I ended up using sinks.many()

Many<String> emitter = Sinks.many().unicast().onBackpressureBuffer();
Flux<String> flux = emitter.asFlux();
Copy after login

I can do it now

The above is the detailed content of Create a Flux that can publish arbitrary events in the future. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template