Overriding ServiceLoader detection

691 views
Skip to first unread message

Stephane Epardaud

unread,
Sep 1, 2021, 9:06:11 AM9/1/21
to Quarkus Development mailing list
I've had cases in the past where I wanted to override which services are loaded by ServiceLoader, or disable them entirely, and https://docs.oracle.com/javase/9/docs/api/java/util/ServiceLoader.html doesn't provide any way to override the detection.

Do you think there'd be any value in Quarkus standardising on a configuration to be able to override them?

Something like:

# disable all such services
quarkus.services.org.eclipse.microprofile.context.spi.ThreadContextProvider=

# limit services
quarkus.services.org.eclipse.microprofile.context.spi.ThreadContextProvider=io.smallrye.context.jta.context.propagation.JtaContextProvider

WDYT?
--
Stéphane Épardaud

Guillaume Smet

unread,
Sep 1, 2021, 9:12:18 AM9/1/21
to stephane...@gmail.com, Quarkus Development mailing list
I think we need something like that. I have been wanting that a few times. 

In my case, I wanted to exclude a specific service from the list so it would be good to have that option without having to provide all the others. 

Le 1 sept. 2021 à 15:06, Stephane Epardaud <stephane...@gmail.com> a écrit :


--
You received this message because you are subscribed to the Google Groups "Quarkus Development mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to quarkus-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/quarkus-dev/CAKU9E9tj2Q9Pw4RmmmgXQb0%3DAWEN3KVOXQ2uhskhPq2L1eneyA%40mail.gmail.com.

Georgios Andrianakis

unread,
Sep 1, 2021, 9:16:15 AM9/1/21
to Guillaume Smet, Stephane Epardaud, Quarkus Development mailing list
I think it's useful as well.
FWIW, Spring has a somewhat similar concept of the spring.factories file which is basically Spring's way of not using the Service Loader.
What you describe sounds even better to me

Stephane Epardaud

unread,
Sep 1, 2021, 9:17:26 AM9/1/21
to Guillaume Smet, Quarkus Development mailing list
# remove a service
quarkus.services.org.eclipse.microprofile.context.spi.ThreadContextProvider=-io.smallrye.context.jta.context.propagation.JtaContextProvider

But next thing you'll ask for:

# limit services
quarkus.services.org.eclipse.microprofile.context.spi.ThreadContextProvider=io.smallrye.context.jta.context.*,-io.smallrye.context.cdi.*

The problem is that we'd have to implement our own ServiceLoader equivalent. Or we push this as a JEP…

--
Stéphane Épardaud

George Gastaldi

unread,
Sep 1, 2021, 9:19:55 AM9/1/21
to Stephane Epardaud, Guillaume Smet, Quarkus Development mailing list
How about modeling this as Build item?

David Lloyd

unread,
Sep 1, 2021, 9:22:32 AM9/1/21
to Stephane Epardaud, Guillaume Smet, Quarkus Development mailing list
It's just a question of controlling the resources that are presented by the class loader for the purposes of service lookup.  It's very possible to tightly control what services are available if you have control of the class loader.

Stephane Epardaud

unread,
Sep 1, 2021, 9:29:44 AM9/1/21
to David Lloyd, Guillaume Smet, Quarkus Development mailing list
Huh, I was thinking of providing our own QuarkusServiceLoader copy, but doing it via the CL would make it work for all users indeed. This is provided the MP Config is already set-up everywhere we use ServiceLoader.
--
Stéphane Épardaud

Sanne Grinovero

unread,
Sep 1, 2021, 11:05:34 AM9/1/21
to Stef Epardaud, David Lloyd, Guillaume Smet, Quarkus Development mailing list

It would be great to enforce all ServiceLoader lookups to require opt-in by the extension, like we already do for native images.

This way not only you get such control and the ability to optimise things further, but we also make sure that applications tested in JVM will have an easier transition to native.



Stuart Douglas

unread,
Sep 1, 2021, 9:47:22 PM9/1/21
to Lloyd, David, Stephane Epardaud, Guillaume Smet, Quarkus Development mailing list
We can already do this to some extent. To do it via config you can use https://quarkus.io/guides/class-loading-reference#hidingremoving-classes-and-resources-from-dependencies

If you removed the META-INF services file then those services would no longer be loaded.

You can also accomplish this via build items using io.quarkus.deployment.builditem.RemovedResourceBuildItem

Stuart







David Lloyd

unread,
Sep 2, 2021, 11:22:50 AM9/2/21
to Stuart Douglas, Stephane Epardaud, Guillaume Smet, Quarkus Development mailing list
I guess most services are probably going to be one per file, so filtering by file will probably work in many cases, but maybe it would be nicer from a usability perspective to have a service-oriented way of describing this like what Steph is suggesting.  Maybe after any resource exclusions are processed, the service files could be cataloged and removed and replaced with something else (new condensed and filtered service files or a some generated service file producer or something).
--
- DML • he/him
Reply all
Reply to author
Forward
0 new messages