Bundle loading order - Jersey service

1,193 views
Skip to first unread message

John Doe

unread,
Jun 20, 2017, 1:25:41 PM6/20/17
to bndtools-users
Hello,

i tried to work with glassfish-jersey.
If i use the all-in-one bundle (eclipsesource or selfe-bundled) everything is fine.

But why using all-in-one bundles when all jar's are bundles.
In the project are all dependencys by using aQute.bnd.repository.maven.provider.MavenBndRepository

I run the Projekt with this requieres bundles:
https://github.com/JohneDoe/osgi-sandbox

-runrequires: \
    osgi
.identity;filter:='(osgi.identity=org.apache.felix.webconsole)',\
    osgi
.identity;filter:='(osgi.identity=org.apache.felix.gogo.jline)',\
    osgi
.identity;filter:='(osgi.identity=org.apache.felix.gogo.command)',\
    osgi
.identity;filter:='(osgi.identity=org.glassfish.hk2.osgi-resource-locator)',\
    osgi
.identity;filter:='(osgi.identity=org.glassfish.jersey.inject.jersey-hk2)',\
    osgi
.identity;filter:='(osgi.identity=my.sandbox.rs.server)',\
    osgi
.identity;filter:='(osgi.identity=my.sandbox.rs.server.example)'


org.glassfish.jersey.inject.jersey-hk2 -> HK2 injection for jersey
org.glassfish.hk2.osgi-resource-locator ->rely on META-INF/services mechanism to locate providers



Depending on the order of the bundles in the -runbundles:  property it runs or fails


runing (servlet bundles at last position?):
https://github.com/JohneDoe/osgi-sandbox/blob/master/my.sandbox.rs/Example1Working.bndrun
https://github.com/JohneDoe/osgi-sandbox/blob/master/my.sandbox.rs/Example2Working.bndrun

failing:
https://github.com/JohneDoe/osgi-sandbox/blob/master/my.sandbox.rs/Example1Fail.bndrun
https://github.com/JohneDoe/osgi-sandbox/blob/master/my.sandbox.rs/Example2Fail.bndrun


when failing the problem is that the:
org.glassfish.hk2.osgi-resource-locator did not Activate.

lb:
   23|Active     |    1|javax.inject:1 as OSGi bundle (2.5.0.b36)|2.5.0.b36
   
24|Active     |    1|ServiceLocator Default Implementation (2.5.0.b36)|2.5.0.b36
   
25|Starting   |    1|OSGi resource locator bundle - used by various API providers that rely on META-INF/services mechanism to locate providers. (1.0.1)|1.0.1
   
26|Active     |    1|HK2 Implementation Utilities (2.5.0.b36)|2.5.0.b36
   
27|Active     |    1|jersey-container-servlet-core (2.26.0.b06)|2.26.0.b06
   
28|Active     |    1|jersey-core-client (2.26.0.b06)|2.26.0.b06

and because of that it could not read the META-INF/services and lookup the InjectionManagerFactory

[ERROR] ([javax.servlet.Servlet]) Error during calling init() on servlet my.sandbox.rs.server.example.ExampleServlet@3116c353
java
.lang.IllegalStateException: InjectionManagerFactory not found.
    at org
.glassfish.jersey.internal.inject.Injections.lambda$lookupInjectionManagerFactory$0(Injections.java:98)
    at java
.util.Optional.orElseThrow(Optional.java:290)
    at org
.glassfish.jersey.internal.inject.Injections.lookupInjectionManagerFactory(Injections.java:98)
    at org
.glassfish.jersey.internal.inject.Injections.createInjectionManager(Injections.java:93)
    at org
.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:282)
    at org
.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:335)
    at org
.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:178)
    at org
.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:370)
    at javax
.servlet.GenericServlet.init(GenericServlet.java:158)
    at org
.apache.felix.http.base.internal.handler.ServletHandler.init(ServletHandler.java:122)
    at org
.apache.felix.http.base.internal.handler.WhiteboardServletHandler.init(WhiteboardServletHandler.java:57)
    at org
.apache.felix.http.base.internal.registry.ServletRegistry.tryToActivate(ServletRegistry.java:358)
    at org
.apache.felix.http.base.internal.registry.ServletRegistry.addServlet(ServletRegistry.java:161)


What is the right way to solve my problem/handle the loading order?
- import org.glassfish.hk2.osgi-resource-locator into all bundles where i use Jersey?
- bundle-start levels?
- repacking org.glassfish.hk2.osgi-resource-locator into my server.base bundle?

Karel Haeck

unread,
Jun 22, 2017, 10:31:29 AM6/22/17
to bndtools-users

Jersey initialization in OSGi is rather tricky. 
In general you should not use any Jersey or HK2 class before its corresponding bundle has been started.
Besides the Activator in the osgi-resource-locator, there is also static initialization code in jersey core that takes care of auto registration of features,
and should only run after the bundle is started.

To avoid playing with bundle start levels,  one can use a bundletracker to wait for all Jersey and relevant HK2 bundles  to start,
and then publish a "JerseyReady" service that other components can reference.
In addition the  org.glassfish.hk2.osgi-resource-locator uses the lazy bundle activation policy, so one must take care not to wait for ever


Op dinsdag 20 juni 2017 19:25:41 UTC+2 schreef John Doe:

devnull

unread,
Jan 18, 2019, 9:39:00 AM1/18/19
to bndtools-users
version 3.1.0-rc-3 solves to me.

Tim Ward

unread,
Jan 18, 2019, 11:03:37 AM1/18/19
to bndtool...@googlegroups.com
Have you tried using the JAX-RS Whiteboard implementation from Aries rather than using Jersey? It has been packaged in a way to make using JAX-RS really simple.

Best Regards,

Tim

--
You received this message because you are subscribed to the Google Groups "bndtools-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bndtools-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages