-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ServiceLocator can't find an implementation in common-pool workers #568
Comments
Interesting. Do you have a simple test case that would allow us to re-create this? Or even a minimal set of code to get us started? Also, have you seen this? https://stackoverflow.com/questions/49113207/completablefuture-forkjoinpool-set-class-loader (specifically the part about not using I wonder if that or something similar could help. cc @bdemers |
Thank you for the link! Unfortunately, I can't get rid of Btw, I made an example app: https://github.com/timsazon/jjwt-568 |
Hey @timsazon thanks for putting together a minimal example, that made things MUCH easier. For a short term workaround you could do something like this: private static Supplier<String> tokenSupplier() {
return () -> {
ClassLoader originalClassloader = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(Jwts.class.getClassLoader());
return Jwts.builder()
.setClaims(Map.of(Claims.SUBJECT, "subject"))
.compact();
} finally {
Thread.currentThread().setContextClassLoader(originalClassloader);
}
};
} Basically just setting the contextClassLoader to something with JJWTs classes. (or if you can configure the thread pool as mentioned in that stackoverflow post @lhazlewood We could also try to fall back, something like this in Services: public static <T> T loadFirst(Class<T> spi) {
Assert.notNull(spi, "Parameter 'spi' must not be null.");
ServiceLoader<T> serviceLoader = ServiceLoader.load(spi);
if (serviceLoader.iterator().hasNext()) {
return serviceLoader.iterator().next();
} else {
// failed to lookup implementation in current classloader
// try again with this classes classloader
try {
Thread.currentThread().setContextClassLoader(Jwts.class.getClassLoader());
serviceLoader = ServiceLoader.load(spi);
if (serviceLoader.iterator().hasNext()) {
return serviceLoader.iterator().next();
} else {
throw new UnavailableImplementationException(spi);
}
} finally {
Thread.currentThread().setContextClassLoader(originalClassloader);
}
}
} (typed into github, so it needs to be cleaned up a little, just wanted to get the idea out) |
@bdemers the old (non ServiceLocator approach) used the proper Classloading logic 🤦♂. jjwt/api/src/main/java/io/jsonwebtoken/lang/Classes.java Lines 72 to 96 in 111633f
I think we need to revert to the old approach - not a wholesale reversion - just the bare minimum to use the proper classloader. Perhaps we replace ServiceLoader.load(spi) with our own implementation that looks for a |
Hrm - maybe another approach is to use this method: And implement our own (static singleton) Classloader instance that just delegates to the ClassLoader cascade logic found in That static singleton would be created once and we'd change Thoughts? |
I also want to say thank you to @timsazon for the sample project! That's so helpful! |
I think parsing the I'm attempting to test, iterating over the three classloaders (similar to the |
…r, and the system classloader Fixes: #568
…r, and the system classloader Fixes: #568
@lhazlewood, I didn't use a classloader, but I did mimic the "accessor" model that (I just tested this out on @timsazon's sample project) |
…r, and the system classloader Fixes: #568
Hi, I've noticed that we can't read/create a token in a worker from
ForkJoinPool.commonPool()
in Spring's uber-jar application because ServiceLocator can't find an implementation for that (looks like there is a problem with context class loader related to the thread).Example (it will throw
UnavailableImplementationException
forSerializer.class
):Currently, I have a workaround to that by providing my own serializer (like this https://github.com/jwtk/jjwt#jackson-json-processor).
The text was updated successfully, but these errors were encountered: