- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我创建了一个示例客户端/服务器应用程序来熟悉 Spring Webflux/Reactor Netty。现在,当响应包含 Flux 并且媒体类型为“文本/事件流”时,我对客户端的行为有点困惑。我可以看到服务器上生成的每个元素都会立即发送到客户端,但尚未传递给订阅者。第一次交付给订阅者发生在服务器端的生产者完成 Flux 之后。对我来说,这意味着所有元素首先会在客户端的reactor-netty 中的某个位置收集,直到获得完整/错误事件。
我的结论是正确的还是我可能做错了什么?如果属实,这种情况在不久的将来会改变吗?根据我目前观察到的行为,使用 Spring Webflux 的大部分好处都被否定了,因为与 Spring Mvc 一样,消费者必须等到整个元素集合创建并传输后才能开始处理元素。
我的服务器应用程序是:`
@SpringBootApplication
public class ServerApp {
public static void main(String[] args) {
new SpringApplicationBuilder().sources(ServerApp.class).run(args);
}
@RestController
public static class TestController {
@GetMapping(value = "/test", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<String> testFlux() {
class AsyncSink implements Consumer<SynchronousSink<String>> {
private List<String> allStrings = List.of(
"Hello Flux1!",
"Hello Flux2!",
"Hello Flux3!",
"Hello Flux4!",
"Hello Flux5!");
private int index = 0;
@Override
public void accept(SynchronousSink<String> sink) {
if (index == allStrings.size()) {
sink.complete();
}
else {
sink.next(allStrings.get(index++));
}
}
}
return Flux.generate(new AsyncSink());
}
}
}
我的客户端应用程序是:
@SpringBootApplication
public class ClientApp {
public static void main(String[] args) throws IOException {
ConfigurableApplicationContext aContext = new SpringApplicationBuilder().web(WebApplicationType.NONE).sources(ClientApp.class).run(args);
Flux<String> aTestFlux = aContext.getBean(TestProxy.class).getFlux();
aTestFlux.subscribe(new TestSubscriber());
System.out.println("Press ENTER to exit.");
System.in.read();
}
@Bean
public WebClient webClient() {
return WebClient.builder().baseUrl("http://localhost:8080").build();
}
@Component
public static class TestProxy {
@Autowired
private WebClient webClient;
public Flux<String> getFlux() {
return webClient.get().uri("/test").accept(MediaType.TEXT_EVENT_STREAM).exchange().flatMapMany(theResponse -> theResponse.bodyToFlux(String.class));
}
}
private static class TestSubscriber extends BaseSubscriber<String> {
@Override
public void hookOnSubscribe(Subscription subscription) {
System.out.println("Subscribed");
request(Long.MAX_VALUE);
}
@Override
public void hookOnNext(String theValue) {
System.out.println(" - " + theValue);
request(1);
}
@Override
protected void hookOnComplete() {
System.out.println(" done");
}
@Override
protected void hookOnCancel() {
System.out.println(" cancelled");
}
@Override
protected void hookOnError(Throwable theThrowable) {
theThrowable.printStackTrace(System.err);
}
}
}
当我访问网址http://localhost:8080/test时使用 Chrome 浏览器我看到:
data:Hello Flux1!
data:Hello Flux2!
data:Hello Flux3!
data:Hello Flux4!
data:Hello Flux5!
对我来说,看起来已经发送了 5 个 http 事件。
最佳答案
取自 react 性文档并重写以满足您的需求。
我的猜测是,在您的示例中,您已经向生成函数传递了一个消费者,该消费者在完成后将被发出。
改为使用方法 Flux#generate(Callable<S> stateSupplier, BiFunction<S,SynchronousSink<T>,S> generator)
您提供一个状态,其中将包含您想要发出的项目,然后在提供的 BiFunction
中您逐一发出每一项。
Flux<String> flux = Flux.generate(
() -> List.of("1!", "2!", "3!", "4!", "5!"),
(state, sink) -> {
if (index == allStrings.size()) {
sink.complete();
} else {
sink.next(state.get(index++));
}
});
我尚未测试在移动设备上编写的代码。
关于java - 服务器上生成的带有 MediaType.TEXT_EVENT_STREAM 的事件何时会传递给客户端上的订阅者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57004002/
我看到 @products 注释允许我传递单个字符串和字符串列表。所以我只是想知道这是如何在java中完成的,如果我需要使用允许以下行为的方法来实现它,我该怎么做?或者这个注释是特定的,所以我们不能在
我需要创建一个 REST 服务(Jersey),它接受 header 为“application/com.foo+xml”(+json,如果是 JSON mime 类型)。 有什么办法可以拥有吗?@P
我有一个使用 XML 和 JSON 的 Web 服务。 输出当前始终采用 XML 格式。 是否可以使用与所使用的 MediaType 相同的 MediaType 生成响应? 我需要的是: 请求是 XM
一旦我得到这个问题 Latest Jersey example does not work 的答案,我就遇到了另一个奇怪的问题: 服务器,GET 方法工作正常。我测试并添加了一些测试代码 hellow
我正在使用 Jersey 启用 JacksonFeature 编写 REST 客户端,用于强制我指定其自定义命名内容类型的 Web 服务,即使它只是常规 JSON。换句话说,当我这样做时: Reque
我正在使用 ReSTLet 来实现一个简单的 RESTful API,它为我提供了一个 org.reSTLet.data.MediaType 作为 ContentType。但是,我正在尝试使用其他一些
我使用 symfony 3.4 和奏鸣曲媒体包。在表单中使用 MediaType 效果很好,如下所示: $this->builder->->add('document', MediaType::cla
我正在使用 ReSTLet 构建某种代理服务器,但是我遇到了一个问题,即没有根据客户端请求自动确定 MediaType 的方法。 这是我的代码: Representation entity = nul
创建多部分/相关请求时。我们需要在 Content-Type 字符串中添加一个额外的 type=MIME_TYPE 内容类型应该是这样的 Content-Type: multipart/related
我的 SpringBoot 应用程序向外部 API 发出 HTTP 请求,所有这些都使用/生成 JSON。默认情况下,我的应用程序使用 Jackson 进行数据绑定(bind),并且所有 HTTP 请
6天前,我问了这个问题: how to select from filtervalues where genre comma delimited values contain only what is
我创建了一个示例客户端/服务器应用程序来熟悉 Spring Webflux/Reactor Netty。现在,当响应包含 Flux 并且媒体类型为“文本/事件流”时,我对客户端的行为有点困惑。我可以看
我有 Spring Rest Controller ,如下: @RestController @RequestMapping(value = "/v1/files") public class Dat
我创建了一个使用 REST 服务器进行 CRUD 操作的 Web 应用程序。我使用 java Jersey 和 gradle。当我执行 POST 操作时,我以 JSON 格式发送对象,如下所示: To
我只使用照片模式,但从 2019 年 Spring 开始,苹果商店要求获得微观许可。当用户只使用照片时,授予 micro 权限会非常困惑。这个问题有什么解决方法吗? 最佳答案 可能需要麦克风权限,因为
我正在构建我的第一个 API。 GET 请求有效,但我陷入了 POST 请求,并出现错误 415 不支持的媒体类型。经过一番查找和重写代码,仍然失败。有人明白为什么吗?参数值为: -String us
我目前正在开发一个由其他团队维护的项目,现在我需要维护它。当我完成该项目时,我发现了以下一些内容: 在 jax-rs Controller 中,它由 @Consumes(MediaType.APPLI
我遇到了一个奇怪的问题。我有一个 Web 项目(java 服务),我用 ANT 构建它。然而由于某些原因,构建系统不得不更改为Maven。 使用 Maven 构建后,我将 war 文件部署到 tomc
我目前正在开发一些 RESTful API 的版本 2,为了保持与版本 1 的兼容性,我想将该版本作为媒体类型参数添加到所有请求/响应中。 版本1:接受:application/json、applic
如何为具有自定义 MediaType 的实体发送 GET 请求? 例如,我想检索 MyUserDTO 并将 MediaType 设置为 application/user+yml。 现在我有两个独立的
我是一名优秀的程序员,十分优秀!