Oracle OCI MonitoringClient是否需每次查询后关闭?能否复用?
Let's break down your questions clearly, from a practical developer perspective:
1. Do I need to close com.oracle.bmc.monitoring.MonitoringClient after every metric query?
Nope, you don't have to shut it down after each individual request. The MonitoringClient implements AutoCloseable because it manages underlying resources like HTTP connection pools and thread pools under the hood. Closing it is important—but only when you're completely done using it (like when your application is shutting down). Closing it after every query would just waste resources recreating those pools over and over, which adds unnecessary overhead.
2. The SDK examples create and close a client for every summarizeMetricsData call—Is this necessary? Can I reuse one client for all requests during my app's runtime?
Those examples are built to be self-contained, "copy-paste and run" demos. They create and close the client each time to show a full, clean standalone flow that doesn't leave resources hanging for a simple tutorial. But this isn't the optimal approach for production or long-running applications.
You absolutely can and should reuse a single MonitoringClient instance across all your requests. Reusing the client lets you leverage the SDK's built-in connection pooling, which cuts down on the overhead of spinning up new connections, authentication contexts, and thread pools every time you query metrics. This will make your app faster and more efficient, especially if you're making frequent metric calls.
Just remember to call client.close() once when your application is shutting down (like in a shutdown hook for a standalone app, or the destroy phase of a web application) to properly release all those underlying resources.
内容的提问来源于stack exchange,提问作者aleksandra

