---

# (DD32) Business Logic — Example.http2() [19 LOC]

| Field | Value |
|-------|-------|
| Fully Qualified Name | `io.github.biezhi.java11.http.Example` |
| Layer | Utility |
| Module | `http` (Package: `io.github.biezhi.java11.http`) |

## 1. Role

### Example.http2()

This method demonstrates an end-to-end HTTP/2 client invocation against a public demo endpoint. Its business role is to verify that the runtime can build an HTTP client, issue a GET request, and consume the response asynchronously using Java 11 HTTP APIs. The method is not a domain transaction processor; instead, it is an executable sample used to validate connectivity, protocol selection, redirect handling, and asynchronous completion behavior.

The method implements a simple request/response routing pattern: it configures an `HttpClient`, constructs a single `HttpRequest`, sends the request with a string body handler, and then processes the completion outcome in a callback. If the request fails, it prints the stack trace for diagnostics. If the request succeeds, it prints the response body and status code to standard output. The overall role in the larger system is that of a reference utility and runtime showcase for HTTP/2 client usage rather than a reusable business service.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["http2()"])
    N1["Build HttpClient with NORMAL redirects"]
    N2["Set HttpClient version to HTTP_2"]
    N3["Build HttpClient instance"]
    N4["Create HttpRequest for https://http2.akamai.com/demo"]
    N5["Set request method to GET"]
    N6["Build HttpRequest instance"]
    N7["Send request asynchronously with String body handler"]
    N8["Register whenComplete callback"]
    D1{"Throwable t is not null"}
    N9["Print stack trace"]
    N10["Print response body"]
    N11["Print response status code"]
    N12["Join completion and block until finished"]
    END_NODE(["Return void"])
    START --> N1
    N1 --> N2
    N2 --> N3
    N3 --> N4
    N4 --> N5
    N5 --> N6
    N6 --> N7
    N7 --> N8
    N8 --> D1
    D1 -->|Yes| N9
    D1 -->|No| N10
    N9 --> N12
    N10 --> N11
    N11 --> N12
    N12 --> END_NODE
```

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| - | (none) | - | - |

External state read by the method: the method uses only locally constructed HTTP client/request objects and the literal target URI `https://http2.akamai.com/demo`. It also reads the asynchronous completion outcome (`resp` and `t`) inside the callback and writes diagnostic output to standard output/error streams.

## 4. CRUD Operations / Called Services

This method does not perform application CRUD against a database or entity store. It invokes Java platform networking APIs and performs an external HTTP read operation against a public endpoint.

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `sendAsync` | N/A | External HTTP endpoint `https://http2.akamai.com/demo` | Reads content from a remote HTTP/2 demo service using a GET request and returns the response body as a string |
| R | `whenComplete` | N/A | N/A | Observes the asynchronous result and branches on success or failure |
| R | `join` | N/A | N/A | Waits for the asynchronous HTTP exchange to complete before the method exits |

## 5. Dependency Trace

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | None found | `Example.http2()` | `sendAsync [R] External HTTP endpoint https://http2.akamai.com/demo` |

## 6. Per-Branch Detail Blocks

**Block 1** — [SEQUENTIAL SETUP] (L154-L161)

> Builds an HTTP client configured for normal redirects and HTTP/2, then prepares a GET request targeting the public demo URI.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `HttpClient.newBuilder()` |
| 2 | EXEC | `.followRedirects(HttpClient.Redirect.NORMAL)` |
| 3 | EXEC | `.version(HttpClient.Version.HTTP_2)` |
| 4 | CALL | `.build()` |
| 5 | CALL | `HttpRequest.newBuilder()` |
| 6 | EXEC | `.uri(new URI("https://http2.akamai.com/demo"))` |
| 7 | EXEC | `.GET()` |
| 8 | CALL | `.build()` |

**Block 2** — [CALL] `sendAsync(...)` (L160)

> Sends the request asynchronously and configures a string body handler so the full response payload can be logged on completion.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `.sendAsync(HttpRequest.newBuilder()...build(), HttpResponse.BodyHandlers.ofString())` |

**Block 3** — [CALL] `whenComplete((resp, t) -> ...)` (L162)

> Registers the completion callback that handles both error and success paths.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `.whenComplete((resp, t) -> { ... })` |

**Block 3.1** — [IF] `(t != null)` (L163)

> Failure handling branch for the asynchronous HTTP exchange.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `t.printStackTrace();` |

**Block 3.2** — [ELSE] `(t == null)` (L165)

> Success handling branch that writes the response payload and HTTP status to the console.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `System.out.println(resp.body());` |
| 2 | EXEC | `System.out.println(resp.statusCode());` |

**Block 4** — [CALL] `join()` (L169)

> Blocks the current thread until the asynchronous HTTP call and completion handler finish executing.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `.join();` |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `http2` | Method name | Sample routine that demonstrates an HTTP/2 client request lifecycle |
| `HttpClient` | Technical term | Java HTTP client used to configure protocol version, redirect policy, and async execution |
| `HttpRequest` | Technical term | Outbound request object that defines the destination URI and HTTP method |
| `HttpResponse` | Technical term | Response wrapper containing the returned body and HTTP status code |
| `HTTP_2` | Protocol term | HTTP/2 transport version, used to request multiplexed, modern HTTP behavior |
| `Redirect.NORMAL` | Technical term | Default redirect policy that follows safe redirects |
| `GET` | HTTP verb | Read-only retrieval operation for obtaining data from a remote endpoint |
| `BodyHandlers.ofString()` | Technical term | Response body converter that decodes the payload as text |
| `whenComplete` | Async callback | Completion handler that executes after the request succeeds or fails |
| `join` | Technical term | Blocking wait that prevents the method from exiting before the async request finishes |
| `resp` | Variable | Asynchronous HTTP response object containing body and status |
| `t` | Variable | Throwable representing request failure, if one occurs |
| `URI` | Technical term | Uniform Resource Identifier that identifies the remote HTTP endpoint |
| `stack trace` | Technical term | Diagnostic output that records the failure path for troubleshooting |
| `statusCode` | Method result | HTTP status returned by the remote server, used to confirm success or failure |
| `body` | Response field | Text payload returned by the remote endpoint |
| `http2.akamai.com/demo` | External endpoint | Public HTTP/2 demo service used to validate client connectivity and protocol behavior |
| `Utility` | Layer | Shared example/utility code that demonstrates platform behavior rather than domain business processing |
