-
Notifications
You must be signed in to change notification settings - Fork 7.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature Request: Add Observable.usingWhen()
for resources generated by a Publisher
#7548
Comments
One way to handle resource cleanup in RxJava is to use the using operator to create a Disposable object that handles the cleanup logic. You can combine this with the Single.defer operator to create a single that generates the resource and executes your query, with the Disposable taking care of the cleanup. Here's an exmp: In this example, the defer operator is used to delay the creation of the connection until the Single is subscribed to. The blockingGet method is used to block and wait for the connection to be created. Then, executeQuery is called with the connection, and the doFinally operator is used to clean up the connection when the Single completes or errors out. You could also create a custom operator that wraps the using operator and converts the resulting Mono to a Single. However, this would require more code than the previous example and may not be necessary for your use case. I hope this helps! :) |
@KarboniteKream Thanks for the feedback. I'm still thinking about the implementation details and support considerations. Stay tuned. @Mahammadnajaf Suggesting blocking operations is 99.99% wrong. |
well In this code, the blockingGet method is only used once to retrieve the Connection object, which should be fine as long as it is done in a non-blocking context (such as during application startup or initialization). However, if you need to avoid blocking operations entirely, you could modify the code to use reactive streams instead of blocking operations. Here's an example: Single result = Single.defer(() -> { In this example, the create method returns a Mono that emits a single Connection object when it is available. The flatMap operator is used to execute the executeQuery method with the Connection object, and the doFinally operator is used to close the Connection when the Single completes or errors out. This code should not block, as all operations are done in a reactive, non-blocking manner. |
Version: 3.1.6
In Reactor, there is
Mono.usingWhen()
, which works similarly toMono.using()
, but supports resources that are generated and cleaned up with aPublisher
.For example, here's a use case in
r2dbc-pool
:In RxJava, we have
Single.using()
, which works the same way asMono.using()
, so there's no support for reactive resource generation/cleanup. I was thinking of doing something like this:However, this does not handle cases like connection cleanup on error, dispose or terminate. The
.doOnXyz()
methods would not suffice, so I'm guessing this would require a customObservable
implementation, unless I'm missing something obvious. I've also checkedRxJavaExtensions
for anything similar to what I'm trying to achieve, but I was not able to find anything, nor could I find anything relevant on Stack Overflow.I currently have the option of just using
Mono.usingWhen()
and then convert it to aSingle
, but it would be nice to have this natively available in RxJava. Would you be willing to add support forusingWhen()
?The text was updated successfully, but these errors were encountered: