Skip to content

Commit 326cde7

Browse files
authored
Merge pull request #95 from rgdoliveira/sync_main
Sync main branch with Apache main branch
2 parents 7007715 + b872387 commit 326cde7

File tree

4 files changed

+99
-4
lines changed

4 files changed

+99
-4
lines changed

NOTICE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Apache KIE
2-
Copyright 2023-2024 The Apache Software Foundation
2+
Copyright 2023-2025 The Apache Software Foundation
33

44
This product includes software developed at
55
The Apache Software Foundation (http://www.apache.org/).

drools-compiler/src/main/java/org/drools/compiler/builder/impl/KnowledgeBuilderImpl.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.Map;
3333
import java.util.Optional;
3434
import java.util.concurrent.ForkJoinPool;
35+
import java.util.concurrent.ForkJoinWorkerThread;
3536
import java.util.function.Supplier;
3637

3738
import org.drools.base.RuleBase;
@@ -500,10 +501,11 @@ protected PackageRegistryManager getPackageRegistryManager() {
500501
}
501502

502503
public static class ForkJoinPoolHolder {
503-
public static final ForkJoinPool COMPILER_POOL = new ForkJoinPool(); // avoid common pool
504+
public static final ForkJoinPool COMPILER_POOL = new ForkJoinPool(Math.min(32767, Runtime.getRuntime().availableProcessors()), pool -> new ForkJoinWorkerThread(pool) {{
505+
setContextClassLoader(Thread.currentThread().getContextClassLoader());
506+
}}, null, false); // avoid common pool
504507
}
505508

506-
507509
public boolean filterAccepts(ResourceChange.Type type, String namespace, String name) {
508510
return assetFilter == null || !AssetFilter.Action.DO_NOTHING.equals(assetFilter.accept(type, namespace, name));
509511
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.drools.compiler.builder.impl;
20+
21+
import org.junit.jupiter.api.AfterEach;
22+
import org.junit.jupiter.api.BeforeEach;
23+
import org.junit.jupiter.api.Test;
24+
25+
import java.util.ArrayList;
26+
import java.util.List;
27+
import java.util.concurrent.RecursiveAction;
28+
import java.util.concurrent.TimeUnit;
29+
30+
import static org.junit.jupiter.api.Assertions.assertSame;
31+
32+
public class KnowledgeBuilderImplCompilerFJPoolTest {
33+
34+
private ClassLoader originalClassLoader;
35+
private ClassLoader customClassLoader;
36+
37+
@BeforeEach
38+
void setUp() {
39+
// Save the original classloader
40+
originalClassLoader = Thread.currentThread().getContextClassLoader();
41+
42+
// Create a custom classloader for testing
43+
customClassLoader = new ClassLoader(ClassLoader.getSystemClassLoader()) {};
44+
Thread.currentThread().setContextClassLoader(customClassLoader);
45+
}
46+
47+
@AfterEach
48+
void tearDown() {
49+
// Restore original classloader
50+
Thread.currentThread().setContextClassLoader(originalClassLoader);
51+
}
52+
53+
private static class ClassLoaderCheckTask extends RecursiveAction {
54+
private final List<ClassLoader> results;
55+
private final int index;
56+
57+
public ClassLoaderCheckTask(List<ClassLoader> results, int index) {
58+
this.results = results;
59+
this.index = index;
60+
}
61+
62+
@Override
63+
protected void compute() {
64+
results.set(index, Thread.currentThread().getContextClassLoader());
65+
}
66+
}
67+
68+
@Test
69+
void testCompilerPoolClassLoader() throws Exception {
70+
int numTasks = 5;
71+
List<ClassLoader> results = new ArrayList<>(numTasks);
72+
for (int i = 0; i < numTasks; i++) {
73+
results.add(null);
74+
}
75+
76+
// Submit tasks to the COMPILER_POOL
77+
for (int i = 0; i < numTasks; i++) {
78+
KnowledgeBuilderImpl.ForkJoinPoolHolder.COMPILER_POOL.submit(new ClassLoaderCheckTask(results, i));
79+
}
80+
81+
// Wait for completion
82+
KnowledgeBuilderImpl.ForkJoinPoolHolder.COMPILER_POOL.awaitQuiescence(1, TimeUnit.SECONDS);
83+
84+
// Verify all worker threads have the expected classloader
85+
for (ClassLoader workerClassLoader : results) {
86+
assertSame(
87+
customClassLoader,
88+
workerClassLoader,
89+
"Worker thread should have inherited the custom classloader"
90+
);
91+
}
92+
}
93+
}

drools-distribution/notice-for-distribution

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Apache KIE
2-
Copyright 2023-2024 The Apache Software Foundation
2+
Copyright 2023-2025 The Apache Software Foundation
33

44
This product includes software developed at
55
The Apache Software Foundation (http://www.apache.org/).

0 commit comments

Comments
 (0)