Skip to content

Commit 749676c

Browse files
gnodetclaude
andcommitted
CAMEL-23271: Fix SonarCloud blocker reliability issues (resource leaks)
- JcrConsumer: store ScheduledExecutorService in a field and shut it down in doStop() - ModelWriterGeneratorMojo: wrap DynamicClassLoader in try-with-resources - Set sonar.java.source=17 to eliminate 23 HttpClient false positives - Add @SuppressWarnings for ExecutorService/CamelContext lifecycle false positives Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent ffbce6a commit 749676c

File tree

7 files changed

+21
-10
lines changed

7 files changed

+21
-10
lines changed

components/camel-jcr/src/main/java/org/apache/camel/component/jcr/JcrConsumer.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.util.Arrays;
2020
import java.util.concurrent.ScheduledExecutorService;
21-
import java.util.concurrent.ScheduledFuture;
2221
import java.util.concurrent.TimeUnit;
2322

2423
import javax.jcr.RepositoryException;
@@ -40,7 +39,7 @@ public class JcrConsumer extends DefaultConsumer {
4039

4140
private Session session;
4241
private EventListener eventListener;
43-
private ScheduledFuture<?> sessionListenerCheckerScheduledFuture;
42+
private ScheduledExecutorService sessionListenerCheckerExecutor;
4443

4544
public JcrConsumer(JcrEndpoint endpoint, Processor processor) {
4645
super(endpoint, processor);
@@ -157,20 +156,23 @@ private void unregisterListenerAndLogoutSession() throws RepositoryException {
157156
}
158157

159158
private void cancelSessionListenerChecker() {
160-
if (sessionListenerCheckerScheduledFuture != null) {
161-
sessionListenerCheckerScheduledFuture.cancel(true);
159+
if (sessionListenerCheckerExecutor != null) {
160+
getJcrEndpoint().getCamelContext().getExecutorServiceManager()
161+
.shutdownNow(sessionListenerCheckerExecutor);
162+
sessionListenerCheckerExecutor = null;
162163
}
163164
}
164165

165166
private void scheduleSessionListenerChecker() {
166167
String name = "JcrConsumerSessionChecker[" + getJcrEndpoint().getEndpointConfiguredDestinationName() + "]";
167-
ScheduledExecutorService executor = getJcrEndpoint().getCamelContext().getExecutorServiceManager()
168+
sessionListenerCheckerExecutor = getJcrEndpoint().getCamelContext().getExecutorServiceManager()
168169
.newSingleThreadScheduledExecutor(this, name);
169170
JcrConsumerSessionListenerChecker sessionListenerChecker = new JcrConsumerSessionListenerChecker();
170171
long sessionLiveCheckIntervalOnStart = JcrConsumer.this.getJcrEndpoint().getSessionLiveCheckIntervalOnStart();
171172
long sessionLiveCheckInterval = JcrConsumer.this.getJcrEndpoint().getSessionLiveCheckInterval();
172-
sessionListenerCheckerScheduledFuture = executor.scheduleWithFixedDelay(sessionListenerChecker,
173-
sessionLiveCheckIntervalOnStart, sessionLiveCheckInterval, TimeUnit.MILLISECONDS);
173+
sessionListenerCheckerExecutor.scheduleWithFixedDelay(
174+
sessionListenerChecker, sessionLiveCheckIntervalOnStart, sessionLiveCheckInterval,
175+
TimeUnit.MILLISECONDS);
174176
}
175177

176178
private class JcrConsumerSessionListenerChecker implements Runnable {

components/camel-salesforce/camel-salesforce-component/src/main/java/org/apache/camel/component/salesforce/SalesforceComponent.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,7 @@ public RawClient createRawClientFor(SalesforceEndpoint endpoint) throws Salesfor
958958
return new DefaultRawClient(httpClient, "", session, loginConfig);
959959
}
960960

961+
@SuppressWarnings("java:S2095") // ExecutorService lifecycle is managed by SalesforceHttpClient
961962
static SalesforceHttpClient createHttpClient(
962963
Object source, final SslContextFactory.Client sslContextFactory, final CamelContext context, int workerPoolSize,
963964
int workerPoolMaxSize) {

core/camel-core-reifier/src/main/java/org/apache/camel/reifier/AggregateReifier.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public Processor createProcessor() throws Exception {
5050
return createAggregator();
5151
}
5252

53+
@SuppressWarnings("java:S2095") // ExecutorService lifecycle is managed by AggregateProcessor via shutdownThreadPool flag
5354
protected AggregateProcessor createAggregator() throws Exception {
5455
Processor childProcessor = this.createChildProcessor(true);
5556

core/camel-core-reifier/src/main/java/org/apache/camel/reifier/ThreadsReifier.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public ThreadsReifier(Route route, ProcessorDefinition<?> definition) {
3434
super(route, (ThreadsDefinition) definition);
3535
}
3636

37+
@SuppressWarnings("java:S2095") // ExecutorService lifecycle is managed by ThreadsProcessor via shutdownThreadPool flag
3738
@Override
3839
public Processor createProcessor() throws Exception {
3940
// the threads name

dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/main/java/org/apache/camel/dsl/yaml/validator/CamelYamlParser.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
*/
5050
public class CamelYamlParser {
5151

52+
@SuppressWarnings("java:S2095") // Registry is owned by CamelContext; CamelContext is stopped in finally block
5253
public List<ValidationMessage> parse(File file) throws Exception {
5354
CamelContext camelContext = null;
5455
try {

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122

123123
<!-- NOTE: this is required to correctly map each module coverage in Sonarqube. The ${maven.multiModuleProjectDirectory} may require some change
124124
when upgrading to Maven 4.x, according to any of the new variables that will replace this one. -->
125+
<sonar.java.source>17</sonar.java.source>
125126
<sonar.coverage.jacoco.xmlReportPaths>
126127
${maven.multiModuleProjectDirectory}/coverage/target/site/jacoco-aggregate/jacoco.xml
127128
</sonar.coverage.jacoco.xmlReportPaths>

tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/ModelWriterGeneratorMojo.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,17 @@ String getModelPackage() {
113113
abstract String getWriterPackage();
114114

115115
protected String generateWriter() throws MojoExecutionException {
116-
ClassLoader classLoader;
117-
try {
118-
classLoader = DynamicClassLoader.createDynamicClassLoader(project.getCompileClasspathElements());
116+
try (DynamicClassLoader classLoader
117+
= DynamicClassLoader.createDynamicClassLoader(project.getCompileClasspathElements())) {
118+
return doGenerateWriter(classLoader);
119119
} catch (DependencyResolutionRequiredException e) {
120120
throw new MojoExecutionException("DependencyResolutionRequiredException: " + e.getMessage(), e);
121+
} catch (IOException e) {
122+
throw new MojoExecutionException("IOException: " + e.getMessage(), e);
121123
}
124+
}
122125

126+
private String doGenerateWriter(ClassLoader classLoader) throws MojoExecutionException {
123127
List<Path> jsonFiles;
124128
try (Stream<Path> stream = PackageHelper.findJsonFiles(modelDir.toPath())) {
125129
jsonFiles = stream.toList();

0 commit comments

Comments
 (0)