Java 如何在每次变量更改时生成事件流?
我想在每次更改整数变量时发送更新信息Java 如何在每次变量更改时生成事件流?,java,spring-boot,stream,reactive-programming,spring-webflux,Java,Spring Boot,Stream,Reactive Programming,Spring Webflux,我想在每次更改整数变量时发送更新信息 @PutMapping public void update() { integer = random.nextInt(); } @GetMapping(produces = MediaType.TEXT_EVENT_STREAM_VALUE) public Flux<Integer> eventFlux() { Flux<Integer> eventFlux = Flux.fromStream(
@PutMapping
public void update() {
integer = random.nextInt();
}
@GetMapping(produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<Integer> eventFlux() {
Flux<Integer> eventFlux = Flux.fromStream(
Stream.generate(() -> integer)
);
return Flux.zip(eventFlux, onUpdate()).map(Tuple2::getT1);
}
private Flux<Long> onUpdate() {
return Flux.interval(Duration.ofSeconds(1));
}
@PutMapping
公共无效更新(){
整数=random.nextInt();
}
@GetMapping(products=MediaType.TEXT\u事件\u流\u值)
公共流量事件流量(){
通量事件通量=通量.fromStream(
Stream.generate(()->integer)
);
返回Flux.zip(eventFlux,onUpdate()).map(Tuple2::getT1);
}
私有流量更新(){
返回通量间隔(持续时间秒(1));
}
您可以使用FluxProcessor
作为:
- 上游
;您可以使用它向其中添加数字:用户
- 下游
(Publisher
);哪些客户端可以订阅以接收添加的号码:Flux
@GetMapping(products=MediaType.TEXT\u EVENT\u STREAM\u VALUE)
公共流量事件流量(){
返回处理器;
}
以下是一个完整的工作示例:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.config.EnableWebFlux;
import org.springframework.web.reactive.config.WebFluxConfigurer;
import reactor.core.publisher.DirectProcessor;
import reactor.core.publisher.Flux;
import reactor.core.publisher.FluxProcessor;
@SpringBootApplication
@EnableWebFlux
@RestController
public class EventStreamApp implements WebFluxConfigurer {
public static void main(String[] args) {
SpringApplication.run(EventStreamApp.class);
}
private FluxProcessor<Integer, Integer> processor = DirectProcessor.create();
@PutMapping("/{number}")
public void update(@PathVariable Integer number) {
processor.onNext(number);
}
@GetMapping(produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<Integer> eventFlux() {
return processor;
}
}
导入org.springframework.boot.SpringApplication;
导入org.springframework.boot.autoconfigure.springboot应用程序;
导入org.springframework.http.MediaType;
导入org.springframework.web.bind.annotation.GetMapping;
导入org.springframework.web.bind.annotation.PathVariable;
导入org.springframework.web.bind.annotation.PutMapping;
导入org.springframework.web.bind.annotation.RestController;
导入org.springframework.web.reactive.config.EnableWebFlux;
导入org.springframework.web.reactive.config.webfluxconfig;
导入reactor.core.publisher.DirectProcessor;
导入reactor.core.publisher.Flux;
导入reactor.core.publisher.FluxProcessor;
@SpringBoot应用程序
@EnableWebFlux
@RestController
公共类EventStreamApp实现WebFluxConfigure{
公共静态void main(字符串[]args){
运行(EventStreamApp.class);
}
私有FluxProcessor processor=DirectProcessor.create();
@PutMapping(“/{number}”)
公共无效更新(@PathVariable整数){
处理器.onNext(编号);
}
@GetMapping(products=MediaType.TEXT\u事件\u流\u值)
公共流量事件流量(){
返回处理器;
}
}
希望这能有所帮助。那么,你的问题是什么?你有什么错误吗。请解释清楚你的疑问!现在的代码是每秒发送一次事件,我想知道只有当变量值发生变化时如何发送事件。谢谢。如果这个链接对你有帮助,请浏览它
@GetMapping(produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<Integer> eventFlux() {
return processor;
}
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.config.EnableWebFlux;
import org.springframework.web.reactive.config.WebFluxConfigurer;
import reactor.core.publisher.DirectProcessor;
import reactor.core.publisher.Flux;
import reactor.core.publisher.FluxProcessor;
@SpringBootApplication
@EnableWebFlux
@RestController
public class EventStreamApp implements WebFluxConfigurer {
public static void main(String[] args) {
SpringApplication.run(EventStreamApp.class);
}
private FluxProcessor<Integer, Integer> processor = DirectProcessor.create();
@PutMapping("/{number}")
public void update(@PathVariable Integer number) {
processor.onNext(number);
}
@GetMapping(produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<Integer> eventFlux() {
return processor;
}
}