Skip to content

Commit

Permalink
User parallel Service loader
Browse files Browse the repository at this point in the history
  • Loading branch information
dstepanov committed Jul 8, 2022
1 parent 1438148 commit 705c6cf
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,12 @@ protected void registerDefaultConverters() {

Collection<TypeConverterRegistrar> registrars = new ArrayList<>();
ForkJoinPool forkJoinPool = new ForkJoinPool(); // Create a custom ForkJoinPool to prevent deadlock
SoftServiceLoader.load(TypeConverterRegistrar.class).collectAll(registrars, null, forkJoinPool);
SoftServiceLoader.load(TypeConverterRegistrar.class)
.withForkJoinPool(forkJoinPool)
.collectAll(registrars);
for (TypeConverterRegistrar registrar : registrars) {
registrar.register(this);
}
forkJoinPool.shutdown();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,13 @@ private void findMicronautMetaServiceConfigs(BiConsumer<URI, String> consumer) t
@SuppressWarnings("java:S1948")
final class DefaultServiceCollector extends RecursiveActionValuesCollector<S> implements SoftServiceLoader.ServiceCollector<S> {

private final ForkJoinPool forkJoinPool;
private final List<RecursiveActionValuesCollector<S>> tasks = new ArrayList<>();

DefaultServiceCollector(ForkJoinPool forkJoinPool) {
this.forkJoinPool = forkJoinPool;
}

@Override
protected void compute() {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ForkJoinPool;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
Expand Down Expand Up @@ -57,6 +58,7 @@ public final class SoftServiceLoader<S> implements Iterable<ServiceDefinition<S>
private Collection<ServiceDefinition<S>> servicesForIterator;
private final Predicate<String> condition;
private boolean allowFork = true;
private ForkJoinPool forkJoinPool = ForkJoinPool.commonPool();

private SoftServiceLoader(Class<S> serviceType, ClassLoader classLoader) {
this(serviceType, classLoader, (String name) -> true);
Expand Down Expand Up @@ -112,6 +114,11 @@ public SoftServiceLoader<S> disableFork() {
return this;
}

public SoftServiceLoader<S> withForkJoinPool(ForkJoinPool forkJoinPool) {
this.forkJoinPool = forkJoinPool;
return this;
}

/**
* @return Return the first such instance
*/
Expand Down Expand Up @@ -194,7 +201,7 @@ private void collectDynamicServices(
throw new ServiceLoadingException(e);
}
return null;
});
}, forkJoinPool);
collector.collect(values, allowFork);
}

Expand Down Expand Up @@ -254,7 +261,7 @@ public Iterator<ServiceDefinition<S>> iterator() {
} catch (NoClassDefFoundError | ClassNotFoundException e) {
return createService(name, null);
}
}).collect(serviceDefinitions, false);
}, forkJoinPool).collect(serviceDefinitions, false);
this.servicesForIterator = serviceDefinitions;
}
}
Expand Down Expand Up @@ -284,11 +291,19 @@ private ServiceDefinition<S> createService(String name, Class<S> loadedClass) {
return new DefaultServiceDefinition<>(name, loadedClass);
}

public static <S> ServiceCollector<S> newCollector(String serviceName,
Predicate<String> lineCondition,
ClassLoader classLoader,
Function<String, S> transformer,
ForkJoinPool forkJoinPool) {
return new ServiceScanner<>(classLoader, serviceName, lineCondition, transformer).new DefaultServiceCollector(forkJoinPool);
}

public static <S> ServiceCollector<S> newCollector(String serviceName,
Predicate<String> lineCondition,
ClassLoader classLoader,
Function<String, S> transformer) {
return new ServiceScanner<>(classLoader, serviceName, lineCondition, transformer).new DefaultServiceCollector();
return new ServiceScanner<>(classLoader, serviceName, lineCondition, transformer).new DefaultServiceCollector(ForkJoinPool.commonPool());
}

/**
Expand Down

0 comments on commit 705c6cf

Please sign in to comment.