Sometimes JerseyServlet fails to start because InjectionManagerFactory can't be found
steinarb opened this issue · comments
Sometimes JerseyServlet fails to start because InjectionManagerFactory can't be found.
Example from karaf.log:
15:34:33.571 WARN [features-3-thread-1] unavailable
java.lang.IllegalStateException: InjectionManagerFactory not found.
at org.glassfish.jersey.internal.inject.Injections.lambda$lookupInjectionManagerFactory$0(Injections.java:74)
at java.base/java.util.Optional.orElseThrow(Unknown Source)
at org.glassfish.jersey.internal.inject.Injections.lookupInjectionManagerFactory(Injections.java:74)
at org.glassfish.jersey.internal.inject.Injections.createInjectionManager(Injections.java:69)
at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:259)
at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:311)
at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:154)
at no.priv.bang.servlet.jersey.JerseyServlet.init(JerseyServlet.java:87)
at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:347)
at javax.servlet.GenericServlet.init(GenericServlet.java:244)
at org.eclipse.jetty.servlet.ServletHolder$WrapperServlet.init(ServletHolder.java:1289)
at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:602)
The problem is spurious: my most common workaround is to clear the karaf data directory and keep reinstalling until the problem goes away.
I googled and found this explanation: the jersey-inject-hk2 maven dependency is missing
The jersey-inject-hk2 OSGi bundle is available at run time and it is loaded by the jersey-karaf-feature.
My theory is that it has something to do with load order. I.e. that if the jersey-inject-hk2 bundle is loaded before any bundles containing JerseyServlet, then the problem never occurs.
I've tested this by installing the jersey-karaf-feature before installing features that pulls in JerseyServlet and this makes the issue go away. But since the issue is spurious I can't be sure that I have a fix/workaround.
I've noticed that the jersey-inject-hk2 OSGi bundle offers up a package that isn't used anywhere:
karaf@root()> bundle:list | grep hk2
140 x Active x 80 x 2.30.1 x jersey-inject-hk2
karaf@root()> bundle:capabilities 140
org.glassfish.jersey.inject.jersey-hk2 [140] provides:
------------------------------------------------------
osgi.wiring.bundle; org.glassfish.jersey.inject.jersey-hk2 2.30.1 [UNUSED]
osgi.wiring.host; org.glassfish.jersey.inject.jersey-hk2 2.30.1 [UNUSED]
osgi.identity; org.glassfish.jersey.inject.jersey-hk2 2.30.1 [UNUSED]
osgi.wiring.package; org.glassfish.jersey.inject.hk2 2.30.1 [UNUSED]
karaf@root()>
Theory: if I can make JerseyServlet import the org.glassfish.jersey.inject.hk2 this should force the jersey-inject-hk2 bundle to always be loaded and made active before any bundle using JerseyServlet.
Fixed by commit 99927fd