[英]How to orginize thread pooling with spring flux and spring flux web client?
[英]How to end nested subscriptions in spring web flux?
我正在嘗試使用我的應用程序,以便在將新條目添加到后端時,通過事件源將新更改通知前端。 代碼如下所示,這一切似乎都運行良好,但我不確定在這樣的方法(getAllShifts)中訂閱是否是一種好習慣,因為我不確定它是如何處理的? 當客戶端關閉應用或事件源結束時訂閱是否結束? 任何有關這方面的指導都會有所幫助。
@Service
public class ShiftService {
@Autowired
ShiftRepository shiftRepository;
private Sinks.Many<Shift> sink = Sinks.many().replay().latest();
public Mono<Shift> addShift(Shift shift) throws InterruptedException {
Mono<Shift> shiftt = shiftRepository.save(shift);
return shiftt
.doOnSuccess(u -> this.sink.tryEmitNext(u));
}
public Flux<Shift> getAllShifts() {
this.shiftRepository.findAll().subscribe(u -> this.sink.tryEmitNext(u));
return sink.asFlux();
}
}
另一個問題,如果我們訂閱 Mono,我們是否需要處理它,或者它是否會自行處理,因為只有一個項目要發出?
是的。 不應使用訂閱。 你讓框架訂閱。 您可以很好地在此使用相同的onSuccess
返回 this.shiftRepository.findAll().doOnsuccess(u -> this.sink.tryEmitNext(u));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.