---

# (DD02) 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 HTTP/2 client execution path that connects to a public demo endpoint and verifies that the Java HTTP client stack can negotiate and consume an HTTP/2 response. It constructs an `HttpClient` with redirect handling enabled, forces the client version to HTTP/2, builds a GET request for `https://http2.akamai.com/demo`, and submits the request asynchronously. The method then waits for completion, printing either the exception stack trace on failure or the response body and HTTP status code on success.

From a business perspective, the method functions as a runtime connectivity and protocol capability sample rather than a domain transaction. It is a reusable technical example that can be invoked from the class entry point to confirm outbound network access, response handling, and asynchronous completion behavior. The control flow implements a straightforward builder-and-dispatch pattern: configure client, configure request, dispatch asynchronously, then branch on completion outcome.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
START["http2()"]
B1["Create HttpClient builder"]
B2["Set redirect policy to NORMAL"]
B3["Set HTTP version to HTTP_2"]
B4["Build HttpClient"]
B5["Build HttpRequest for https://http2.akamai.com/demo"]
B6["Set GET method"]
B7["Build request"]
B8["Send async request with BodyHandlers.ofString()"]
B9["Complete future with whenComplete(resp, t)"]
D1{"t != null?"}
B10["Print stack trace"]
B11["Print response body"]
B12["Print response status code"]
END["join() and return"]
START --> B1
B1 --> B2
B2 --> B3
B3 --> B4
B4 --> B5
B5 --> B6
B6 --> B7
B7 --> B8
B8 --> B9
B9 --> D1
D1 -->|Yes| B10
D1 -->|No| B11
B11 --> B12
B10 --> END
B12 --> END
```

**CRITICAL — Constant Resolution:**
- `HttpClient.Redirect.NORMAL` = standard redirect-following behavior for normal HTTP redirect responses.
- `HttpClient.Version.HTTP_2` = explicit request to use HTTP/2 as the transport protocol.

## 3. Parameter Analysis

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

Instance fields or external state read by the method:
- No instance fields are read.
- The method depends on outbound network access to the public endpoint `https://http2.akamai.com/demo`.
- The method also depends on the Java runtime HTTP client implementation and console output for diagnostics.

## 4. CRUD Operations / Called Services

Analyze all method calls within this method and classify each as a CRUD operation.

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `sendAsync` | - | HTTP endpoint `https://http2.akamai.com/demo` | Issues an outbound HTTP GET request and reads the remote response asynchronously. |
| R | `whenComplete` | - | CompletableFuture result | Observes the completed response or failure signal and routes execution accordingly. |
| R | `join` | - | CompletableFuture result | Waits for the asynchronous request to finish before returning. |
| R | `body` | - | HTTP response payload | Reads the response body for console output. |
| R | `statusCode` | - | HTTP response metadata | Reads the HTTP status code for console output. |
| R | `printStackTrace` | - | Throwable diagnostic output | Reads the failure state and prints diagnostic details when the request completes exceptionally. |

## 5. Dependency Trace

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

## 6. Per-Branch Detail Blocks

**Block 1** — [SEQUENCE] (L154-L171)

> Builds an HTTP/2 client, sends a GET request to a public demo server, and prints either the failure details or the successful response details.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `HttpClient.newBuilder();` |
| 2 | CALL | `.followRedirects(HttpClient.Redirect.NORMAL);` |
| 3 | CALL | `.version(HttpClient.Version.HTTP_2);` |
| 4 | CALL | `.build();` |
| 5 | CALL | `.sendAsync(HttpRequest.newBuilder().uri(new URI("https://http2.akamai.com/demo")).GET().build(), HttpResponse.BodyHandlers.ofString());` |
| 6 | CALL | `.whenComplete((resp, t) -> { ... });` |
| 7 | CALL | `.join();` |

**Block 1.1** — [IF] `(t != null)` (L165)

> Handles the exceptional completion path for the asynchronous HTTP request.

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

**Block 1.2** — [ELSE] `(t == null)` (L167-L170)

> Handles the successful completion path and emits the received payload and transport status.

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

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `http2` | Method name | Demonstration routine for making an HTTP/2 request and printing the result. |
| HTTP | Acronym | HyperText Transfer Protocol — the application protocol used for web requests. |
| HTTP/2 | Business term | Second major version of HTTP with multiplexing and protocol efficiency improvements. |
| `HttpClient` | Technical term | Java client used to send outbound HTTP requests. |
| `Redirect.NORMAL` | Constant | Redirect policy that follows standard redirects when the server asks the client to move. |
| `HTTP_2` | Constant | Explicit protocol selection indicating the request should use HTTP/2. |
| `sendAsync` | Technical term | Asynchronous request dispatch that returns a future result. |
| `whenComplete` | Technical term | Completion callback that runs after the request succeeds or fails. |
| `join` | Technical term | Blocking wait for the asynchronous request to finish. |
| `URI` | Technical term | Uniform Resource Identifier — the address of the remote endpoint. |
| `GET` | HTTP verb | Read-only request method used to retrieve a resource. |
| `BodyHandlers.ofString()` | Technical term | Response body converter that collects the payload as a text string. |
| `resp` | Variable | Completed HTTP response object containing the returned body and status code. |
| `t` | Variable | Throwable representing an error that occurred during request completion. |
| `statusCode` | Response field | Numeric HTTP status returned by the remote server. |
| `body` | Response field | Text content returned by the remote server. |
| `printStackTrace` | Technical term | Standard exception logging method that prints diagnostic details to the console. |
| Akamai | Proper noun | Global content delivery and edge networking provider hosting the HTTP/2 demo endpoint. |
| demo endpoint | Business term | Public sample URL used to verify client connectivity and protocol behavior. |
