-
Bug
-
Resolution: Fixed
-
P3
-
None
-
b06
-
Not verified
The API documentation of BodySubscribers::mapping says:
```
* <p> The mapping function is executed using the client's {@linkplain
* HttpClient#executor() executor}, and can therefore be used to map any
* response body type, including blocking {@link InputStream}, as shown
* in the following example which uses a well-known JSON parser to
* convert an {@code InputStream} into any annotated Java type.
*
* <p>For example:
* <pre> {@code public static <W> BodySubscriber<W> asJSON(Class<W> targetType) {
* BodySubscriber<InputStream> upstream = BodySubscribers.ofInputStream();
*
* BodySubscriber<W> downstream = BodySubscribers.mapping(
* upstream,
* (InputStream is) -> {
* try (InputStream stream = is) {
* ObjectMapper objectMapper = new ObjectMapper();
* return objectMapper.readValue(stream, targetType);
* } catch (IOException e) {
* throw new UncheckedIOException(e);
* }
* });
* return downstream;
* } }</pre>
```
Blocking an executor thread can lead to deadlocks, or starve the HttpClient's executor of threads, and therefore should not be promoted by the API documentation.
Instead we should promote e.g. returning a Supplier<W> in the case where creating an instance of W would block.
```
* <p> The mapping function is executed using the client's {@linkplain
* HttpClient#executor() executor}, and can therefore be used to map any
* response body type, including blocking {@link InputStream}, as shown
* in the following example which uses a well-known JSON parser to
* convert an {@code InputStream} into any annotated Java type.
*
* <p>For example:
* <pre> {@code public static <W> BodySubscriber<W> asJSON(Class<W> targetType) {
* BodySubscriber<InputStream> upstream = BodySubscribers.ofInputStream();
*
* BodySubscriber<W> downstream = BodySubscribers.mapping(
* upstream,
* (InputStream is) -> {
* try (InputStream stream = is) {
* ObjectMapper objectMapper = new ObjectMapper();
* return objectMapper.readValue(stream, targetType);
* } catch (IOException e) {
* throw new UncheckedIOException(e);
* }
* });
* return downstream;
* } }</pre>
```
Blocking an executor thread can lead to deadlocks, or starve the HttpClient's executor of threads, and therefore should not be promoted by the API documentation.
Instead we should promote e.g. returning a Supplier<W> in the case where creating an instance of W would block.
- relates to
-
JDK-8217264 HttpClient: Blocking operations in mapper function do not work as documented
-
- Closed
-
-
JDK-8217799 HttpClient: consider adding an HttpResponse::map method to map an HttpResponse<T> to an HttpResponse<U>
-
- Open
-