Camel CXF Component – WSDL First Example

cxf-eipThis is the first of a series of posts of simple examples in Camel designed to show a single simple use case. In this one I’ll show how to create a consumer and producer using the Camel CXF component and a WSDL first approach.

  1. Generate source from the WSDL
  2. Create a Consumer (our server)
  3. Create a Producer (client) that also acts as the test

Generate Source

The WSDL is simple. It defines two methods.

  1. getCustomersByName
  2. updateCustomer

CustomerService.wsdl

Setup maven to generate the java code from the WSDL using the cxf-codegen-plugin

Now when “mvn compile” is run the CXF plugin will generate source into the directory: target/generated-sources.

Create the Consumer

In this example the consumer and producer are running in separate VM. Since the CXF endpoint parameters are shared between both it is defined in a separate file cxf-context-xml

The Spring context for the consumer is in the file camel-server-context.xml. We setup a beans for CustomerServiceProcessor and CustomerServiceRoutes (which is a Camel route builder) and then tell Camel to use the latter to build it’s routes.

The consumer route is quite simple providing a CXF endpoint referenced above and routing that to the CustomerServiceProcessor.

The CustomerServiceProcessor determines the operation name and handles it by calling the matching method on the CustomerService class.

 Create the Producer

The context for the producer, found in camel-client-context.xml,  only differs in the route builder class specified.

The producer is a bit more complicated. Three routes are defined to test getCustomersByName, NoSuchCustomerException and updateCustomer. A fire once timer calls all three tests.

The first test looks for NoSuchCustomerException. It does so by defining Exception handling for the route then calling the service using a non existent customer.

The second test calls getCustomersByName and expects to get 2 results back.

The third test gets a customer, modifies it, then calls updateCustomer. This test is divided into 3 routes for clarity.

 

Run the Tests

To run the tests in one terminal window type ‘mvn -Pserver’ then in another type ‘mvn -Pclient’. The server will show a stacktrace for the NoSuchCustomerException but this is expected. The client will show the following at the end of it’s output.

The full source for this example can be found on Github at http://github.com/tjsnell/camel-cxf-wsdl-first.

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

Comments on this entry are closed.

  • tjsnell

    disqus test

  • Andrey

    Please change cxf endpoint:

    to:

    otherwise SAX parser throws exception while generating wsdl.

    BTW. This example not working in SoapUI.

    • tjsnell

      Fixed! Thanks. I meant to get to that error and forgot.

      I just ran the server and tested with SoapUI with no errors. What happens when you try?

      • Andrey

        nevermind, my fault, SoapUI is worked well ))

  • Rajesh

    I was setting this up but wsdl2java conversion is not happening.The in pom file was 2.8.0-SNAPSHOT on which i was getting an error. I changed that to 2.7.10 after which mavan error went away but still class generation is not happening. Any idea why?

    • Rajesh

      Ok. It was getting generated in target folder

  • Udayakumar Reddy

    Hi there, am very new to cxf and want to run this example as part of tomcat. How can I run this application in tomcat?? please suggest