Как добавить потребителя к издателю Flux при первом вызове API ⇐ JAVA
-
Гость
Как добавить потребителя к издателю Flux при первом вызове API
I am using the below api code for returning the Flux , but seems when I start my application and do the first API call from postman due to no consumer for the Flux publisher my first API call is getting stuck for infinite time. The events are getting emitted when I do the 2nd api call.
As I I understood this Flux.create(eventsPublisher).share(); looks for any active consumer , as my application started and on the api call /bites/events triggered first time so no consumer. Any suggestion would helpful ?
BiteEventsController.java
public class BiteEventsController { private final Flux biteEvent; private final int INTERVAL = 10; private final Flux keepAlive = Flux.interval(Duration.ofSeconds(INTERVAL)) .map(e -> new BiteEventsResponse("iam-alive", null)); public BiteEventsController(BiteEventsPublisher eventsPublisher) { this.biteEvent = Flux.create(eventsPublisher).share(); } @GetMapping(value = "/bites/events", produces = "text/event-stream;charset=UTF-8") public Flux biteEvents(HttpServletRequest request) { try { Flux biteEvent = this.biteEvent.filter( event -> (event.getOwnerId() != null )) .map(event -> { return new BiteEventsResponse("message", (BiteResponse) event.getSource()); }); return Flux.merge(keepAlive, biteEvent); } catch (Exception ex) { log.error("{}", ex); return Flux.empty(); } } }
Источник: https://stackoverflow.com/questions/781 ... t-api-call
I am using the below api code for returning the Flux , but seems when I start my application and do the first API call from postman due to no consumer for the Flux publisher my first API call is getting stuck for infinite time. The events are getting emitted when I do the 2nd api call.
As I I understood this Flux.create(eventsPublisher).share(); looks for any active consumer , as my application started and on the api call /bites/events triggered first time so no consumer. Any suggestion would helpful ?
BiteEventsController.java
public class BiteEventsController { private final Flux biteEvent; private final int INTERVAL = 10; private final Flux keepAlive = Flux.interval(Duration.ofSeconds(INTERVAL)) .map(e -> new BiteEventsResponse("iam-alive", null)); public BiteEventsController(BiteEventsPublisher eventsPublisher) { this.biteEvent = Flux.create(eventsPublisher).share(); } @GetMapping(value = "/bites/events", produces = "text/event-stream;charset=UTF-8") public Flux biteEvents(HttpServletRequest request) { try { Flux biteEvent = this.biteEvent.filter( event -> (event.getOwnerId() != null )) .map(event -> { return new BiteEventsResponse("message", (BiteResponse) event.getSource()); }); return Flux.merge(keepAlive, biteEvent); } catch (Exception ex) { log.error("{}", ex); return Flux.empty(); } } }
Источник: https://stackoverflow.com/questions/781 ... t-api-call
Мобильная версия