Giriş
Şu satırı dahil ederiz.
Şu satırı dahil ederiz.
import java.net.http.HttpClient;
1. HttpRequest nesnesi yaratılır. Bu nesne GET, POST gibi bir isteği temsil ederi
2. HttpClient nesnesi yaratılır. Bu nesnenin send() veya sendAsync() metodu çağrılır. 3. Çağrı sonucunda HttpResponse nesnesi elde edilir.
4. HttpResponse sınıfı şöyle kullanılır
Açıklaması şöyle
The absence of a configurable connection pool in JDK HTTP client at first surprised me. How to control concurrency?Spring Boot falls back to using this impl. underneath its modern HTTP Service Interface Clients when no higher-priority client library is available.Yet neither layer exposes the familiar connection-pool controls:- Max total connections- Max connections per destination- Pending-acquire timeoutThe official docs offer little explanation for this design.That’s why I traced the JDK code with a Codex agent and found an interesting answer.Let’s have a closer look.The java.net.HttpClient manages and reuses protocol-specific connections internally. Yet it does not expose one universal pool model, and the reason becomes clearer across HTTP versions:- HTTP/1.1 usually needs more connections for more concurrency- HTTP/2 multiplexes many requests over one connection- HTTP/3 supported in JDK 26+, does the same over QUICWith multiplexed protocols, connection count no longer maps cleanly to concurrency, making “maxConnections” a less useful abstraction rooted in the HTTP/1.1 model.The focus therefore shiftsfrom limitinconnections to limiting admitted workBecause the HTTP client itself cannot determine a dependency’s safe operating limits, critical outbound paths need application-level admission control:Request -> Concurreny limit (Semaphore) -> HttpClient -> Remote APIExcess work waits for a permit, possibly up to a timeout, or fails fast before reaching the dependency. Once admitted, the request timeout limits how long it can occupy a permit.For APIs with published quotas, an optional rate limiter can help avoid unnecessary calls and noisy 429s.So the division of responsibility is:- The HTTP client manages connection reuse- The application code enforces admission limits- The engineer determines those limits from the dependency’s capacity, latency targets, and quotas.Virtual threads (VTs) make explicit admission control even more important.Without a bounded thread pool, VTs can allow far more requests to reach a downstream dependency at once, potentially overwhelming it.That said, Spring Boot supports several HTTP client impl. with different pool controls.These should be tuned per client, while downstream load should be bound explicitly at the application level.
Örnek - String HttpResponse
Şöyle yaparız.
Şöyle yaparız.
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = ...;
HttpResponse<String> responseOfString = client.send(request,
HttpResponse.BodyHandlers.ofString());
System.out.println("Status code: " + responseOfString.statusCode());
System.out.println("Body: " + responseOfString.body());Örnek - Stream HttpResponse
Şöyle yaparız
HttpResponse<InputStream> getResponse(HttpRequest request)
throws IOException, InterruptedException {
return HttpClient
.newHttpClient()
.send(request, HttpResponse.BodyHandlers.ofInputStream());
}
HttpResponse<InputStream> response = getResponse(request);
System.out.println("status code: " + response.statusCode());
InputStream body = response.body();newBuilder metodu
HttpClient.Builder Sınıfı yazısına taşıdım
newHttpClient metodu
Şöyle yaparız
HttpClient client = HttpClient.newHttpClient();
send metodu
send metodu yazısına taşıdım
sendAsync metodu
sendAsync metodu yazısına taşıdım
Java 21
Açıklaması şöyle
The java.net.http.HttpClient now implements AutoClosable, making it usable in a try-with-resources block.There are also 5 new related methods:
İmzası şöyle
// Initiates an orderly shutdown by completing previously submitted
// request but not accepting new ones.
void close()
// Initiates a graceful shutdown and returns immediately
void shutdown()
// Initiates an immediate shutdown and tries to interrupt any active
// operation. Returns immediately, too.
void shutdownNow()
// Waits for the client to terminate for the given duration.
// Returns true if the client was terminated in time.
boolean awaitTermination(Duration duration)
// Checks if a client is terminated.
boolean isTerminated()Açıklaması şöyle
Instances obtained via HttpClient.newHttpClient() or HttpClient.newBuilder() provide best-effort implementations of these methods.
Hiç yorum yok:
Yorum Gönder