-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Add async try-with-resources for java api #6357
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
Conversation
| public static <T extends AutoCloseable, U> CompletionStage<U> asyncTryWithResource( | ||
| T resource, Function<T, CompletionStage<U>> body | ||
| ) { | ||
| CompletionStage<U> completionStage = body.apply(resource); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the function body fails to prepare the CompletionStage, the close() is never called.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I fixed it now and added the test case
2c9a665 to
74217eb
Compare
| CompletionStage<U> completionStage = body.apply(resource); | ||
| completionStage.whenCompleteAsync((u, throwable) -> tryCloseResource(resource)); | ||
| return completionStage; | ||
| } catch (Exception e) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice if we didn't have to catch and re-throw the exception here. Why not use a try-finally?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that it is cleaner to use try-finally, however, in this case that would mean that we close the resource while the async task is potentially still running and using the resource.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, we would need to avoid closing it twice. If the async task is running, we don't want to close it. But maybe we can just avoid wrapping if we don't need to:
} catch (RuntimeException e) {
tryCloseResource(resource);
throw e;
} catch (Throwable t) {
tryCloseResource(resource);
throw new RuntimeException(t);
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yeah, closing twice would be an issue as well. Anyway, I've changed it now to only wrap it if necessary as you suggested. I did the same for the close expcetion handler just to make sure.
74217eb to
aad20d5
Compare
|
Should we backport this to 2.5.x? |
|
2.5.x: e7d1ccd |
Pull Request Checklist
Helpful things
Fixes
Fixes #5547
Purpose
Introduces function for async try-with-resource
Background Context
References