CXF Example

CXF example for routing messages with different transports

The Camel CXF example is a demo of the camel-cxf component to show how to route messages between CXF endpoints, with one endpoint consuming a SOAP over HTTP request while the other providing a SOAP over JMS request for the actual CXF Service endpoint. The Camel router just routes the SOAP over HTTP CXF client request to the SOAP over JMS CXF service.

To run the example see instructions in the README.txt file in the camel-example-cxf directory under examples.

We start with creating a CamelContext - which is a container for Components, Routes etc:

{snippet:id=e1|lang=java|url=camel/trunk/examples/camel-example-cxf/src/main/java/org/apache/camel/example/cxf/httptojms/CamelCxfExample.java}

Here is code for starting the JMS broker and the CXF server:

{snippet:id=e2|lang=java|url=camel/trunk/examples/camel-example-cxf/src/main/java/org/apache/camel/example/cxf/httptojms/CamelCxfExample.java}

Now we setup the router for the two endpoint URIs which we mentioned before, ROUTER_ENDPOINT_URI is the endpoint for consuming the request of SOAP over HTTP, SERVICE_ENDPOINT_URI is the endpoint for providing the request for the SOAP over JMS service.

{snippet:id=e3|lang=java|url=camel/trunk/examples/camel-example-cxf/src/main/java/org/apache/camel/example/cxf/httptojms/CamelCxfExample.java}{snippet:id=e4|lang=java|url=camel/trunk/examples/camel-example-cxf/src/main/java/org/apache/camel/example/cxf/httptojms/CamelCxfExample.java}

Then we start up the camel context and create the CXF client to kick off the request.

CXF example for using Camel transport

Since Camel has lots of components and is a great mediation rule engine, we can leverage these Camel features in CXF by using the Camel transport. Here is an example to show how to use the Camel transport in CXF.

In this example, we setup a load balancing Camel context in the Camel destination.

{snippet:id=example|lang=xml|url=camel/trunk/examples/camel-example-cxf/src/main/resources/org/apache/camel/example/camel/transport/CamelDestination.xml}

When CXF loads this configuration, it will publish the CXF endpoint to the Camel endpoint that is found in the Camel context.

{snippet:id=e1|lang=java|url=camel/trunk/examples/camel-example-cxf/src/main/java/org/apache/camel/example/camel/transport/Server.java}

The code below shows how to publish the endpoint to different Camel context endpoints. You can specify the Camel endpoint URI as the endpoint address parameter.

{snippet:id=e2|lang=java|url=camel/trunk/examples/camel-example-cxf/src/main/java/org/apache/camel/example/camel/transport/Server.java}

CXF example for using the WebServiceProvider API

JAX-WS provides the WebServiceProvider API to meet the requirements of XML-level message handling. This example shows how to route a SOAP Message from a WebServiceProvider implementation to bean endpoint in the Camel context. Basically, we still pass the WebServiceProvider implementor as SEI, and the CxfConsumer will pass the SOAP Message in a list of parameters to the Camel router for further processing.

First, let's have a look at the Camel context's configuration. Here we start a CxfConsumer to listen to the address and pass SOAP messages to the test bean.

{snippet:id=example|lang=xml|url=camel/trunk/examples/camel-example-cxf/src/main/resources/META-INF/spring/CamelCXFProviderRouteConfig.xml}

Below is the WebServiceProvider implementor's annotation. In this code, we just want to handle SOAPMessages in the Message mode:

{snippet:id=e1|lang=java|url=camel/trunk/examples/camel-example-cxf/src/main/java/org/apache/camel/example/cxf/provider/GreeterProvider.java}

Since the CXF-Camel component will replace the CXF invoker to call the processor when starting a CXF endpoint, the CXF-Camel consumer will never call the provider implementor's invoke method here.

{snippet:id=e2|lang=java|url=camel/trunk/examples/camel-example-cxf/src/main/java/org/apache/camel/example/cxf/provider/GreeterProvider.java}

Now we can implement the bean for handling the invocation of the SOAP message:

{snippet:id=e1|lang=java|url=camel/trunk/examples/camel-example-cxf/src/main/java/org/apache/camel/example/cxf/provider/TesterBean.java}
  • No labels