/**
* Connects to the specified JVM process and requests system information.
+ *
* @return the system info, or absent if the process is not up or if its HTTP URL
* is not registered into IPC.
*/
.post(RequestBody.create(null, new byte[0]))
.url(url + "?level=" + newLogLevel.name())
.build();
- okhttp3.Response response = new OkHttpClient().newCall(request).execute();
- if (response.code() != 200) {
- throw new IOException(
- String.format(
- "Failed to change log level in Compute Engine. Code was '%s' and response was '%s' for url '%s'",
- response.code(),
- response.body().string(),
- url));
+ try (okhttp3.Response response = new OkHttpClient().newCall(request).execute()) {
+ if (response.code() != 200) {
+ throw new IOException(
+ String.format(
+ "Failed to change log level in Compute Engine. Code was '%s' and response was '%s' for url '%s'",
+ response.code(),
+ response.body().string(),
+ url));
+ }
+ return null;
}
- return null;
}
}
.post(RequestBody.create(null, new byte[0]))
.url(url)
.build();
- okhttp3.Response response = new OkHttpClient().newCall(request).execute();
- if (response.code() != 200) {
- throw new IOException(
- String.format(
- "Failed to trigger refresh of CE Worker count. Code was '%s' and response was '%s' for url '%s'",
- response.code(),
- response.body().string(),
- url));
+ try (okhttp3.Response response = new OkHttpClient().newCall(request).execute()) {
+ if (response.code() != 200) {
+ throw new IOException(
+ String.format(
+ "Failed to trigger refresh of CE Worker count. Code was '%s' and response was '%s' for url '%s'",
+ response.code(),
+ response.body().string(),
+ url));
+ }
+ return null;
}
- return null;
}
}
package org.sonar.server.telemetry;
import java.io.IOException;
+import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
+import okhttp3.Response;
import org.sonar.api.config.Configuration;
import org.sonar.api.server.ServerSide;
import org.sonar.api.utils.log.Logger;
void upload(String json) throws IOException {
Request request = buildHttpRequest(json);
- okHttpClient.newCall(request).execute();
+ execute(okHttpClient.newCall(request));
}
void optOut(String json) {
request.delete(body);
try {
- okHttpClient.newCall(request.build()).execute();
+ execute(okHttpClient.newCall(request.build()));
} catch (IOException e) {
LOG.debug("Error when sending opt-out usage statistics: {}", e.getMessage());
}
return config.get(PROP_URL).orElseThrow(() -> new IllegalStateException(String.format("Setting '%s' must be provided.", PROP_URL)));
}
+ private static void execute(Call call) throws IOException {
+ try (Response ignored = call.execute()) {
+ // auto close connection to avoid leaked connection
+ }
+ }
+
}