From 02def12bd20be7fcb7600903ab70c0f3950a934d Mon Sep 17 00:00:00 2001 From: Sergio del Amo Date: Fri, 19 Jan 2024 17:41:51 +0100 Subject: [PATCH] Remove StreamedFile mention (#1400) * Remove StreamedFile mention * add ReferenceCounted * update callout number * change callout callout one line * rename guide to micronaut-reactor-streaming-http-client * add license --- .../example/micronaut/HomeController.java | 69 ++++++++++++ .../example/micronaut/HomeControllerTest.java | 0 .../metadata.json | 2 +- ...ronaut-reactor-streaming-http-client.adoc} | 5 +- .../src/test/resources/micronaut5K.png | Bin .../example/micronaut/HomeController.java | 104 ------------------ ...out-streaming-http-client-data-stream.adoc | 2 +- 7 files changed, 73 insertions(+), 109 deletions(-) create mode 100644 guides/micronaut-reactor-streaming-http-client/java/src/main/java/example/micronaut/HomeController.java rename guides/{micronaut-streamed-file-and-reactor-streaming-http-client => micronaut-reactor-streaming-http-client}/java/src/test/java/example/micronaut/HomeControllerTest.java (100%) rename guides/{micronaut-streamed-file-and-reactor-streaming-http-client => micronaut-reactor-streaming-http-client}/metadata.json (85%) rename guides/{micronaut-streamed-file-and-reactor-streaming-http-client/micronaut-streamed-file-and-reactor-streaming-http-client.adoc => micronaut-reactor-streaming-http-client/micronaut-reactor-streaming-http-client.adoc} (90%) rename guides/{micronaut-streamed-file-and-reactor-streaming-http-client => micronaut-reactor-streaming-http-client}/src/test/resources/micronaut5K.png (100%) delete mode 100644 guides/micronaut-streamed-file-and-reactor-streaming-http-client/java/src/main/java/example/micronaut/HomeController.java diff --git a/guides/micronaut-reactor-streaming-http-client/java/src/main/java/example/micronaut/HomeController.java b/guides/micronaut-reactor-streaming-http-client/java/src/main/java/example/micronaut/HomeController.java new file mode 100644 index 0000000000..4e64e0410e --- /dev/null +++ b/guides/micronaut-reactor-streaming-http-client/java/src/main/java/example/micronaut/HomeController.java @@ -0,0 +1,69 @@ +/* + * Copyright 2017-2024 original authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package example.micronaut; + +import io.micronaut.context.exceptions.ConfigurationException; +import io.micronaut.core.io.buffer.ByteBuffer; +import io.micronaut.core.io.buffer.ReferenceCounted; +import io.micronaut.http.HttpRequest; +import io.micronaut.http.annotation.Controller; +import io.micronaut.http.annotation.Get; +import io.micronaut.reactor.http.client.ReactorStreamingHttpClient; +import jakarta.annotation.PreDestroy; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import reactor.core.publisher.Flux; + +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URL; + +@Controller // <1> +class HomeController implements AutoCloseable { + private static final Logger LOG = LoggerFactory.getLogger(HomeController.class); + private static final URI DEFAULT_URI = URI.create("https://guides.micronaut.io/micronaut5K.png"); + + private final ReactorStreamingHttpClient reactorStreamingHttpClient; + + HomeController() { + String urlStr = "https://guides.micronaut.io/"; + URL url; + try { + url = new URL(urlStr); + } catch (MalformedURLException e) { + throw new ConfigurationException("malformed URL" + urlStr); + } + this.reactorStreamingHttpClient = ReactorStreamingHttpClient.create(url); // <2> + } + + @Get // <3> + Flux> download() { + HttpRequest request = HttpRequest.GET(DEFAULT_URI); + return reactorStreamingHttpClient.dataStream(request).doOnNext(bb -> { + if (bb instanceof ReferenceCounted rc) { + rc.retain(); + } + }); // <4> + } + + @PreDestroy // <5> + @Override + public void close() { + if (reactorStreamingHttpClient != null) { + reactorStreamingHttpClient.close(); + } + } +} diff --git a/guides/micronaut-streamed-file-and-reactor-streaming-http-client/java/src/test/java/example/micronaut/HomeControllerTest.java b/guides/micronaut-reactor-streaming-http-client/java/src/test/java/example/micronaut/HomeControllerTest.java similarity index 100% rename from guides/micronaut-streamed-file-and-reactor-streaming-http-client/java/src/test/java/example/micronaut/HomeControllerTest.java rename to guides/micronaut-reactor-streaming-http-client/java/src/test/java/example/micronaut/HomeControllerTest.java diff --git a/guides/micronaut-streamed-file-and-reactor-streaming-http-client/metadata.json b/guides/micronaut-reactor-streaming-http-client/metadata.json similarity index 85% rename from guides/micronaut-streamed-file-and-reactor-streaming-http-client/metadata.json rename to guides/micronaut-reactor-streaming-http-client/metadata.json index 2d2160e055..c588518695 100644 --- a/guides/micronaut-streamed-file-and-reactor-streaming-http-client/metadata.json +++ b/guides/micronaut-reactor-streaming-http-client/metadata.json @@ -1,6 +1,6 @@ { "title": "Download a big file with StreamingHttpClient", - "intro": "Learn how to stream responses with ReactorStreamingHttpClient, and how to use it in combination with StreamedFile.", + "intro": "Learn how to stream responses with ReactorStreamingHttpClient", "authors": ["Sergio del Amo"], "tags": [], "categories": ["Beyond the Basics"], diff --git a/guides/micronaut-streamed-file-and-reactor-streaming-http-client/micronaut-streamed-file-and-reactor-streaming-http-client.adoc b/guides/micronaut-reactor-streaming-http-client/micronaut-reactor-streaming-http-client.adoc similarity index 90% rename from guides/micronaut-streamed-file-and-reactor-streaming-http-client/micronaut-streamed-file-and-reactor-streaming-http-client.adoc rename to guides/micronaut-reactor-streaming-http-client/micronaut-reactor-streaming-http-client.adoc index 962ae0556f..f3162cac1e 100644 --- a/guides/micronaut-streamed-file-and-reactor-streaming-http-client/micronaut-streamed-file-and-reactor-streaming-http-client.adoc +++ b/guides/micronaut-reactor-streaming-http-client/micronaut-reactor-streaming-http-client.adoc @@ -16,9 +16,8 @@ source:HomeController[] callout:controller[number=1,arg0=/] callout:reactor-streaming-http-client[2] callout:get-generic[3] -callout:streamed-file[4] -callout:streaming-http-client-data-stream[5] -callout:predestroy[6] +callout:streaming-http-client-data-stream[4] +callout:predestroy[5] == Test diff --git a/guides/micronaut-streamed-file-and-reactor-streaming-http-client/src/test/resources/micronaut5K.png b/guides/micronaut-reactor-streaming-http-client/src/test/resources/micronaut5K.png similarity index 100% rename from guides/micronaut-streamed-file-and-reactor-streaming-http-client/src/test/resources/micronaut5K.png rename to guides/micronaut-reactor-streaming-http-client/src/test/resources/micronaut5K.png diff --git a/guides/micronaut-streamed-file-and-reactor-streaming-http-client/java/src/main/java/example/micronaut/HomeController.java b/guides/micronaut-streamed-file-and-reactor-streaming-http-client/java/src/main/java/example/micronaut/HomeController.java deleted file mode 100644 index f0f02beefb..0000000000 --- a/guides/micronaut-streamed-file-and-reactor-streaming-http-client/java/src/main/java/example/micronaut/HomeController.java +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright 2017-2024 original authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package example.micronaut; - -import io.micronaut.context.exceptions.ConfigurationException; -import io.micronaut.core.annotation.Nullable; -import io.micronaut.http.HttpRequest; -import io.micronaut.http.MediaType; -import io.micronaut.http.annotation.Controller; -import io.micronaut.http.annotation.Get; -import io.micronaut.http.annotation.QueryValue; -import io.micronaut.http.server.types.files.StreamedFile; -import io.micronaut.reactor.http.client.ReactorStreamingHttpClient; -import jakarta.annotation.PreDestroy; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.IOException; -import java.io.PipedInputStream; -import java.io.PipedOutputStream; -import java.net.MalformedURLException; -import java.net.URI; -import java.net.URL; - -@Controller // <1> -class HomeController implements AutoCloseable { - - private static final Logger LOG = LoggerFactory.getLogger(HomeController.class); - private static final URI DEFAULT_URI = URI.create("https://guides.micronaut.io/micronaut5K.png"); - - private final ReactorStreamingHttpClient reactorStreamingHttpClient; - - HomeController() { - String urlStr = "https://guides.micronaut.io/"; - URL url; - try { - url = new URL(urlStr); - } catch (MalformedURLException e) { - throw new ConfigurationException("malformed URL" + urlStr); - } - this.reactorStreamingHttpClient = ReactorStreamingHttpClient.create(url); // <2> - } - - @Get("{?q}") // <3> - StreamedFile download(@Nullable @QueryValue String q) throws IOException { // <4> - URI uri = q == null ? DEFAULT_URI : URI.create(q); - HttpRequest request = HttpRequest.GET(uri); - - LOG.trace("downloading: {}", uri); - PipedOutputStream outputStream = new PipedOutputStream(); - dataStreamToOutputStream(request, outputStream, () -> LOG.trace("finished download")); - - PipedInputStream inputStream = new PipedInputStream(1024*10); - inputStream.connect(outputStream); - - return new StreamedFile(inputStream, MediaType.IMAGE_PNG_TYPE); - } - - private void dataStreamToOutputStream(HttpRequest request, - PipedOutputStream outputStream, - Runnable finallyRunnable) { - reactorStreamingHttpClient.dataStream(request) // <5> - .doOnNext(byteBuffer -> { - LOG.trace("Saving byte array"); - try { - outputStream.write(byteBuffer.toByteArray()); - } catch (IOException e) { - LOG.error("IO Exception closing the stream", e); - } - }) - .doFinally(signalType -> { - try { - LOG.trace("Closing OutputStream"); - outputStream.close(); - } catch (IOException e) { - LOG.error("IO Exception closing the stream", e); - System.out.println(); - } - finallyRunnable.run(); - }) - .subscribe(); - } - - @PreDestroy // <6> - @Override - public void close() { - if (reactorStreamingHttpClient != null) { - reactorStreamingHttpClient.close(); - } - } -} diff --git a/src/docs/common/callouts/callout-streaming-http-client-data-stream.adoc b/src/docs/common/callouts/callout-streaming-http-client-data-stream.adoc index c08c4717e9..278f9ed985 100644 --- a/src/docs/common/callouts/callout-streaming-http-client-data-stream.adoc +++ b/src/docs/common/callouts/callout-streaming-http-client-data-stream.adoc @@ -1 +1 @@ -Request a stream of data where each emitted item is a `ByteBuffer` instance. \ No newline at end of file +The `dataStream` method emits instances of `ByteBuffer`. These instances are garbage collected after each chunk is emitted. In order to propagate the chunks to the response stream they need to be retained by calling the `retain()` method. The framework will call `release()` on these garbage collected chunks once each one is written to the response. \ No newline at end of file