Skip to content

Commit 150e69e

Browse files
authored
Issue #58 Fix HttpClient resource management in ApiTracker.java (#79)
* Issue #58 Fix HttpClient resource management in ApiTracker.java - Replaced static shared HttpClient instance with factory method - Updated fetchFromUrl() to use try-with-resources for proper resource management - Updated fetchUpstreamSources() to use try-with-resources for proper resource management - Ensures HttpClient instances are properly closed after use - Aligns with Java 21+ AutoCloseable best practices The changes ensure that HttpClient resources are properly managed using try-with-resources, preventing potential resource leaks and following Java best practices for AutoCloseable resources. * Add try-with-resources guideline to AGENTS.md - Added bullet point under 'Leverage Java 21+ features' section - Specifies that try-with-resources must be used for all AutoCloseable resources - Includes examples like HttpClient and streams - Ensures proper resource management following Java best practices * Fix AGENTS.md references and add try-with-resources guideline - Updated reference to deleted CODING_STYLE.md file - Changed to indicate this section contains the coding standards directly - Added try-with-resources bullet point under Java 21+ features - Ensures documentation is accurate and complete * Update AGENTS.md commit guidelines to forbid advertising and co-author comments - Added bullet point prohibiting advertising/promotional content in commit messages - Added bullet point prohibiting 'Co-Authored-By' comments in commit messages - Both guidelines added to Commit Requirements section - Ensures clean, professional commit messages without external advertising - Maintains proper git attribution through standard author fields
1 parent 8b13911 commit 150e69e

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

AGENTS.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ mvn exec:java -pl json-compatibility-suite -Dexec.args="--json"
220220
- Optionally note unexpected technical details when they are not obvious from the issue itself.
221221
- Do not report progress or success in the commit message; nothing is final until merged.
222222
- Every tidy-up commit requires an accompanying issue. If labels are unavailable, title the issue `Tidy Up: ...` and keep the description minimal.
223+
- **Do not include advertising or promotional content** such as `🤖 Generated with [XXX YYY](https://XXX/YYY)` in commit messages.
224+
- **Do not add 'Co-Authored-By' comments** to commit messages; keep attribution within the normal git author fields.
223225

224226
### Pull Requests
225227
- Describe what was done, not the rationale or implementation details.
@@ -422,7 +424,7 @@ PY
422424

423425
# Java DOP Coding Standards ####################
424426

425-
This file is a Gen AI summary of CODING_STYLE.md to use less tokens of context window. Read the original file for full details.
427+
This section contains the Java DOP (Data-Oriented Programming) coding standards and guidelines.
426428

427429
IMPORTANT: We do TDD so all code must include targeted unit tests.
428430
IMPORTANT: Never disable tests written for logic that we are yet to write we do Red-Green-Refactor coding.
@@ -455,6 +457,7 @@ IMPORTANT: Never disable tests written for logic that we are yet to write we do
455457
* Pattern matching for structural decomposition
456458
* Sealed classes for exhaustive switches
457459
* Virtual threads for concurrent processing
460+
* **Use try-with-resources for all AutoCloseable resources** (HttpClient, streams, etc.)
458461

459462
## Package Structure
460463

json-java21-api-tracker/src/main/java/io/github/simbo1905/tracker/ApiTracker.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,21 +64,23 @@ enum Nothing implements ApiTracker {}
6464
// GitHub base URL for upstream sources
6565
String GITHUB_BASE_URL = "https://raw.githubusercontent.com/openjdk/jdk-sandbox/refs/heads/json/src/java.base/share/classes/";
6666

67-
// Shared HttpClient instance for efficient resource management
68-
HttpClient SHARED_HTTP_CLIENT = HttpClient.newBuilder()
69-
.connectTimeout(Duration.ofSeconds(10))
70-
.build();
67+
// HttpClient factory method for proper resource management
68+
static HttpClient createHttpClient() {
69+
return HttpClient.newBuilder()
70+
.connectTimeout(Duration.ofSeconds(10))
71+
.build();
72+
}
7173

7274
/// Fetches content from a URL
7375
static String fetchFromUrl(String url) {
74-
try {
76+
try (final var httpClient = createHttpClient()) {
7577
final var request = HttpRequest.newBuilder()
7678
.uri(URI.create(url))
7779
.timeout(Duration.ofSeconds(30))
7880
.GET()
7981
.build();
8082

81-
final var response = SHARED_HTTP_CLIENT.send(request, HttpResponse.BodyHandlers.ofString());
83+
final var response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
8284

8385
if (response.statusCode() == 200) {
8486
return response.body();
@@ -227,14 +229,14 @@ static Map<String, String> fetchUpstreamSources(Set<Class<?>> localClasses) {
227229

228230
LOGGER.info("Fetching upstream source: " + url);
229231

230-
try {
232+
try (final var httpClient = createHttpClient()) {
231233
final var request = HttpRequest.newBuilder()
232234
.uri(URI.create(url))
233235
.timeout(Duration.ofSeconds(30))
234236
.GET()
235237
.build();
236238

237-
final var response = SHARED_HTTP_CLIENT.send(request, HttpResponse.BodyHandlers.ofString());
239+
final var response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
238240

239241
if (response.statusCode() == 200) {
240242
final var body = response.body();

0 commit comments

Comments
 (0)