1

Im am using Jersey in an OSGi environment. I wrapped all Jetty and Jersey jars in one bundle containing also my own server / servlet / resource abstraction. This is running quite well. I am importing the javax.ws.rs-api via OSGi package import because related packages are also used by JAX-RS resource implementations in other bundles.

However, from time to time my implementation does not work with an "java.lang.ClassNotFoundException: org.glassfish.jersey.internal.RuntimeDelegateImpl cannot be found by javax.ws.rs-api_2.1.0".

It seems this could be a race condition. Maybe Jersey sometimes uses a class from my internal bundles and sometimes a class from imported javax. I cannot really figure out what the problem is.

The problem is similar to [1], but in my case it is not about any import or dependency. I wrapped the Jetty and Jersey Jars in an OSGi bundle.

[1] org.glassfish.jersey.internal.RuntimeDelegateImpl NOT FOUND

Martin
  • 129
  • 1
  • 11
  • Possible duplicate of [org.glassfish.jersey.internal.RuntimeDelegateImpl NOT FOUND](https://stackoverflow.com/questions/19452887/org-glassfish-jersey-internal-runtimedelegateimpl-not-found) – Ravi Jan 30 '18 at 15:41
  • The problem is similar, but in my case it is not about any import or dependency. I wrapped the Jersey Jars in an OSGi bundle. – Martin Jan 30 '18 at 16:10
  • I am currently testing: RuntimeDelegate.setInstance(new org.glassfish.jersey.internal.RuntimeDelegateImpl()). However, I would really like to understand the problem in order to be 100% safe and find the right solution. – Martin Jan 30 '18 at 16:11

1 Answers1

3

This problem occurs because the JAX-RS API uses many static methods to get hold of implementation types from the JAX-RS provider. In this case the error isn't coming from Jersey, but actually from the JAX-RS API itself when it tries to find an implementation of the various JAX-RS interfaces.

You effectively have four options:

  • Avoid exposing the JAX-RS API from your server by putting all the resources in there as well (I don't recommend this option!)
  • Embed the JAX-RS API in your existing Jetty/Jersey uber bundle and export it from there. Be careful to include the correct API package versions and contract capabilities! (This option is fiddly)
  • Use an OSGi-aware API bundle (such as one from Apache Aries, Apache Geronimo or Apache Service Mix). Many Java EE API bundles (JAX-RS included) package themselves as OSGi bundles, but ignore the fact that they actually need to work in OSGi, which means that you can't rely on reflectively loading types/resources from a flat classpath. (This option is probably quickest and lets you keep working as you are)
  • Move to using the new OSGi JAX-RS whiteboard from OSGi Release 7. The reference implementation for this is in Apache Aries and available on GitHub (This is the best long term option from an OSGi perspective, and means that you can avoid maintaining your own server)

I hope this all makes sense, explains why you're seeing the error, and hopefully gives you some options for working through the problem.

Tim Ward
  • 1,169
  • 8
  • 9
  • Thanks for the explanation and options! Currently I don't have the error anymore using "RuntimeDelegate.setInstance(new org.glassfish.jersey.internal.RuntimeDelegateImpl())" in my startup phase of the server bundle. Could this also be a potential solution? What is different to the re-packaged API bundle. I am already using javax.ws.rs-api_2.1.0 and it seems to be OSGi-aware. It is added to the target platform and packages are included by my server bundle. – Martin Feb 02 '18 at 09:49
  • I can't identify any specific changes in the aries API bundle compared to the original API bundle (option three): https://github.com/apache/aries-jax-rs-whiteboard/tree/master/javax.jax.rs-api – Martin Feb 02 '18 at 10:06
  • Setting an explicit RuntimeDelegate will work, but isn't a very modular solution (different implementations can fight with one another, and there are potential ordering issues). Also for the extra metadata - there's a big Provide-Capability section added (see https://github.com/apache/aries-jax-rs-whiteboard/blob/c62794dfaf5fab8e95e03815c6176bdcf9fa763e/javax.jax.rs-api/bnd.bnd#L5-L9) – Tim Ward Feb 05 '18 at 14:58