---

# (DD04) Business Logic — Example.asyncGet() [16 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.asyncGet()

This method performs an asynchronous HTTP GET request against the supplied URI and waits for the request lifecycle to finish before returning. Its business role is to demonstrate a non-blocking client interaction pattern for retrieving remote content, then handling either a transport failure or a successful response in a completion callback. The method acts as a small orchestration utility: it creates the HTTP client, builds the request, dispatches the request asynchronously, and centralizes completion handling in one place.

From a system-design perspective, this method implements a simple builder-and-callback flow. The request is assembled fluently, the outbound call is delegated to the JDK HTTP client, and result handling is performed through `whenComplete`, which cleanly separates success and failure handling. There are no business-domain branches beyond the completion-path split: if an exception is present, the stack trace is printed; otherwise, the response body and status code are written to standard output. In the larger system, this method serves as an example entry point for async GET behavior and is not a reusable domain service with persistence or state mutation responsibilities.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START["asyncGet(uri)"]
    BUILD_CLIENT["Create HttpClient via HttpClient.newHttpClient()"]
    BUILD_REQUEST["Create HttpRequest via HttpRequest.newBuilder()"]
    SET_URI["Set request URI with URI.create(uri)"]
    BUILD_FINAL["Build HttpRequest"]
    SEND_ASYNC["Call client.sendAsync(request, BodyHandlers.ofString())"]
    WHEN_COMPLETE["Register whenComplete(resp, t)"]
    CHECK_THROWABLE{"t != null?"}
    PRINT_ERROR["t.printStackTrace()"]
    PRINT_BODY["System.out.println(resp.body())"]
    PRINT_STATUS["System.out.println(resp.statusCode())"]
    JOIN["Call join() and block until completion"]
    END["Return"]
    START --> BUILD_CLIENT
    BUILD_CLIENT --> BUILD_REQUEST
    BUILD_REQUEST --> SET_URI
    SET_URI --> BUILD_FINAL
    BUILD_FINAL --> SEND_ASYNC
    SEND_ASYNC --> WHEN_COMPLETE
    WHEN_COMPLETE --> CHECK_THROWABLE
    CHECK_THROWABLE -->|Yes| PRINT_ERROR
    CHECK_THROWABLE -->|No| PRINT_BODY
    PRINT_BODY --> PRINT_STATUS
    PRINT_STATUS --> JOIN
    PRINT_ERROR --> JOIN
    JOIN --> END
```

**CRITICAL — Constant Resolution:**

No project constants are referenced in this method. The flow is driven entirely by the input URI and JDK HTTP client APIs.

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| 1 | `uri` | `String` | Target endpoint location for the outbound HTTP GET request. It represents the remote resource address to be contacted by the HTTP client. Any valid URI string may be supplied; the method converts it into a `java.net.URI` and uses it to route the request. |

**Instance fields or external state read by the method:** none. The method operates only on its parameter and local variables, and it writes its outcome to standard output.

## 4. CRUD Operations / Called Services

This method does not perform database CRUD operations and does not call application-specific SC/CBS services. Its method calls are limited to JDK HTTP client APIs and console output.

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `URI.create` | N/A | N/A | Converts the provided text into a URI object for request routing. |
| C | `HttpClient.newHttpClient` | N/A | N/A | Creates a new HTTP client instance for outbound communication. |
| C | `HttpRequest.newBuilder` | N/A | N/A | Starts construction of a new HTTP request object. |
| U | `requestBuilder.uri` | N/A | N/A | Assigns the target URI into the request definition. |
| C | `requestBuilder.build` | N/A | N/A | Finalizes the HTTP request to be sent. |
| C | `client.sendAsync` | N/A | N/A | Dispatches the GET request asynchronously and produces a future for completion handling. |
| R | `responseCompletableFuture.whenComplete` | N/A | N/A | Registers a completion callback to inspect success or failure. |
| R | `t.printStackTrace` | N/A | N/A | Reads the exception state and outputs diagnostics when the async call fails. |
| R | `resp.body` | N/A | N/A | Reads the response payload after a successful call. |
| R | `resp.statusCode` | N/A | N/A | Reads the HTTP status code after a successful call. |
| R | `join` | N/A | N/A | Waits for completion of the asynchronous execution before returning. |

## 5. Dependency Trace

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | `Example.main` | `Example.main` -> `Example.asyncGet` | `client.sendAsync [C] N/A` |
| 2 | Commented example invocation | `Example.main` -> `asyncGet("https://biezhi.me")` -> `Example.asyncGet` | `client.sendAsync [C] N/A` |

## 6. Per-Branch Detail Blocks

**Block 1** — [TRY] `(start async GET execution)` (L43)

> Initializes the HTTP client and prepares an outbound request to the remote endpoint specified by the caller.

| # | Type | Code |
|---|------|------|
| 1 | SET | `HttpClient client = HttpClient.newHttpClient();` |
| 2 | SET | `HttpRequest request = HttpRequest.newBuilder()` |
| 3 | EXEC | `.uri(URI.create(uri))` // converts the input string into a URI object |
| 4 | EXEC | `.build();` // finalizes the request |

**Block 1.1** — [EXECUTION] `(dispatch async request)` (L47)

| # | Type | Code |
|---|------|------|
| 1 | SET | `CompletableFuture<HttpResponse<String>> responseCompletableFuture = client.sendAsync(request, HttpResponse.BodyHandlers.ofString());` |
| 2 | CALL | `client.sendAsync(request, HttpResponse.BodyHandlers.ofString())` |

**Block 1.2** — [CALLBACK REGISTRATION] `(whenComplete(resp, t))` (L48)

> Registers post-processing logic for both the failure and success outcomes of the asynchronous call.

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

**Block 1.2.1** — [IF] `(t != null)` (L49)

> Handles request failure by printing the exception stack trace.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `t.printStackTrace();` // prints diagnostic information for the failure |

**Block 1.2.2** — [ELSE] `(t == null)` (L51)

> Handles successful completion by emitting the response payload and HTTP status code.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `System.out.println(resp.body());` // prints the remote response body |
| 2 | EXEC | `System.out.println(resp.statusCode());` // prints the HTTP status returned by the server |

**Block 1.3** — [EXECUTION] `(join completion)` (L54)

| # | Type | Code |
|---|------|------|
| 1 | CALL | `... .join();` // blocks until the async pipeline completes |

**Block 2** — [RETURN] `(method exit)` (L55)

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

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `uri` | Field | Target resource address for the outbound HTTP request. |
| HTTP | Acronym | Hypertext Transfer Protocol — standard protocol for web-based client/server communication. |
| GET | Acronym | HTTP method used to retrieve data from a remote resource without modifying it. |
| `HttpClient` | Technical term | JDK client used to send HTTP requests. |
| `HttpRequest` | Technical term | JDK request object that defines the target URI and request metadata. |
| `URI` | Acronym | Uniform Resource Identifier — standardized address format for a remote resource. |
| `CompletableFuture` | Technical term | Asynchronous result container that completes when the HTTP call finishes. |
| `whenComplete` | Technical term | Completion callback that runs on both success and failure paths. |
| `join` | Technical term | Blocking wait operation that returns after the asynchronous work is done. |
| `resp` | Technical term | Response object returned by the HTTP client after a successful request. |
| `t` | Technical term | Throwable received when the asynchronous request fails. |
| `statusCode` | Field/Method concept | HTTP response status indicating success or failure outcome. |
| `body` | Field/Method concept | Response payload returned by the remote endpoint. |
