Monday, September 14, 2009

Consuming external web services

Example of how to call an external Web Service from a BPEL process:

Follow these steps:
  1. First, download and install "HelloWorld" Web Service into your servlet container. This simple service has 2 operations:
    1. sum: returns the result of adding two numbers. This is the operation we will use.
    2. example: concatenates your input with "External WebService".
  2. Before calling this WS check if the web service is working fine. Test the "sum" operation.
  3. Open your Eclipse and create a new BPEL project. Name it as "HelloCaller".
  4. Create a new BPEL process file with the following data:
    1. BPEL Process name: helloCaller
    2. Namespace: http://mydomain.org/bpel/helloCaller
    3. Template: Synchronous BPEL process
  5. Add the HelloWorld WSDL file into the process. Be sure your file is visible to your project.



  6. Add a Partner Link and named it as "helloLnk".
  7. Open the details of "helloLnk" and click on "Browse" button. Then a dialog will appear, at the bottom click over the "Add WSDL" button. Select the HelloWorld WSDL file.
  8. Select the port named as "HelloWorldServicePortType"and click "OK" button; a new dialog will appear.
    1. Define the name of your Partner Link Type as "helloPLType" and click "Next" button.
    2. Type a Role Name as "helloProvider" and click "Finish" button.
  9. Bellow the "Partner Role" section select "helloProvider".



  10. Open the BPEL process (helloCaller.bpel) with the BPEL designer and add the following activities and name them as suggested:
    1. Assign: InitializeVars
    2. Invoke: SumNumbers
    3. Assign: SetResult

    You should see your diagram as the following:



  11. Open the properties of "SumNumbers" and select the "sum" operation from the "HelloWorldPortType".











  12. Open the properties of "InitializeVars" activity and add a copy step.
    1. From: Fixed Value

      <ns0:sum xmlns:ns0=http://webservice.augusto.org>
      <ns0:in0/>
      <ns0:in1/>
      </ns0:sum>

    2. To: Variable
    3. Select helloLnkRequest the "and then "parameters"


  13. Add a new copy step to "InitializeVar" with the following data:



  14. And add the last copy step to the "InitializeVars"


  15. Now, select the "SetResult" and add 2 steps. In the first one initialize the response:
    1. From: Fixed Value

      <tns:helloCallerResponse xmlns:tns="http://mydomain.org/bpel/helloCaller">
      <tns:result/>
      </tns:helloCallerResponse>
    2. To: Variable
    3. Select output the and then "payload"
  16. Finally, copy the result from the "HelloWorld" service to the output of your BPEL process.
  17. You need to define the service and the location of it.
  18. Define the deployment descriptor. Remember that for every partner link used with a <receive> activity must be matched with a <provide> element, and every partnerLink used in an <invoke> activity must be matched with an <invoke> element in deploy.xml. For example, for this process the following code is used:
<process name="pns:helloCaller">
<active>true</active>
<provide partnerLink="client">
<service name="pns:helloCallerService" port="helloCallerPort" />
</provide>
<!-- Hello World Web Service -->
<invoke partnerLink="helloLnk">
<service name="wns:HelloWorldService" port="HelloWorldServiceHttpPort" />
</invoke>
</process>

  1. Now, deploy your helloCaller service and test it. Download the example from here.


Software required to run the example:
  1. Eclipse
  2. BPEL designer for Eclipse - http://www.eclipse.org/bpel/
  3. Apache ODE 1.3.2

References

  1. How to create a Partner Link

1 comment:

Danny said...

thanks for the post! this one is really helpful.