Skip to content

Fix HttpClient resource management in ApiTracker.java #58

@simbo1905

Description

@simbo1905

Issue Description

The HttpClient instance in ApiTracker.java is not properly managed using try-with-resources, which violates Java's AutoCloseable resource management best practices.

Location

  • File: json-java21-api-tracker/src/main/java/io/github/simbo1905/tracker/ApiTracker.java
  • Lines: Around lines where HttpClient.newBuilder() is used

Problem

The code creates an HttpClient instance:

final var httpClient = HttpClient.newBuilder()
    .connectTimeout(Duration.ofSeconds(10))
    .build();

But does not use it within a try-with-resources statement, which means:

  1. The HttpClient may not be properly closed
  2. Potential resource leaks if exceptions occur
  3. Violates the AutoCloseable contract

Expected Fix

Wrap the HttpClient usage in a try-with-resources statement to ensure proper resource cleanup:

try (final var httpClient = HttpClient.newBuilder()
        .connectTimeout(Duration.ofSeconds(10))
        .build()) {
    // HttpClient usage here
}

Impact

  • Severity: Medium (resource leak potential)
  • Type: Code quality / Resource management
  • Module: json-java21-api-tracker

Additional Context

This issue was identified by static analysis tools that detect AutoCloseable instances used without proper resource management. The fix aligns with Java best practices for managing AutoCloseable resources.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions