英文:
Remove multiple subscription and keep it to a single flow
问题
我有以下的方法,它按预期运行。
但是否有一种方法可以修改它,以便我不必单独订阅 webclient 的调用,
而是让它成为流的一部分,并且只从某个订阅者那里订阅一次?
请注意,最终这必须返回一个 Observable<Integer>。
Observable<Integer> observable = Observable.just(1)
.delay(5, TimeUnit.MILLISECONDS)
.compose(obs -> {
webClient.putAbs("url")
.rxSend()
.doOnSubscribe(() -> System.out.println("Subbing to client")) // to be removed with solution
.subscribe(); // I don't want to have to do a sub here.
return obs;
})
.doOnSubscribe(() -> System.out.println("the only single sub i want to have"));
某个外部的订阅者会执行以下操作。
observable.subscribe();
我希望这会触发整个流程,从而也会触发 webclient 的调用,而不是像上面那样单独调用它。
这可行吗?
因此,寻找类似以下的东西,它不会单独订阅 webClient。
尝试过使用 flatmap 和 compose,但无法实现。
(以下在语法上是错误的。这只是大致显示我在寻找什么。)
Observable<Integer> observable = Observable.just(1)
.delay(5, TimeUnit.MILLISECONDS)
.compose(obs -> webClient.putAbs(""))
.rxSend()
.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 >.
Observable<Integer> observable = Observable.just(1)
.delay(5, TimeUnit.MILLISECONDS)
.compose(obs -> {
webClient.putAbs("url")
.rxSend()
.doOnSubscribe(() -> System.out.println("Subbing to client")) // to be removed with solution
.subscribe(); // I don't want to have to do a sub here.
return obs;
})
.doOnSubscribe(() -> System.out.println("the only single sub i want to have"));
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).
Observable<Integer> observable = Observable.just(1)
.delay(5, TimeUnit.MILLISECONDS)
.compose(obs -> webClient.putAbs(""))
.rxSend()
.toObservable();
Appreciate any guidance. Thanks.
答案1
得分: 0
这应该可以工作:
Observable<Integer> observable = Observable.just(1)
.delay(5, TimeUnit.MILLISECONDS)
.flatMap(obs -> webClient.putAbs("url").rxSend().map(a -> obs));
// .flatMap() 立即订阅了内部的 webClient 调用。
英文:
This should work:
Observable<Integer> observable = Observable.just(1)
.delay(5, TimeUnit.MILLISECONDS)
.flatMap(obs -> webClient.putAbs("url").rxSend().map(a -> obs));
.flatMap()
subscribes to the inner webClient
call eagerly.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论