---

# (DD03) 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 request lifecycle using the Java 11 `HttpClient` API. It constructs a client configured to follow redirects and negotiate HTTP/2, then issues a GET request to the Akamai HTTP/2 demo endpoint. The method is primarily an instructional utility rather than a business transaction processor, and its role in the larger system is to showcase how the application can initiate modern HTTP communication from a standalone example entry point.

The processing pattern is linear and builder-driven: create the client, configure transport behavior, create the request, send it asynchronously, and wait for completion. The only conditional branch is the completion callback, which separates the success path from the failure path. On success, it prints the response body and HTTP status code to standard output; on failure, it prints the stack trace. There are no domain entities, persistence operations, or CRUD interactions in this method.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["http2()"])
    B1["Build HttpClient builder"]
    B2["Set followRedirects to NORMAL"]
    B3["Set protocol version to HTTP_2"]
    B4["Build HttpClient instance"]
    B5["Build HttpRequest for https://http2.akamai.com/demo"]
    B6["Set request method to GET"]
    B7["Send request asynchronously with String body handler"]
    C1{"Throwable t is not null"}
    B8["Print stack trace"]
    B9["Print response body"]
    B10["Print status code"]
    B11["Join async completion"]
    END(["Method exits"])

    START --> B1
    B1 --> B2
    B2 --> B3
    B3 --> B4
    B4 --> B5
    B5 --> B6
    B6 --> B7
    B7 --> C1
    C1 -->|Yes| B8
    C1 -->|No| B9
    B8 --> B11
    B9 --> B10
    B10 --> B11
    B11 --> END
```

**CRITICAL — Constant Resolution:**
No custom project constants are referenced in this method. The only symbolic constant is the JDK enum value `HttpClient.Version.HTTP_2`, which denotes the HTTP/2 transport protocol.

## 3. Parameter Analysis

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

This method does not accept parameters. Its behavior is driven entirely by locally constructed request objects and the fixed destination URI. External state is limited to console output and the remote HTTP endpoint response.

## 4. CRUD Operations / Called Services

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `sendAsync(...)` | N/A | HTTP endpoint `https://http2.akamai.com/demo` | Sends an outbound HTTP GET request and reads the remote response body asynchronously. |

The method does not call application service components, repositories, or database tables. The only outbound integration is a remote HTTP read operation against an external demo endpoint.

## 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] https://http2.akamai.com/demo` |

The caller search identified the example class itself as the only direct reference. This method is therefore best understood as a self-contained utility routine that can be invoked from the example application's `main` flow.

## 6. Per-Branch Detail Blocks

**Block 1** — **SET / EXEC chain** `(client construction)` (L154-L157)

> Builds a reusable HTTP client instance and configures transport behavior for the outbound request.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `HttpClient.newBuilder()` |
| 2 | EXEC | `.followRedirects(HttpClient.Redirect.NORMAL)` |
| 3 | EXEC | `.version(HttpClient.Version.HTTP_2)` |
| 4 | CALL | `.build()` |

**Block 2** — **SET / EXEC chain** `(request construction)` (L158-L161)

> Creates the HTTP request that targets the external demo endpoint and prepares it as a GET request.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `HttpRequest.newBuilder()` |
| 2 | EXEC | `.uri(new URI("https://http2.akamai.com/demo"))` |
| 3 | EXEC | `.GET()` |
| 4 | CALL | `.build()` |

**Block 3** — **CALL** `(asynchronous send)` (L162-L164)

> Dispatches the request asynchronously and requests the response body as a string.

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

**Block 4** — **TRY / CALLBACK IF** `(whenComplete callback)` (L165-L171)

> Handles the terminal outcome of the asynchronous HTTP call by separating failure from success.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `whenComplete((resp, t) -> { ... })` |
| 2 | IF | `if (t != null)` |
| 3 | EXEC | `t.printStackTrace();` |
| 4 | ELSE | `else` |
| 5 | EXEC | `System.out.println(resp.body());` |
| 6 | EXEC | `System.out.println(resp.statusCode());` |
| 7 | CALL | `.join()` |

**Block 4.1** — **IF** `(t != null)` (L166)

> Records the exception details when the outbound request fails.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `t.printStackTrace();` // prints diagnostic stack trace |

**Block 4.2** — **ELSE** `(response completed successfully)` (L168-L171)

> Prints the remote payload and the HTTP status code returned by the server.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `System.out.println(resp.body());` // outputs response content |
| 2 | EXEC | `System.out.println(resp.statusCode());` // outputs HTTP status |

**Block 5** — **RETURN** `(method exits)` (L172)

> The method completes after the asynchronous pipeline is joined and no further business logic remains.

| # | Type | Code |
|---|------|------|
| 1 | RETURN | `}` |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `http2` | Method | Example routine that demonstrates an HTTP/2 client call to an external endpoint. |
| `HttpClient` | Technical term | Java HTTP client used to build and send outbound web requests. |
| `HttpRequest` | Technical term | Immutable outbound request object containing the URI and HTTP method. |
| `HTTP_2` | Acronym / protocol | HTTP version 2, a multiplexed web transport protocol. |
| `NORMAL` | Technical term | Redirect policy that follows redirects in the standard, limited manner. |
| `sendAsync` | Technical term | Non-blocking request execution that returns a completion stage. |
| `whenComplete` | Technical term | Callback that handles success or failure after asynchronous execution. |
| `join` | Technical term | Waits for the asynchronous computation to finish. |
| `URI` | Acronym | Uniform Resource Identifier — identifies the target network resource. |
| `resp` | Variable | HTTP response object containing the returned body and status code. |
| `t` | Variable | Throwable instance representing request failure, if any. |
| `body` | Field / response data | Response payload returned by the remote service. |
| `statusCode` | Field / response data | Numeric HTTP status returned by the remote service. |
| Akamai | Business term | External content delivery and demo host used here as the HTTP/2 sample endpoint. |
