Using the Talend cCXF component to consume a SOAP service
The Talend cCXF component is an important part of the integration developer’s toolset. It allows you to integrate SOAP web services into Talend Routes, whether you wish to provide (publish) your own service, or consume (invoke) another service. Talend’s documentation shows how to provide a SOAP service, but not how to consume one. Read this tutorial to find out how.
The cCXF component
When developing integration routes, you may need to interact with other services; for example, to fetch a customer record or to publish an update to a customer record. Commonly these will be implemented as SOAP web services.
To interact with SOAP services, Talend provides a component cCXF, which is based on the cxf component in the the underlying Apache Camel framework.
When used as a start component in a Route, cCXF can be used to provide a SOAP service. When it is used within a route flow, it is used to consume a SOAP service.
Tutorial: consuming a SOAP service in a Talend Route using cCXF
In this tutorial you will see how to invoke a SOAP service from Talend ESB. For the purposes of this tutorial I’m invoking one of the free SOAP services provided by WebserviceX.NET – specifically its Currency Converter service tool.
Note! WebserviceX.NET is a particularly popular site, so the service can run slowly at times.
Follow the steps below to invoke the SOAP service from Talend ESB:
-
Create a new route in Talend Studio.
-
From the component palette, drag the following components onto the new route canvas: cTimer, cSetBody, cCXF, cLog.
-
Connect each component to the next in the order given above, by right-clicking each component and selecting Route → Row. The route should now look similar to the following screenshot:
- Double-click the cTimer component and set the following properties:
- Period: 1000
- Repeat: 1
- Delay: 1000
-
Double-click the cSetBody component. In the component properties, set the Language to Simple and paste in the following string in the Expression box (including the surrounding quotes):
"<ConversionRate xmlns="http://www.webserviceX.NET/"><FromCurrency>EUR</FromCurrency><ToCurrency>GBP</ToCurrency></ConversionRate>"
This will be used as the input to the web service.
- Double-click the cCXF component and set the following properties:
- Address:
"http://www.webservicex.net/CurrencyConvertor.asmx"
(including quotes) - Type: wsdlURL
- WSDL Type: File
- Wsdl file:
"http://www.webservicex.net/CurrencyConvertor.asmx?WSDL"
(including quotes) - Dataformat: PAYLOAD
- Ensure the Service box is ticked
- Service name:
"{http://www.webserviceX.NET/}CurrencyConvertor"
(this is defined within the WSDL) - Port name:
"{http://www.webserviceX.NET/}CurrencyConvertorSoap"
(this is defined within the WSDL)
- Address:
Now run the Route. Once it has initialised, and after a delay of 1 second, a call will be made to the Stock Quote web service and the response will be displayed in the console:
[INFO ]: cCXFConsume.cLog_1 - Exchange[ExchangePattern: InOnly, BodyType: org.apache.camel.component.cxf.CxfPayload, Body: **<ConversionRateResponse xmlns="http://www.webserviceX.NET/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><ConversionRateResult>0.7107</ConversionRateResult></ConversionRateResponse>**]
How does it work?
We use a cTimer component to start the route automatically for demonstration purposes. Normally you would use an appropriate component to receive a request, such as cJMS or cFile.
The cSetBody component sets the body of the Exchange, which will become the message that we send to send to the currency service. In this example, we wish to retrieve the currency conversion rate from Euros (EUR) to British Pounds (GBP).
The cCXF component submits the message to the SOAP service and returns the response. CXF handles all the necessary SOAP work for us, such as creating the message envelope and setting any relevant headers. When we use CXF, we need to specify a service name and a port name. These are taken from the service’s WSDL. You must define these properties so that CXF knows exactly which service should be invoked. The service name is given in the wsdl:service element in the WSDL, and the port name is given in the wsdl:port element.
The final cLog component simply logs the current contents of the Exchange to the console. In this instance, this contains the response from the SOAP service - which is our requested currency exchange rate.
Don’t forget, when you’ve finished, you should decide how you will deal with faults from the SOAP service and implement an appropriate fault handling strategy.
Installing dependencies
When you edit the cCXF component properties, you may see a message similar to the screenshot below asking you to install an external JAR.
To resolve this issue, click on the Install button and follow the steps given to download and install the relevant modules:
Note that you may have to accept some licence agreements before continuing.
Did this tutorial help you understand how to use cCXF to consume a web service? Please do post your feedback below!
Photo credit: Ari Moore