-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adding AsyncCloseable with codesnippet. * Implementing AsyncCloseable and deleting AsyncAutoCloseable. * Add CHANGELOG entry. * Removing azure-core as an explicit dependency. * Fix use in AmqpReceiveLinkProcessor.
- Loading branch information
Showing
15 changed files
with
133 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 0 additions & 19 deletions
19
.../azure-core-amqp/src/main/java/com/azure/core/amqp/implementation/AsyncAutoCloseable.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,9 @@ | |
|
||
## 1.17.0-beta.1 (Unreleased) | ||
|
||
### Features Added | ||
|
||
- Added `AsyncCloseable` | ||
|
||
## 1.16.0 (2021-05-07) | ||
|
||
|
27 changes: 27 additions & 0 deletions
27
sdk/core/azure-core/src/main/java/com/azure/core/util/AsyncCloseable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.core.util; | ||
|
||
import reactor.core.publisher.Mono; | ||
|
||
/** | ||
* Interface for close operations that are asynchronous. | ||
* | ||
* <p><strong>Asynchronously closing a class</strong></p> | ||
* <p>In the snippet below, we have a long-lived {@code NetworkResource} class. There are some operations such | ||
* as closing {@literal I/O}. Instead of returning a sync {@code close()}, we use {@code closeAsync()} so users' | ||
* programs don't block waiting for this operation to complete.</p> | ||
* | ||
* {@codesnippet com.azure.core.util.AsyncCloseable.closeAsync} | ||
*/ | ||
public interface AsyncCloseable { | ||
/** | ||
* 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. | ||
* | ||
* @return A Mono representing the close operation. If the close operation is unsuccessful, the Mono completes with | ||
* an error. | ||
*/ | ||
Mono<Void> closeAsync(); | ||
} |
79 changes: 79 additions & 0 deletions
79
...ore/azure-core/src/samples/java/com/azure/core/util/AsyncCloseableJavaDocCodeSnippet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.core.util; | ||
|
||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
import reactor.core.publisher.Sinks; | ||
|
||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
import java.nio.charset.StandardCharsets; | ||
import java.time.Duration; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.stream.IntStream; | ||
|
||
/** | ||
* Code snippets for {@link AsyncCloseable}. | ||
*/ | ||
public class AsyncCloseableJavaDocCodeSnippet { | ||
public void asyncResource() throws IOException { | ||
// BEGIN: com.azure.core.util.AsyncCloseable.closeAsync | ||
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(); | ||
// END: com.azure.core.util.AsyncCloseable.closeAsync | ||
} | ||
|
||
/** | ||
* A long lived network resource. | ||
*/ | ||
static class NetworkResource implements AsyncCloseable { | ||
private final AtomicBoolean isClosed = new AtomicBoolean(); | ||
private final Sinks.Empty<Void> closeMono = Sinks.empty(); | ||
|
||
/** | ||
* Downloads a resource. | ||
* | ||
* @param url URL for the download. | ||
* | ||
* @return A stream of bytes. | ||
*/ | ||
Flux<ByteBuffer> longRunningDownload(String url) { | ||
final byte[] bytes = url.getBytes(StandardCharsets.UTF_8); | ||
|
||
// Does nothing real but it represents taking from this possibly infinite Flux until | ||
// the closeMono emits a signal. | ||
return Flux.fromStream(IntStream.range(0, bytes.length) | ||
.mapToObj(index -> ByteBuffer.wrap(bytes))) | ||
.takeUntilOther(closeMono.asMono()); | ||
} | ||
|
||
@Override | ||
public Mono<Void> closeAsync() { | ||
// If the close operation has started, then | ||
if (isClosed.getAndSet(true)) { | ||
return closeMono.asMono(); | ||
} | ||
|
||
return startAsyncClose().then(closeMono.asMono()); | ||
} | ||
|
||
private Mono<Void> startAsyncClose() { | ||
return Mono.delay(Duration.ofSeconds(10)).then() | ||
.doOnError(error -> closeMono.emitError(error, Sinks.EmitFailureHandler.FAIL_FAST)) | ||
.doOnSuccess(unused -> closeMono.emitEmpty(Sinks.EmitFailureHandler.FAIL_FAST)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters