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:
That will install the CXF Karaf features file. After that, you can run “features:list” and see the features that CXF provides.
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.