Must-share information (formatted with Markdown):
- SonarQube Server Enterprise Edition 9.9.9 - zip
- sonar-scanner for maven 5.1.0.4751 (also with 4.0.0.4121)
- jdk21 executing sonar-scanner.
- Project on Java 11
Hi there, I have a problem with a project using Java 11 and ExecutorService. Sonar is rising an issue
Here is the obfuscated code (in order to preserve our customer privacy):
@Override
public void write(List<? extends SomethingDto> items) {
[They do things]
ExecutorService executorService = null;
try {
executorService = Executors.newFixedThreadPool(Constants.BATCH_SIZE);
[They do things]
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();
} catch (Exception ex) {
[They log]
throw ex;
} finally {
[They log]
if (executorService != null) {
executorService.shutdown();
}
}
}
Sonar is encouraging us to close ExecutorService or using try-with-resources. In order to use try-with-resources, object must implement AutoCloseable. ExecutorService implements it on Java 19:
Since we are using Java 11, we must close it manually in a finally. We are setting Java version in the pom properties:
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
I think ExecutorService in this case is correctly closed after usage. Is there something I am missing?
Why it keeps us raising this issue?