Partager via


AsyncCloseable Interface

public interface AsyncCloseable

Interface for close operations that are asynchronous.

Asynchronously closing a class

In the snippet below, we have a long-lived NetworkResource class. There are some operations such as closing I/O. Instead of returning a sync close(), we use closeAsync() so users' programs don't block waiting for this operation to complete.

NetworkResource resource = new NetworkResource();
 resource.longRunningDownload("https://longdownload.com")
     .subscribe(
         byteBuffer -> System.out.println("Buffer received: " + byteBuffer),
         error -> System.err.printf("Error occurred while downloading: %s%n", error),
         () -> System.out.println("Completed download operation."));

 System.out.println("Press enter to stop downloading.");
 System.in.read();

 // We block here because it is the end of the main Program function. A real-life program may chain this
 // with some other close operations like save download/program state, etc.
 resource.closeAsync().block();

Method Summary

Modifier and Type Method and Description
abstract Mono<Void> closeAsync()

Begins the close operation.

Method Details

closeAsync

public abstract Mono closeAsync()

Begins the close operation. If one is in progress, will return that existing close operation. If the close operation is unsuccessful, the Mono completes with an error.

Returns:

A Mono representing the close operation. If the close operation is unsuccessful, the Mono completes with an error.

Applies to