如何在 Reactor 的 doFinally 或 doOnCancel 中获取上下文?

huangapple go评论100阅读模式
英文:

How can I get the context in doFinally or doOnCancel with reactor?

问题

我正在尝试获取上下文中的一个值,我无法弄清楚如何做到这一点,有任何想法吗?示例:

 return mono
        .doFinally(signalType -> 如何??? )
        .doOnEach(signal -> { 
               ... signal.getContext();
               ...
            }) -> 好的我获取到上下文了
        .subscriberContext(ctx -> ctx.put("key", "foo"));
英文:

I'm trying to get a value that I have in the context, I canot figure out how can I do that, any idea?, example:

 return mono
        .doFinally(signalType -> how??? )
        .doOnEach(signal -> { 
               ... signal.getContext();
               ...
            }) -> is ok I got the context
        .subscriberContext(ctx -> ctx.put("key", "foo"));

答案1

得分: 4

考虑使用 Mono#deferWithContext:

return Mono
    .deferWithContext(ctx -> {
        mono.doFinally(signalType -> handleSignal(ctx, signalType))
            .doOnEach(...)
    })
    // 后续操作...
    .subscriberContext(ctx -> ctx.put("key", "foo"));
英文:

Consider using Mono#deferWithContext:

return Mono
    .deferWithContext(ctx -> {
        mono.doFinally(signalType -> handleSignal(ctx, signalType))
            .doOnEach(...)
    })
    // later...
    .subscriberContext(ctx -> ctx.put("key", "foo"));

huangapple
  • 本文由 发表于 2020年10月6日 22:22:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/64227807.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定