AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题

问题[spring-webflux](coding)

Martin Hope
Snuii
Asked: 2024-10-04 15:55:05 +0800 CST

Mono.then() 与 .and() 的并行化对比

  • 6

我之前将实体保存到表中,如下所示:
insertTableOne(foo).then(insertTableTwo(foo)).then(Mono.just(foo));

团队成员建议使用以下方法.and()来并行化插入件:
insertTableOne(foo).and(insertTableTwo(foo).thenRetuen(foo);

虽然我同意它看起来更简洁,但我找不到任何文档表明使用.and()将同时执行Schedulers.parallel()两个插入。我认为它仍然是连续的,除非明确说明与并行。

我错了吗?

spring-webflux
  • 1 个回答
  • 28 Views
Martin Hope
Snuii
Asked: 2024-08-08 19:40:30 +0800 CST

可以跳过 doOnNext 的副作用吗?

  • 5

我目前正在编写一个方法,该方法需要在继续下游操作之前将每个元素添加到并发HashMap中。例如

public Flux<Bar> methodOne(final Flux<Foo> foos) {
    Map<UUID, Foo> idToFoo = new ConcurrentHashMap<>();
    return foos.doOnNext(foo -> idToFoo.put(foo.getId(), foo)
         .flatMap(foo -> fooToBar(foo, idToFoo));
}

我是否可以确定fooToBar()传递的地图中将填充相应的元素?

据我所知,doOnNext()它是同步的,它会在继续下游之前执行并完成 lambda。然而,我遭到了一位高级开发人员的反对 - 他们认为这是一种副作用,是一种“发射后不管”的类型。

我正在努力更好地理解源代码,以便能够证明用例的合理性,但也想在这里检查。

谢谢

spring-webflux
  • 1 个回答
  • 24 Views
Martin Hope
Jin Kwon
Asked: 2024-08-05 12:19:21 +0800 CST

如何在 Mono 上调用阻塞 IO 调用

  • 5

我有类似这样的实用方法。

    public static WebClient.ResponseSpec retrieve(final String baseUrl, final Duration responseTimeout) {
        // ...
    }

    public static <T> Flux<T> retrieveBodyToFlux(final String baseUrl, final Duration responseTimeout,
                                                 final Class<T> elementClass) {
        return retrieve(baseUrl, responseTimeout)
                .bodyToFlux(elementClass);
    }

    public static Mono<Void> download(final String baseUrl, final Duration responseTimeout,
                                      final Path destination, final OpenOption... openOptions) {
        return DataBufferUtils.write(
                retrieveBodyToFlux(baseUrl, responseTimeout, DataBuffer.class),
                destination,
                openOptions
        );
    }

现在我想添加另一种方法让客户端使用临时文件。

    // Let me download the file, and you can just cononsume the file!
    public static Mono<Void> download(final String baseUrl, final Duration responseTimeout,
                                      final Consumer<? super Path> consumer) {

        // Create a temp file
        // download the URL to the file
        // accept the file to the consumer; possibly blocking IO operations
        // don't forget to delete the file!
    }

我怎样才能在没有任何阻塞呼叫警告的情况下完成这项工作?

我试过了(似乎有效),但我不知道我做了什么。这是最佳选择吗?我还有其他方法吗?

        return Mono.usingWhen(
                        Mono.fromCallable(() -> Files.createTempFile(null, null)).subscribeOn(Schedulers.boundedElastic()),
                        p -> download(baseUrl, responseTimeout, p, StandardOpenOption.WRITE)
                                .then(Mono.just(p))
                                .doOnNext(consumer).subscribeOn(Schedulers.boundedElastic()),
                        p -> Mono.fromCallable(() -> {
                            Files.delete(p);
                            return null;
                        }).publishOn(Schedulers.boundedElastic()).then()
                )
                .then();
spring-webflux
  • 1 个回答
  • 18 Views
Martin Hope
Dawid
Asked: 2024-02-04 18:21:52 +0800 CST

Reactor 项目 Mono.block() 和 Mono.subscribe() 有什么区别

  • 5

我找不到使用 Mono.block() 和 Mono.subscribe() 的区别

对我来说,当使用这两种方法时,代码的行为完全相同。但它不应该。

对于 Mono.block() 我的期望是调用它的线程将阻塞并等待结果,但它在 Mono 的 map 方法中使用,并且基本上会自行解除阻塞。

我有以下使用 Mono.block() 的代码片段:

void doBlocking() {
        final var myMono = Mono.just("test").map(elem -> {
            System.out.printf("On thread: [%s] inside map\n",Thread.currentThread().getName());
            return elem;
        });


        String value;

        System.out.printf("On thread: [%s] before block\n",Thread.currentThread().getName());
        value = myMono.block();
        System.out.printf("On thread: [%s] after block\n",Thread.currentThread().getName());

        System.out.println(value);
    }

当我调用此代码时,我收到以下内容:

On thread: [main] before block
On thread: [main] inside map
On thread: [main] after block
test

根据我的理解 Mono.block() 是阻塞方法,所以我假设线程将像获取锁时一样被阻塞。相反,线程用于在Mono 的映射内部执行代码,这意味着它根本不会被阻塞。

对于Mono.subscribe()我希望调用 subscribe 的线程将继续而不等待结果,但它的行为与使用Mono.block()时完全相同

我有一个类似的片段,但现在使用订阅而不是块

void doSubscribing() {
        final var myMono = Mono.just("test").map(elem -> {
            System.out.printf("On thread: [%s] inside map\n",Thread.currentThread().getName());
            return elem;
        });


        AtomicReference<String> value = new AtomicReference<>();

        System.out.printf("On thread: [%s] before subscribe\n",Thread.currentThread().getName());
        myMono.subscribe(value::set);
        System.out.printf("On thread: [%s] after subscribe\n",Thread.currentThread().getName());

        System.out.println(value);
    }

当我再次调用此代码时,我得到相同的结果:

On thread: [main] before subscribe
On thread: [main] inside map
On thread: [main] after subscribe
test

我希望当我调用 subscribe 时,当前线程将继续工作,可能显示:

On thread: [main] after subscribe
null

就我而言,阻止和订阅的行为完全相同,那么真正的区别是什么?

spring-webflux
  • 1 个回答
  • 19 Views
Martin Hope
Tiina
Asked: 2023-08-18 10:05:07 +0800 CST

Webflux Reactor如何合并Stream.map输出,可以是Mono和Flux成Flux

  • 5

A用于获取源码。在下面的函数中,如果拥有map源,则源是一个,否则源是一个。函数返回基于A的List可以查询的所有源,列表大小可以是1。问题是当Stream.map输出可以是两者之一时,如何处理这个Mono和Flux组合成Flux。FluxaMono

class A {
    String x;
    String y;
    boolean isOwner();
}

Flux<Source> foo(List<A> a) {
  Flux<Source> sources = a.stream().map(v -> if (v.isOwner) {
        Flux<Source> ownedSource = ...; return ownedSource;
      } else {
        Mono<Source> givenSource = ...; return givenSource;
      }
    }.???
  return sources;
}
spring-webflux
  • 1 个回答
  • 11 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve