移除多个订阅,保持为单一流程。

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

Remove multiple subscription and keep it to a single flow

问题

我有以下的方法,它按预期运行。

但是否有一种方法可以修改它,以便我不必单独订阅 webclient 的调用,

而是让它成为流的一部分,并且只从某个订阅者那里订阅一次?

请注意,最终这必须返回一个 Observable<Integer>

  1. Observable&lt;Integer&gt; observable = Observable.just(1)
  2. .delay(5, TimeUnit.MILLISECONDS)
  3. .compose(obs -&gt; {
  4. webClient.putAbs("url")
  5. .rxSend()
  6. .doOnSubscribe(() -&gt; System.out.println("Subbing to client")) // to be removed with solution
  7. .subscribe(); // I don't want to have to do a sub here.
  8. return obs;
  9. })
  10. .doOnSubscribe(() -&gt; System.out.println("the only single sub i want to have"));

某个外部的订阅者会执行以下操作。

observable.subscribe();

我希望这会触发整个流程,从而也会触发 webclient 的调用,而不是像上面那样单独调用它。

这可行吗?

因此,寻找类似以下的东西,它不会单独订阅 webClient。

尝试过使用 flatmap 和 compose,但无法实现。

(以下在语法上是错误的。这只是大致显示我在寻找什么。)

  1. Observable&lt;Integer&gt; observable = Observable.just(1)
  2. .delay(5, TimeUnit.MILLISECONDS)
  3. .compose(obs -&gt; webClient.putAbs(""))
  4. .rxSend()
  5. .toObservable();

非常感谢任何指导。谢谢。

英文:

I have the following method which works as expected.

But is there a way I could amend it so that I don't have to subscribe to the webclient's call separately

and instead let it be part of the flow and subscribe to it only once from some subscriber?

Note that this must ultimately return a Observable < Integer >.

  1. Observable&lt;Integer&gt; observable = Observable.just(1)
  2. .delay(5, TimeUnit.MILLISECONDS)
  3. .compose(obs -&gt; {
  4. webClient.putAbs(&quot;url&quot;)
  5. .rxSend()
  6. .doOnSubscribe(() -&gt; System.out.println(&quot;Subbing to client&quot;)) // to be removed with solution
  7. .subscribe(); // I don&#39;t want to have to do a sub here.
  8. return obs;
  9. })
  10. .doOnSubscribe(() -&gt; System.out.println(&quot;the only single sub i want to have&quot;));

Some external subscriber would do the following.

observable.subscribe();

I would want this to trigger the overall flow which would also trigger the webclient's call instead of separately calling it as above.

Is this possible?

Thus looking for something like the following which doesn't subscribe to webClient separately.

Tried via flatmap and compose and not able to achieve it.

(The following is syntactically wrong. This is just to roughly show what I am looking for).

  1. Observable&lt;Integer&gt; observable = Observable.just(1)
  2. .delay(5, TimeUnit.MILLISECONDS)
  3. .compose(obs -&gt; webClient.putAbs(&quot;&quot;))
  4. .rxSend()
  5. .toObservable();

Appreciate any guidance. Thanks.

答案1

得分: 0

这应该可以工作:

  1. Observable<Integer> observable = Observable.just(1)
  2. .delay(5, TimeUnit.MILLISECONDS)
  3. .flatMap(obs -> webClient.putAbs("url").rxSend().map(a -> obs));
  4. // .flatMap() 立即订阅了内部的 webClient 调用。
英文:

This should work:

  1. Observable&lt;Integer&gt; observable = Observable.just(1)
  2. .delay(5, TimeUnit.MILLISECONDS)
  3. .flatMap(obs -&gt; webClient.putAbs(&quot;url&quot;).rxSend().map(a -&gt; obs));

.flatMap() subscribes to the inner webClient call eagerly.

huangapple
  • 本文由 发表于 2020年10月27日 01:55:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/64542450.html
匿名

发表评论

匿名网友

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

确定