Skip to content

Commit

Permalink
Remove StreamedFile mention (#1400)
Browse files Browse the repository at this point in the history
* Remove StreamedFile mention

* add ReferenceCounted

* update callout number

* change callout

callout one line

* rename guide to micronaut-reactor-streaming-http-client

* add license
  • Loading branch information
sdelamo authored Jan 19, 2024
1 parent 7f451ce commit 02def12
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 109 deletions.
Original file line number Diff line number Diff line change
@@ -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<ByteBuffer<?>> 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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
Request a stream of data where each emitted item is a `ByteBuffer` instance.
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.

0 comments on commit 02def12

Please sign in to comment.