Apache CXF in OSGi

I’ve had a bunch of people asking me lately about getting Apache CXF up and running in OSGi. A lot of people have run into issues trying to find the right third party bundles, configuring things like JAX-WS and JAXB, normal “class loader” issues, etc…. Thus, I decided I need to write a blog entry. 🙂

Obviously, the EASIEST way to get CXF up and running in OSGi is to grab a preconfigured OSGi container that has CXF already setup. There are a couple of good ones out there. For “pure CXF”, the best option is the Talend Service Factory. It’s pretty much Apache Karaf and Apache CXF completely setup to run in OSGi out of the box. Very simple. If you want/need a little more functionality, you could grab the Talend Integration Factory or the Talend ESB. Both of them contain Apache Karaf, Apache CXF, and Apache Camel pre-setup to run in OSGi. If you don’t mind an older version of CXF, you can also use the latest Apache ServiceMix 4.x distributions as they would have CXF ready to run as well. However, the current downloads are based on a fairly old version of CXF. They are working on a newer version that would contain a newer version of CXF, but that may not be available for a couple weeks. Also, ServiceMix contains a lot of other things that may or may not be useful for you. For the “lightest weight” option, the Talend Service Factory is the best option.

However, not everyone wants a pre-configured thing. They may want to customize things a bit. Or maybe add some of their own things. For those types of people here are some basic instructions that I would recommend:

1) Start with Apache Karaf. Karaf is an excellent general purpose OSGi container and provides a bunch of features to make life with OSGi much easier. Grab the latest (currently 2.2.4) version, download it, and unpack it.

2) In the unpacked Karaf area, edit the etc/jre.properties file. For CXF to work we need to use OSGi enabled bundles of various API’s. The versions of the API’s built into the JDK are not OSGi enabled and thus will not work properly with CXF. In the jdk-1.6 (and 1.7 if using Java7), you need to commend out the lines:

# javax.activation, \
# javax.annotation;version="1.1", \

# javax.jws, \
# javax.jws.soap, \

# javax.xml.bind, \
# javax.xml.bind.annotation, \
# javax.xml.bind.annotation.adapters, \
# javax.xml.bind.attachment, \
# javax.xml.bind.helpers, \
# javax.xml.bind.util, \

# javax.xml.soap, \
# javax.xml.stream, \
# javax.xml.stream.events, \
# javax.xml.stream.util, \

# javax.xml.ws, \
# javax.xml.ws.handler, \
# javax.xml.ws.handler.soap, \
# javax.xml.ws.http, \
# javax.xml.ws.soap, \
# javax.xml.ws.spi, \

All of those API’s use the SPI mechanism in the JDK to find implementations. That mechanism relies on Thread contextClassLoader mechanisms that don’t work reliably in OSGi. For CXF, we have specific bundled versions of those API’s that use a proper BundleActivator/Listener method to find the implementations.

I’d also recommend editing the etc/custom.properties file to add karaf.framework=equinox to set the underlying OSGi runtime to Equinox instead of Felix. Currently, CXF is a bit better tested with Equinox. Future versions of CXF will work better with Felix, but right now, the testing is more complete with Equinox.

3) Start the container by running the bin/karaf file.

4) From the Karaf command line, type:

features:addurl mvn:org.apache.cxf.karaf/apache-cxf/2.5.0/xml/features

That will install the CXF Karaf features file. After that, you can run “features:list” and see the features that CXF provides.

5) Run

features:install cxf

To install the CXF feature. Karaf will then download and install all the required dependencies, bundles, etc… and install them into the OSGi container. It will also start the bundles. If you run “list -t 0” you should see “Apache CXF Bundle Jar” active at the bottom.

That’s really all there is to it. At that point, CXF is installed and ready to be used. Now, USING it is a subject for another blog post. 🙂 For now, your best option is to grab the Talend Service Factory examples and take a look at them. All of them are ready to run in OSGi.

8 thoughts on “Apache CXF in OSGi

  1. Hi Dan,

    Thank you for the explanation. I can’t wait to get the CXF’s new feature the WS-Notification implementation up and running without JBI and with improved addressing…

    Unfortunately, there is a sort of dependency problem by trying to install the CXF 2.5.0 into Karaf 2.2.4:

    Caused by: org.osgi.framework.BundleException: Unresolved constraint in bundle org.apache.servicemix.bundles.xmlsec [193]: Unable to resolve 193.0: missing requirement [193.0] package; (package=javax.xml)

    Could you give me any hint about how to get rid of it?

    BR
    AleksB

  2. The Camel documentation states something of note:

    “… that Karaf 2.2.5 now provides an extra configuration file that has been configured to work with Camel and CXF, so its recommended to use this file if you use Karaf 2.2.5 or newer. You can prepare Karaf by copying the etc/jre.properties.cxf and override the existing etc/jre.properties file.”

  3. Excellent blog DAN !!

    This is what I was looking for and was stuck due to cxf-osgi..

    Initially I thought that my WAR file has something wrong or it is not generated proper way by the plugin…I was not able to install the WAR in OSGi container….

    I just followed above steps in blog and it worked successfully !!!

    Thanks DAN !

  4. This is no longer working, due to broken dependencies in the features repo.
    The http feature is now out of bounds for the allowed versions need by cxf.

  5. JS Hook:
    I just tried this with CXF 2.7.3 using Karaf 2.3.1 (does not need any changes to jre.properties) and CXF just installed. With Karaf 2.2.9, if you use the jre.properties.cxf that ships with Karaf, CXF 2.7.3 also installed immediately.

  6. Hi Daniel,
    I have a very quick question. Is it possible to add Talend esb into existing Karaf 2.3.1?

    features:listurl
    true mvn:org.talend.esb/features/5.3.0/xml
    true mvn:org.apache.cxf.karaf/apache-cxf/2.7.3/xml/features
    true mvn:org.jclouds.karaf/jclouds-karaf/1.4.0/xml/features
    true mvn:org.apache.karaf.assemblies.features/standard/2.3.1/xml/features
    true mvn:org.apache.karaf.assemblies.features/enterprise/2.3.1/xml/features
    true mvn:org.apache.activemq/activemq-karaf/5.7.0/xml/features
    true mvn:org.apache.camel.karaf/apache-camel/2.10.4/xml/features

    I was able to install talend-job-api and talend-job-dependencies, but when I tried to install talend-job-controller, I received –
    Error executing command: URL [mvn:org.talend.esb/org.talend.esb.locator.server/5.3.0/cfg] could not be resolved.

    Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *