Introduction

Apache CXF JAX-RS and JAX-WS clients can be configured to become failover-capable. Core CXF Failover and Load Distribution features are supported. It is also supported by OSGi blueprints, including the recent failover implementation based on circuit breakers. We are going to illustrate the typical example of using OSGi blueprint for registering failover-aware clients in Apache Karaf 4.0.x container.

Prerequisites

As our example is going to be based on circuit breakers failover, there are a couple of features and additional bundles required to be installed.

feature:install cxf-jaxws cxf-features-clustering

install -s mvn:org.apache.cxf.samples/failover_server
install -s mvn:org.apache.cxf.samples/failover_jaxws_osgi
install -s wrap:mvn:org.codeartisans/org.json/20130213
install -s mvn:joda-time/joda-time/2.8.1
install -s mvn:org.qi4j.core/org.qi4j.core.functional/2.1
install -s mvn:org.qi4j.core/org.qi4j.core.api/2.1
install -s mvn:org.qi4j.core/org.qi4j.core.io/2.1
install -s mvn:org.qi4j.core/org.qi4j.core.spi/2.1
install -s mvn:org.qi4j.core/org.qi4j.core.bootstrap/2.1
install -s mvn:org.qi4j.library/org.qi4j.library.jmx/2.1
install -s mvn:org.qi4j.library/org.qi4j.library.circuitbreaker/2.1

Blueprint Configuration

Once all prerequisites are installed, the configuration becomes really trivial, for example here is the OSGi blueprint snippet which registers JAX-WS  failover-aware client. Although it uses circuit-breaker-failover, the way to register regular failover and loadDistribution features is very similar.

<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jaxws="http://cxf.apache.org/blueprint/jaxws"
       xmlns:clustering="http://cxf.apache.org/clustering"
       xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
                           http://cxf.apache.org/blueprint/core http://cxf.apache.org/schemas/blueprint/core.xsd
                           http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/blueprint/jaxrs.xsd">

    <service ref="sampleServiceProxy" 
        interface="org.apache.cxf.samples.failover.jaxws.SampleService" />

    <jaxws:client id="sampleServiceProxy" serviceClass="org.apache.cxf.samples.failover.jaxws.SampleService">
        <jaxws:features>
            <clustering:circuit-breaker-failover>
                <clustering:strategy>
                    <bean id="strategy" class="org.apache.cxf.clustering.SequentialStrategy">
                       <property name="alternateAddresses">
                           <list id="alternateAddresses" value-type="java.lang.String">
                               <value>http://localhost:8181/cxf/sample</value>
                               <value>http://localhost:8282/cxf/sample</value>
                           </list>
                       </property>
                    </bean>
                </clustering:strategy>
            </clustering:circuit-breaker-failover>
        </jaxws:features>
    </jaxws:client>
</blueprint>

Samples

There are sample projects available: distribution\src\main\release\samples\clustering\failover_jaxws_osgi