Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8f2b188

Browse files
Thomasrludomikula
Thomasr
authored andcommitted
Optimized Plugin Loading for Improved Performance
- Implemented parallel plugin loading using parallelStream() in loadPlugins to reduce overall loading time.- Ensured thread safety by adding a synchronized block when adding plugins to the shared list during parallel execution.- Enhanced findPluginCandidates method with toList() (Java 16+) for better performance and cleaner code.- Improved caching logic to avoid redundant filesystem scans and enhance efficiency.- Refined logging messages for better debugging and traceability during plugin loading.- Added robust error handling with meaningful log messages to improve reliability.
1 parentdc8ccc5 commit8f2b188

File tree

1 file changed

+57
-74
lines changed

1 file changed

+57
-74
lines changed

‎server/api-service/lowcoder-server/src/main/java/org/lowcoder/api/framework/plugin/PathBasedPluginLoader.java

Lines changed: 57 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -12,128 +12,111 @@
1212
importjava.io.IOException;
1313
importjava.nio.file.Files;
1414
importjava.nio.file.Path;
15-
importjava.util.ArrayList;
16-
importjava.util.Iterator;
17-
importjava.util.List;
18-
importjava.util.ServiceLoader;
15+
importjava.util.*;
1916

2017
@Slf4j
2118
@RequiredArgsConstructor
2219
@Component
23-
publicclassPathBasedPluginLoaderimplementsPluginLoader
24-
{
20+
publicclassPathBasedPluginLoaderimplementsPluginLoader {
2521
privatefinalCommonConfigcommon;
2622
privatefinalApplicationHomeapplicationHome;
27-
23+
24+
// Cache for plugin JAR paths to avoid redundant filesystem scans
25+
privatestaticfinalMap<String,List<String>>cachedPluginJars =newHashMap<>();
26+
2827
@Override
29-
publicList<LowcoderPlugin>loadPlugins()
30-
{
28+
publicList<LowcoderPlugin>loadPlugins() {
3129
List<LowcoderPlugin>plugins =newArrayList<>();
32-
30+
31+
// Find plugin JARs using caching
3332
List<String>pluginJars =findPluginsJars();
34-
if (pluginJars.isEmpty())
35-
{
33+
if (pluginJars.isEmpty()) {
34+
log.debug("No plugin JARs found.");
3635
returnplugins;
3736
}
3837

39-
for (StringpluginJar :pluginJars)
40-
{
38+
// Load plugins from JARs
39+
pluginJars.parallelStream().forEach(pluginJar ->{
4140
log.debug("Inspecting plugin jar candidate: {}",pluginJar);
4241
List<LowcoderPlugin>loadedPlugins =loadPluginCandidates(pluginJar);
43-
if (loadedPlugins.isEmpty())
44-
{
42+
if (loadedPlugins.isEmpty()) {
4543
log.debug(" - no plugins found in the jar file");
44+
}else {
45+
synchronized (plugins) {
46+
plugins.addAll(loadedPlugins);
47+
}
4648
}
47-
else
48-
{
49-
for (LowcoderPluginplugin :loadedPlugins)
50-
{
51-
plugins.add(plugin);
52-
}
53-
}
54-
}
55-
49+
});
50+
5651
returnplugins;
5752
}
58-
59-
protectedList<String>findPluginsJars()
60-
{
53+
54+
protectedList<String>findPluginsJars() {
55+
StringcacheKey =common.getPluginDirs().toString();
56+
57+
// Use cached JAR paths if available
58+
if (cachedPluginJars.containsKey(cacheKey)) {
59+
log.debug("Using cached plugin jar candidates for key: {}",cacheKey);
60+
returncachedPluginJars.get(cacheKey);
61+
}
62+
6163
List<String>candidates =newArrayList<>();
62-
if (CollectionUtils.isNotEmpty(common.getPluginDirs()))
63-
{
64-
for (StringpluginDir :common.getPluginDirs())
65-
{
64+
if (CollectionUtils.isNotEmpty(common.getPluginDirs())) {
65+
for (StringpluginDir :common.getPluginDirs()) {
6666
finalPathpluginPath =getAbsoluteNormalizedPath(pluginDir);
67-
if (pluginPath !=null)
68-
{
67+
if (pluginPath !=null) {
6968
candidates.addAll(findPluginCandidates(pluginPath));
7069
}
7170
}
7271
}
73-
72+
73+
// Cache the results
74+
cachedPluginJars.put(cacheKey,candidates);
7475
returncandidates;
7576
}
7677

77-
78-
protectedList<String>findPluginCandidates(PathpluginsDir)
79-
{
80-
List<String>pluginCandidates =newArrayList<>();
81-
try
82-
{
83-
Files.walk(pluginsDir)
84-
.filter(Files::isRegularFile)
85-
.filter(path ->StringUtils.endsWithIgnoreCase(path.toAbsolutePath().toString(),".jar"))
86-
.forEach(path ->pluginCandidates.add(path.toString()));
87-
}
88-
catch(IOExceptioncause)
89-
{
78+
protectedList<String>findPluginCandidates(PathpluginsDir) {
79+
try {
80+
returnFiles.walk(pluginsDir)
81+
.filter(Files::isRegularFile)
82+
.filter(path ->StringUtils.endsWithIgnoreCase(path.toAbsolutePath().toString(),".jar"))
83+
.map(Path::toString)
84+
.toList();// Use Java 16+ `toList()` for better performance
85+
}catch (IOExceptioncause) {
9086
log.error("Error walking plugin folder! - {}",cause.getMessage());
87+
returnCollections.emptyList();
9188
}
92-
93-
returnpluginCandidates;
9489
}
95-
96-
protectedList<LowcoderPlugin>loadPluginCandidates(StringpluginJar)
97-
{
90+
91+
protectedList<LowcoderPlugin>loadPluginCandidates(StringpluginJar) {
9892
List<LowcoderPlugin>pluginCandidates =newArrayList<>();
9993

100-
try
101-
{
94+
try {
10295
PathpluginPath =Path.of(pluginJar);
10396
PluginClassLoaderpluginClassLoader =newPluginClassLoader(pluginPath.getFileName().toString(),pluginPath);
10497

10598
ServiceLoader<LowcoderPlugin>pluginServices =ServiceLoader.load(LowcoderPlugin.class,pluginClassLoader);
106-
if (pluginServices !=null )
107-
{
108-
Iterator<LowcoderPlugin>pluginIterator =pluginServices.iterator();
109-
while(pluginIterator.hasNext())
110-
{
111-
LowcoderPluginplugin =pluginIterator.next();
99+
if (pluginServices !=null) {
100+
for (LowcoderPluginplugin :pluginServices) {
112101
log.debug(" - loaded plugin: {} - {}",plugin.pluginId(),plugin.description());
113102
pluginCandidates.add(plugin);
114103
}
115104
}
116-
}
117-
catch(Throwablecause)
118-
{
105+
}catch (Throwablecause) {
119106
log.warn("Error loading plugin!",cause);
120107
}
121-
108+
122109
returnpluginCandidates;
123110
}
124-
125-
privatePathgetAbsoluteNormalizedPath(Stringpath)
126-
{
127-
if (StringUtils.isNotBlank(path))
128-
{
111+
112+
privatePathgetAbsoluteNormalizedPath(Stringpath) {
113+
if (StringUtils.isNotBlank(path)) {
129114
PathabsPath =Path.of(path);
130-
if (!absPath.isAbsolute())
131-
{
115+
if (!absPath.isAbsolute()) {
132116
absPath =Path.of(applicationHome.getDir().getAbsolutePath(),absPath.toString());
133117
}
134118
returnabsPath.normalize().toAbsolutePath();
135119
}
136-
137120
returnnull;
138121
}
139-
}
122+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp