Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 反应流问题_Java_Spring_Spring Boot_Spring Webflux - Fatal编程技术网

Java 反应流问题

Java 反应流问题,java,spring,spring-boot,spring-webflux,Java,Spring,Spring Boot,Spring Webflux,然后它停止了。while(true)始终填充队列。据我所知,我使用的thenMany方法应该是每次将前一个标记为complete()时,通过ConcurrentLinkedQueue内容生成一个新的流量。但这似乎不起作用 编辑: 基本上,我想要的是从lambda范围之外向websocket发送数据。这就是为什么我创建了一个队列并使用了。然后使用many(session.send(Flux.generate…)我希望它会在其他线程向队列添加数据时继续从队列中读取数据。正如我看到的那样,您正在搜索一

然后它停止了。
while(true)
始终填充队列。据我所知,我使用的
thenMany
方法应该是每次将前一个标记为
complete()
时,通过
ConcurrentLinkedQueue
内容生成一个新的流量。但这似乎不起作用

编辑:
基本上,我想要的是从lambda范围之外向websocket发送数据。这就是为什么我创建了一个队列并使用了
。然后使用many(session.send(Flux.generate…)
我希望它会在其他线程向队列添加数据时继续从队列中读取数据。

正如我看到的那样,您正在搜索一个总是发出到达队列的数据的队列。 所以我建议你使用WebFlux处理器,在两个不同的流中分离发送和接收

INFO reactor.Flux.Map.1 - onNext(Received: INIT)
INFO reactor.Flux.Map.1 - onNext(Received: 1)
INFO reactor.Flux.Map.1 - onNext(Received: 2)
INFO reactor.Flux.Map.1 - onNext(Received: 3)
INFO reactor.Flux.Map.1 - onNext(Received: MSG #0)
EmitterProcessor e=EmitterProcessor.create();
e、 onNext(“1”);
WebSocketClient WebSocketClient=新的ReactorNettyWebSocketClient();
执行(新的java.net.URI(“wss://echo.websocket.org“”,webSocketSession->{
返回Mono.zip(listenData(webSocketSession),
sendSms(webSocketSession,e),(aVoid,aVoid2)->aVoid;
}).subscribe();
int i=0;
while(true)
{
字符串msg=“msg#”+i++;
e、 onNext(msg);
睡眠(1000);
}
专用Mono sendSms(WebSocketSession WebSocketSession,EmitterProcessor e){
返回webSocketSession.send(e.map(webSocketSession::textMessage).log());
}
专用Mono listenData(WebSocketSession WebSocketSession){
返回webSocketSession
.receive()
.map(WebSocketMessage::getPayloadAsText)
.map(s->“已接收:”+s)
.log()
.然后();
}

问题在于,您将发送和接收与
Mono
Flux
上的方法相结合。
thenMany
方法使
通量
忽略该通量中的元素,并仅对完成信号作出反应

因此,除非调用
sink.complete()
,否则不会发生任何事情。 但调用方法后,即使请求,也不会发送进一步的事件

发送和接收应该是独立的

此外,还可以使用
ConcurrentLinkedQueue
a和
FluxSink
代替。
EmitterProcessor
可以向多个订阅者发射,同时为每个订阅者提供背压。当没有订阅者时,它仍然可以接受一些数据推送,直至达到可配置的
缓冲大小

    EmitterProcessor<String> e = EmitterProcessor.create();
    e.onNext("1");

    WebSocketClient webSocketClient = new ReactorNettyWebSocketClient();



    webSocketClient.execute(new java.net.URI("wss://echo.websocket.org"), webSocketSession -> {
        return    Mono.zip( listenData(webSocketSession),
                sendSms(webSocketSession,e),(aVoid, aVoid2) -> aVoid);

    }).subscribe();



    int i = 0;
    while (true)
    {
        String msg = "MSG #" + i++;

        e.onNext(msg);
        Thread.sleep(1000);
    }


 private Mono<Void> sendSms(WebSocketSession webSocketSession,EmitterProcessor<String> e) {
   return webSocketSession.send(e.map(webSocketSession::textMessage).log());
}

private Mono<Void> listenData(WebSocketSession webSocketSession) {
    return webSocketSession
            .receive()
            .map(WebSocketMessage::getPayloadAsText)
            .map(s -> "Received: " + s)
            .log()
            .then();
}

.thenMany(session.send(Flux.from(emitterProcessor))
也不能像预期的那样工作。你能用简单的数据举例说明你想要实现什么以及问题在哪里。所以也许我可以帮你。@1nt3rn3t我想从lambda作用域之外向websocket发送数据。这就是为什么我创建了一个队列并使用了
。thenMany(session.send(Flux.generate…
。我希望在其他线程向队列添加数据时,它会一直从队列中读取数据。您好@1nt3rn3t感谢您的解释我发现了这个问题,并且我已经编辑了我的响应。您能测试一下吗。它似乎工作正常。我已经编辑了下面的解决方案是否可能是
sink.complete();
在任何新消息进入队列之前执行?
    EmitterProcessor<String> e = EmitterProcessor.create();
    e.onNext("1");

    WebSocketClient webSocketClient = new ReactorNettyWebSocketClient();



    webSocketClient.execute(new java.net.URI("wss://echo.websocket.org"), webSocketSession -> {
        return    Mono.zip( listenData(webSocketSession),
                sendSms(webSocketSession,e),(aVoid, aVoid2) -> aVoid);

    }).subscribe();



    int i = 0;
    while (true)
    {
        String msg = "MSG #" + i++;

        e.onNext(msg);
        Thread.sleep(1000);
    }


 private Mono<Void> sendSms(WebSocketSession webSocketSession,EmitterProcessor<String> e) {
   return webSocketSession.send(e.map(webSocketSession::textMessage).log());
}

private Mono<Void> listenData(WebSocketSession webSocketSession) {
    return webSocketSession
            .receive()
            .map(WebSocketMessage::getPayloadAsText)
            .map(s -> "Received: " + s)
            .log()
            .then();
}
int bufferSize = 10;
FluxProcessor<String, String> processor =
    EmitterProcessor.<String>create(bufferSize).serialize();
FluxSink<String> sink = processor.sink(FluxSink.OverflowStrategy.BUFFER);

sink.next("1");
sink.next("2");
sink.next("3");

WebSocketClient webSocketClient = new ReactorNettyWebSocketClient();
webSocketClient.execute(new URI("wss://echo.websocket.org"),
    session -> {
      Flux<WebSocketMessage> out = Flux.just("INIT")
          .concatWith(processor)
          .map(session::textMessage);

      session.send(out)
          .subscribe(); //instead of thenMany

      return session.receive()
          .map(WebSocketMessage::getPayloadAsText)
          .map(s -> "Received: " + s)
          .log()
          .then();
    })
    .subscribe();

for (int i = 1; i <= 10; i++) {
  sink.next("MSG #" + i);
  TimeUnit.SECONDS.sleep(1);
}
sink.complete();
17:57:54.177 [reactor-http-nio-1] INFO reactor.Flux.Map.1 - onSubscribe(FluxMap.MapSubscriber)
17:57:54.178 [reactor-http-nio-1] INFO reactor.Flux.Map.1 - request(unbounded)
17:57:54.304 [reactor-http-nio-1] INFO reactor.Flux.Map.1 - onNext(Received: INIT)
17:57:54.305 [reactor-http-nio-1] INFO reactor.Flux.Map.1 - onNext(Received: 1)
17:57:54.305 [reactor-http-nio-1] INFO reactor.Flux.Map.1 - onNext(Received: 2)
17:57:54.306 [reactor-http-nio-1] INFO reactor.Flux.Map.1 - onNext(Received: 3)
17:57:54.307 [reactor-http-nio-1] INFO reactor.Flux.Map.1 - onNext(Received: MSG #1)
17:57:54.396 [reactor-http-nio-1] INFO reactor.Flux.Map.1 - onNext(Received: MSG #2)
17:57:55.454 [reactor-http-nio-1] INFO reactor.Flux.Map.1 - onNext(Received: MSG #3)
17:57:56.480 [reactor-http-nio-1] INFO reactor.Flux.Map.1 - onNext(Received: MSG #4)
17:57:57.505 [reactor-http-nio-1] INFO reactor.Flux.Map.1 - onNext(Received: MSG #5)
17:57:58.412 [reactor-http-nio-1] INFO reactor.Flux.Map.1 - onNext(Received: MSG #6)
17:57:59.448 [reactor-http-nio-1] INFO reactor.Flux.Map.1 - onNext(Received: MSG #7)
17:58:00.484 [reactor-http-nio-1] INFO reactor.Flux.Map.1 - onNext(Received: MSG #8)
17:58:01.496 [reactor-http-nio-1] INFO reactor.Flux.Map.1 - onNext(Received: MSG #9)
17:58:02.434 [reactor-http-nio-1] INFO reactor.Flux.Map.1 - onNext(Received: MSG #10)