Wednesday, July 1, 2015

How to use Callout Mediator in WSO2 ESB

In a composite web service if there is a requirement to call a new external service then Callout mediator can be used. There are some other methods as well to create a composite service but in this blog we will talk about callout mediator.

This is a simple approach because it works in normal coding format from top to bottom. Whatever response we get from the callout mediator we can put our logic with that response.

Syntax:

Syntax is as mention below, which starts with callout and it asks for an URL and the action type. Suppose if in your external service has 3 operations just give a particular operation name which you have to call. If you have only one operation in that service then just give that one name.

<callout serviceURL="string" [action="string"]>
      <configuration [axis2xml="string"] [repository="string"]/>?
      <source xpath="expression" | key="string"/>
      <target xpath="expression" | key="string"/>
</callout>


With the action you have to provide the namespace of that service which is going to be invoked here.

After the callout mediator you can get the parameter response as in parameter in the property tag and use as you want.

UI configuration

To add the Callout mediator to a sequence, click Add Child at the point in the sequence tree where you want to add the mediator, and then chooseAdvanced -> Callout.


Example:




<callout serviceURL="http://XXX:8280/services/MyExternalService" action="getEmpDetails" xmlns:emp="http://soap.pm.javaiseasy.com"><source xmlns:s11="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xpath="soap:Body/child::*[fn:position()=1] | soapenv:Body/child::*[fn:position()=1]"/>               <target xmlns:s11="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xpath="soap:Body/child::*[fn:position()=1] | soapenv:Body/child::*[fn:position()=1]"/>        </callout><property name="empDetails" action="set" expression="//emp: empDetails " xmlns:emp=" http://soap.pm.javaiseasy.com "/>

In above example you can see we have given an URL 
http://XXX:8280/services/MyExternalService , action name “getEmpDetails” and the namespace “xmlns:emp="http://soap.pm.javaiseasy.com"”.
After calling the external service from the callout we are trying to capture this response from the property tag
<property name="empDetails" action="set" expression="//emp: empDetails " xmlns:emp=" http://soap.pm.javaiseasy.com "/>
Once you get the emp details as a response use wherever you want in your code.

No comments:

Post a Comment