# (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 to a remote endpoint specified by the `uri` parameter. It serves as a Java 11 Http Client demonstration utility, illustrating how to leverage the `java.net.http` package's non-blocking capabilities for web service communication. The method creates a new `HttpClient` instance, constructs an `HttpRequest` using the builder pattern, and dispatches the request asynchronously via `sendAsync()`. Its role in the larger system is that of a shared utility example — it is not called by any other application code (no callers were found), but rather serves as a reference implementation for developers learning or demonstrating the Java 11 HTTP Client API's asynchronous request-response pattern. The comment above the method (`异步调用 GET`) translates to "Asynchronous call GET," confirming its purpose as a synchronous-vs-asynchronous HTTP demonstration alongside its counterpart `syncGet()` method.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["asyncGet(uri)"])
    A["Create HttpClient instance via newHttpClient()"] --> B["Build HttpRequest from uri using HttpRequest.newBuilder()"]
    B --> C["Set URI via URI.create(uri)"]
    C --> D["Send async request via client.sendAsync(request, BodyHandlers.ofString())"]
    D --> E["Register whenComplete callback with (resp, t) lambda"]
    E --> F["Block via join() waiting for completion"]
    F --> G["Callback evaluates result"]
    G -->|t != null: Error| H["Print exception stack trace via t.printStackTrace()"]
    G -->|t == null: Success| I["Print response body via resp.body()"]
    I --> J["Print response status code via resp.statusCode()"]
    H --> END(["Return from asyncGet"])
    J --> END
```

This method does not reference any constants — it performs a straightforward asynchronous HTTP GET. There are no conditional branches at the top level; the only branching occurs within the `whenComplete` callback which differentiates between success and failure paths.

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| 1 | `uri` | `String` | The full URL (Uniform Resource Identifier) of the remote HTTP endpoint to request. It is parsed via `URI.create()` and determines the target server, port, path, and query string of the GET request. Examples include any valid HTTP/HTTPS URL such as `https://example.com/api/resource`. |

No instance fields or external state are read by this method. It is a purely stateless utility method that creates its own local `HttpClient` and `HttpRequest` instances.

## 4. CRUD Operations / Called Services

This method does not perform any database CRUD operations. All calls are to the Java 11 standard library `java.net.http` API:

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| — | `HttpClient.newHttpClient()` | (JDK) | — | Creates a new default HTTP Client instance (JDK factory method, not an SC/CBS) |
| — | `HttpRequest.newBuilder()` | (JDK) | — | Creates a request builder for constructing an HTTP request (JDK fluent API) |
| — | `URI.create(uri)` | (JDK) | — | Parses the URI string into a URI object (JDK factory method) |
| — | `client.sendAsync(request, BodyHandlers.ofString())` | (JDK) | — | Dispatches an asynchronous HTTP GET request, returning a CompletableFuture |
| — | `responseCompletableFuture.whenComplete(...)` | (JDK) | — | Registers a completion callback handler for success/error |
| — | `responseCompletableFuture.join()` | (JDK) | — | Blocks the calling thread until the async request completes |
| — | `resp.body()` | (JDK) | — | Extracts the response body as a String (within callback) |
| — | `resp.statusCode()` | (JDK) | — | Extracts the HTTP response status code (within callback) |
| — | `t.printStackTrace()` | (JDK) | — | Prints the exception stack trace to standard error (within callback) |

## 5. Dependency Trace

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | — (no callers found) | N/A | N/A |

This method is defined as `public static` but no callers were found anywhere in the codebase. It appears to be a standalone demonstration method within the Java 11 HTTP Client examples collection, not integrated into any application workflow.

## 6. Per-Branch Detail Blocks

**Block 1** — [METHOD_ENTRY] `(entry point, L43)`

> Initialize the HTTP client and build the request.

| # | Type | Code |
|---|------|------|
| 1 | SET | `HttpClient client = HttpClient.newHttpClient();` // Create a new default HTTP client [-> JDK factory method] |
| 2 | SET | `HttpRequest request = HttpRequest.newBuilder().uri(URI.create(uri)).build();` // Build an HTTP GET request from the URI parameter [-> HttpRequest.Builder pattern] |

**Block 2** — [EXEC] `(async dispatch, L48)`

> Send the request asynchronously and wait for the result.

| # | Type | Code |
|---|------|------|
| 1 | SET | `CompletableFuture<HttpResponse<String>> responseCompletableFuture = client.sendAsync(request, HttpResponse.BodyHandlers.ofString());` // Dispatch async GET, register body handler [-> JDK java.net.http] |
| 2 | EXEC | `responseCompletableFuture.whenComplete((resp, t) -> { ... }).join();` // Register callback and block until completion [-> CompletableFuture chain] |

**Block 2.1** — [IF] `(t != null : callback has error, L50)`

> Within the `whenComplete` callback, check if an exception occurred during the async request.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `t.printStackTrace();` // Print the exception stack trace to stderr [-> Error handling path] |

**Block 2.2** — [ELSE] `(t == null : callback succeeded, L51)`

> The async request completed successfully; print the response.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `System.out.println(resp.body());` // Print the HTTP response body as a String [-> Response body output] |
| 2 | EXEC | `System.out.println(resp.statusCode());` // Print the HTTP status code (e.g., 200, 404, 500) [-> Response status output] |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `uri` | Parameter | Uniform Resource Identifier — the full web address of the target HTTP endpoint being requested |
| `HttpClient` | Class | Java 11 standard library class for sending HTTP requests. Created per-call in this example (no connection pooling). |
| `HttpRequest` | Class | Java 11 standard library class representing an HTTP request. Built using the fluent Builder pattern. |
| `HttpResponse` | Class | Java 11 standard library class representing an HTTP response, containing body and status code. |
| `CompletableFuture` | Class | Java util.concurrent class providing asynchronous computation support. Used here to handle the non-blocking response. |
| `sendAsync()` | Method | JDK method that dispatches an HTTP request asynchronously, returning a CompletableFuture that completes when the response arrives. |
| `whenComplete()` | Method | CompletableFuture method that registers a callback to run when the async operation completes (either successfully or with an exception). |
| `join()` | Method | CompletableFuture method that blocks the calling thread until the async operation completes, then returns the result. |
| `BodyHandlers.ofString()` | Class/Method | JDK body handler that reads the full HTTP response body into a String. |
| `异步调用 GET` | Comment | Chinese comment meaning "Asynchronous call GET" — describes the method's purpose as demonstrating async HTTP GET. |
| `同步调用 GET` | Comment | Chinese comment (in `syncGet`) meaning "Synchronous call GET" — the synchronous counterpart to this async method. |
| `异步调用 POST` | Comment | Chinese comment (in `asyncPost`) meaning "Asynchronous call POST" — the async POST counterpart method. |
