Skip to content

Commit

Permalink
Tweak documentation for how to use service files in extensions
Browse files Browse the repository at this point in the history
Fixes #2299
  • Loading branch information
FroMage authored and gsmet committed Nov 19, 2019
1 parent 890c7f4 commit e91f184
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion docs/src/main/asciidoc/writing-extensions.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2040,6 +2040,31 @@ public final class MyExtProcessor {
If you are using `META-INF/services` files you need to register the files as resources so that your native image can find them,
but you also need to register each listed class for reflection so they can be instantiated or inspected at run-time:

[source,java]
----
public final class MyExtProcessor {
@BuildStep
void registerNativeImageReources(BuildProducer<ServiceProviderBuildItem> services) {
String service = "META-INF/services/" + io.quarkus.SomeService.class.getName();
// find out all the implementation classes listed in the service files
Set<String> implementations =
ServiceUtil.classNamesNamedIn(Thread.currentThread().getContextClassLoader(),
service);
// register every listed implementation class so they can be instantiated
// in native-image at run-time
services.produce(
new ServiceProviderBuildItem(io.quarkus.SomeService.class.getName(),
implementations.toArray(new String[0])));
}
}
----

NOTE: This only registers the implementation classes for instantiation via reflection (you will not be able
to inspect its fields and methods). If you need to do that, you can do it this way:

[source,java]
----
public final class MyExtProcessor {
Expand All @@ -2063,7 +2088,7 @@ public final class MyExtProcessor {
}
----

This is the easiest way to get your services running natively, but it's less efficient than scanning the implementation
While this is the easiest way to get your services running natively, it's less efficient than scanning the implementation
classes at build time and generating code that registers them at static-init time instead of relying on reflection.

You can achieve that by adapting the previous build step to use a static-init recorder instead of registering
Expand Down

0 comments on commit e91f184

Please sign in to comment.