project-reactor-常见问题汇总

笔记本源码请参考reactor

@file:DependsOn("io.projectreactor:reactor-core:3.2.9.RELEASE")
import reactor.core.publisher.*

switchIfEmpty总是被调用的原因

先看一个例子

//打印A,没有打印b,说明调用flatMap,没调用switchIfEmpty
Mono.justOrEmpty<String>("a")
.flatMap({Mono.just(it.uppercase())})
.switchIfEmpty(Mono.just("b"))
.subscribe({println(it)})
A





reactor.core.publisher.LambdaMonoSubscriber@107f4980
//只打印了b,说明没调用flatMap,调用了switchIfEmpty
Mono.justOrEmpty<String>(null)
.flatMap({Mono.just(it.uppercase())})
.switchIfEmpty(Mono.just("b"))
.subscribe({println(it)})
b





reactor.core.publisher.LambdaMonoSubscriber@2a685eba

再看一个例子

//打印flatMap,也打印了switchIfEmpty,说明flatMap和switchIfEmpty都调用了
Mono.justOrEmpty<String>("a")
.flatMap({Mono.fromRunnable<Void>({println("flatMap")})})
.switchIfEmpty({Mono.fromRunnable<Void>({println("switchIfEmpty")})})
.subscribe({println(it)})
flatMap
switchIfEmpty





reactor.core.publisher.LambdaMonoSubscriber@77d680e6
//只打印了switchIfEmpty,说明没调用flatMap,调用了switchIfEmpty
Mono.justOrEmpty<String>(null)
.flatMap({Mono.fromRunnable<Void>({println("flatMap")})})
.switchIfEmpty({Mono.fromRunnable<Void>({println("switchIfEmpty")})})
.subscribe({println(it)})
switchIfEmpty





reactor.core.publisher.LambdaMonoSubscriber@f08fdce

为什么会这样

switchIfEmpty如果上一个操作符的数据类型是Void,则switchIfEmpty会被调用。

怎么解决这个问题

switchIfEmpty上一个操作符的返回值不能是Void,可以使用java.lang.ref.SoftReference或java.util.concurrent.atomic.AtomicReference作为返回值,然后根据SoftReference和AtomicReference的值去判断

改写上面的例子

//这样就只调用了flatMap
Mono.justOrEmpty<String>("a")
.map({java.lang.ref.SoftReference(it)})
.defaultIfEmpty(java.lang.ref.SoftReference(null))
.flatMap({
    if(it.get()==null){
        Mono.fromRunnable<Void>({println("switchIfEmpty")})
    }else{
        Mono.fromRunnable<Void>({println("flatMap")})
    }
})
.subscribe({println(it)})
flatMap





reactor.core.publisher.LambdaMonoSubscriber@76ddd61a

主要思路就是flatMap+switchIfEmpty的组合改写成map+defaultIfEmpty+flatMap的组合

典型场景:gateway过滤器处理请求

DataBufferUtils.join(request.getBody())
    .map(SoftReference::new)
    .defaultIfEmpty(new SoftReference<>(null))
    .flatMap(new Function<SoftReference<DataBuffer>, Mono<Void>>() {
        @Override
        public Mono<Void> apply(SoftReference<DataBuffer> reference) {
            if(reference.get()==null){
                //无body逻辑
            }else{
                //有body逻辑
            }
        }
    });
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值