DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Momento Migrates Object Cache as a Service to Ampere® Altra®
  • Why I Ditched Redis for Cloudflare Durable Objects in My Rate Limiter
  • The Unreasonable Effectiveness of the Actor Model for Creating Agentic LLM Applications
  • Real-Object Detection at the Edge: AWS IoT Greengrass and YOLOv5

Trending

  • Testing Strategies for Web Development Code Generated by LLMs
  • OpenAPI, ORM, SVG, and Lottie
  • Parallel Kafka Batch Processing With Kotlin Coroutines in Spring Boot
  • Encryption Won't Survive Quantum Computing: What to Do?
  1. DZone
  2. Coding
  3. Languages
  4. Groovy Goodness: Making Sure Closeable Objects Are Closed [Snippet]

Groovy Goodness: Making Sure Closeable Objects Are Closed [Snippet]

See how Groovy handles classes and objects that implement the Closeable interface and how you can ensure your closed methods are invoked.

By 
Hubert Klein Ikkink user avatar
Hubert Klein Ikkink
·
Oct. 20, 17 · Code Snippet
Likes (1)
Comment
Save
Tweet
Share
11.4K Views

Join the DZone community and get the full member experience.

Join For Free

If a class implements the Closeable interface, Groovy adds the withCloseable method to the class. The withCloseable method has a closure as an argument. The code in the closure is executed and then the implementation of the close method of the Closeable interface is invoked. The Closeable object is passed as an argument to the closure so we can refer to it inside the closure.

In the following example, we have two objects that implement the Closeable interface. By using withCloseable, we know for sure the close method is invoked after all the code in the closure is executed:

@Grab(group='org.apache.httpcomponents', module='httpclient', version='4.5.3')
import org.apache.http.impl.client.HttpClientBuilder
import org.apache.http.client.methods.HttpGet

// HttpClientBuilder.create().build() returns a CloseableHttpClient
// that implements the Closeable interface. Therefore we can use
// the withCloseable method to make sure the client is closed
// after the closure code is executed. 
HttpClientBuilder.create().build().withCloseable { client ->
    final request = new HttpGet('http://www.mrhaki.com')

    // The execute method returns a CloseableHttpResponse object
    // that implements the Closeable interface. We can use
    // withCloseable method to make sure the response is closed
    // after the closure code is executed.
    client.execute(request).withCloseable { response ->
        assert response.statusLine.statusCode == 200
    }
}


Written with Groovy 2.4.12.

Groovy (programming language) Object (computer science)

Published at DZone with permission of Hubert Klein Ikkink. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Momento Migrates Object Cache as a Service to Ampere® Altra®
  • Why I Ditched Redis for Cloudflare Durable Objects in My Rate Limiter
  • The Unreasonable Effectiveness of the Actor Model for Creating Agentic LLM Applications
  • Real-Object Detection at the Edge: AWS IoT Greengrass and YOLOv5

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook