弹簧反应堆通量意外关闭

时间:2020-06-29 08:24:43

标签: java spring websocket spring-webflux project-reactor

你好,我想用电抗器通量来实现:

服务Websocket服务,其功能是:

  • 等待单身
  • 开始并发送间隔

这是我的代码:

    @Override
    public Mono<Void> doHandle(WebSocketSession session, JwtAuthenticationToken jwt) {
        Mono<List<Application>> apps = applicationsService.getApplications(jwt);
        
        // Ask another rest service for a list app permited application uuids.
        Mono<List<String>> appUUidsMono = apps
                .map(l -> l.stream().map(Application::getUuid).collect(Collectors.toList()));

        AtomicLong since = new AtomicLong(0);

        List<String> appUUids = new ArrayList<>();
        Flux<List<Task>> tasksFlux = appUUidsMono //
                .doOnSuccess(au -> { // A hacky solution to wait for the mono.
                    appUUids.addAll(au);
                }) //
                // Start the intervall to load data from db. 
                // What i wish to have is not a static intervall. Better is an loop that respects the time that the db query required to prevent overloading the db.
                .thenMany(Flux.interval(Duration.ofMillis(1000L))) //
                .map(t -> loadUpdatesFromDb(appUUids, since));

        return session.send(tasksFlux //
                .flatMapIterable(Function.identity()) //
                .map(task -> {
                    try {
                        return objectMapper.writeValueAsString(task);
                    } catch (JsonProcessingException e) {
                        session.close();
                        log.error("Invalid websocket message", e);
                        return null;
                    }
                }) //
                .log() //
                .filter(Objects::nonNull) //
                .map(session::textMessage) //
        );
    }

问题A:

有没有比以下更好的解决方案: .doOnSuccess() 在我开始intervall之前grep单声道的结果? Flux.zip()不起作用,因为单声道的.complete()还将完成Flux.zip()产生的Flux

问题B:

有没有比这更好的解决方案:Flux.interval 我搜索类似的内容:

while(flux.isNotCanceled){ sink.emit(loadUpdatesFromDb()) 线程睡眠(1000) }

0 个答案:

没有答案