Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@reneweb
Copy link
Contributor

@reneweb reneweb commented Jul 26, 2016

Pull Request Checklist

  • Have you read How to write the perfect pull request?
  • Have you read through the contributor guidelines?
  • Have you signed the Lightbend CLA?
  • Have you [squashed your commits]? (Optional, but makes merge messages nicer.)
  • Have you added copyright headers to new files?
  • Have you checked that both Scala and Java APIs are updated?
  • Have you updated the documentation for both Scala and Java sections?
  • Have you added tests for any changed functionality?

Helpful things

Fixes

Fixes #5547

Purpose

Introduces function for async try-with-resource

Background Context

References

public static <T extends AutoCloseable, U> CompletionStage<U> asyncTryWithResource(
T resource, Function<T, CompletionStage<U>> body
) {
CompletionStage<U> completionStage = body.apply(resource);
Copy link
Member

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.

Copy link
Contributor Author

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

@reneweb reneweb force-pushed the async-try-with-resource branch from 2c9a665 to 74217eb Compare July 27, 2016 19:19
CompletionStage<U> completionStage = body.apply(resource);
completionStage.whenCompleteAsync((u, throwable) -> tryCloseResource(resource));
return completionStage;
} catch (Exception e) {
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

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);
}

Copy link
Contributor Author

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.

@reneweb reneweb force-pushed the async-try-with-resource branch from 74217eb to aad20d5 Compare July 29, 2016 17:26
@gmethvin gmethvin merged commit b43fb53 into playframework:master Aug 1, 2016
@mkurz
Copy link
Member

mkurz commented Aug 1, 2016

Should we backport this to 2.5.x?

@gmethvin
Copy link
Member

gmethvin commented Aug 2, 2016

2.5.x: e7d1ccd

wsargent pushed a commit to wsargent/playframework that referenced this pull request Oct 25, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants