Thursday, February 18, 2016

How to Create BPEL process in WSO2 BPS (Service Orchestration)

WSO2 BPS uses Business process execution language to do orchestration. We can use some internal, external service or xpath calculation inside the BPS process. In this tutorial, we are going to solve a formula (X^2+Y^2)^2. To solve this formula we need two services, 1st which gives the square result of a value and 2nd provides the adding functionality. Both square and adder service have been created in ESB. We will use this ESB WSDL in this BPS process.

So first we need to create 2 services which provide square and adding functionality.

ESB Services:

  • I have created GetSquareService in ESB which takes input as X and provides its square result as mentioned below.

  • There is another service I have created which returns addition of two numbers.

By these two above service, I will be able to solve the formula in BPS because formula uses only square and addition functionality.

BPS Service Creation:

To start with BPS process, we need to have eclipse which has the plugins to create BPEL flow. If you don’t have it, please download from WSO2 website.

Once you have pre-requisite things you can start with below steps.

  • Go to file menu, select new then other, below window will pop up and choose the BPEL project.
  • Give a project name, you are going to create. I have given FormulaCalculator for this blog as below. Click on finish.
  • Now create a BPEL process file in which we will define our process flow and complete logic. To do that chose the “New BPEL Process File” as below and click on Next button.
  • Click Next button to give BPEL file name, namespace and template type, in this tutorial I have given  BPEL name as FormulaCalculator, namespace as and chosen synchronous type template as mentioned below.
  • Click Next button, below screen will pop up with some information. If you want to make any changes then make it, otherwise click on finish.
  • If you click on the Next button then below screen will come otherwise click on Finish button.
  • Once you click on finish button, your file will look like below screenshot in which you can write your logic.

If you see at left hand side files, there are to files have been created. One is BPEL file which we have created and other one is WSDL file (FormulaCalculatorArtifacts.wsdl). This WSDL actually decides that what kind of parameter this BPS process is going to take and which kind of output, this process provides. 

  • So as we have decided that we are going to solve (X^2+Y^2)^2 formula so according to this input should be 2 as part of X and Y. In below screenshot we are making changes in WSDL file according to our requirement.
  • I have made changes in request field and this takes input as X and Y as mentioned below screenshot.
  • Same we have make changes in output as response as mentioned below. In this process our output will be one, which I have taken it as FormulaResult.
  • Now copy ESB WSDL in this project which we have created in ESB one is for Adder Service and other one is for the square service. It should be as below screen.
  • BPS process provides functionality to create partner link in BPEL in order to use services in our logic. If we have to use GetSquareService and AddNumberService, we have to create two Partner link, one for each. To create a partner link, you have to follow below screen. Click on the + icon given in right side and provide the necessary information. Partner link is same as library file in java, we import library file to use their functionality. Same partner link invokes service to be used inside the BPEL logic.
If you see in above screenshot, I have created 2 Partner Links one is GetSquarePartnerLink and other is AddNumberPartnerLink

  • Once you define a Partner Link, we have to link it with a defined WSDL. To do that, select the Created Partner Link and go to property. In below screenshot, I have selected AddNumberPartnerLink and this need to be linked with WSDL.
  • Click on Property, you will see below screen.
  • Click on the browse button, below screen will popup. Select the AddNumberServicePortType where you will be able to see Partner Link Type Structure.
  • Click on the Add WSDL button, below screen will pop up.
  • Select the WSDL “AddNumberService.wsdl” and click on the ‘OK’ button. Below Screen pop ups in which you have to define Partner Link Type Name.
  • Provide the Partner Link Type name and click Next button. Below screen will be shown in which you have to provide Role Name and select the Port Type. Here we have given Role Name as AddNumberRole and select AddNumberServicePortType. Then click on Finish button.
  • Below screen should be shown in which you can see partner Role and Partner Operation.
  • Repeat the above steps to create another Partner Link for the other service (GetSquareService).
Once you are done with the Partner Link then create variables to be used in the BPEL logic.
  • Click on the below highlighted area in Yellow, give the variable name. In this screenshot I have given variable name for GetSquare request and GetSquare response with the name of “GetSquarePartnerLinkRequest” and “GetSquarePartnerLinkResponse” now select GetSquarePartnerLinkRequest and go to the property.
  • Below Screen will popup, in which you can select GetSquareRequest variable and click OK button as mentioned in below screenshot.
  • You can see in below screenshot that it takes only one parameter as X.
  • In same way as above link GetSquarePartnerLinkResponse with GetSquareResponse.
  • There are some local variable required which holds temporary results like for square, addition etc. these variable can be defined as below. It’s same as we define for other variable. Click on + button in variable lane and give the variable name as Value_Y.
  • Choose this variable as string type, you can select this as any data type like integer, Boolean etc. but in this case I have taken it as String.
  • Once you select this, can see in property that variable is now showing as String datatype
  • In same way as mentioned above, you have to create Value_X, Value_Y and AddedResult variable of String datatype.
  • Now we are all set to proceed with BPEL flow, to start with flow activity you need to use Palette’, where you can see there are lots of activity icons. If it is not open there in eclipse, go to ‘Window’ menu then open ‘Show View’ then select other. In pop up window just type ‘Palette’, below screen will be shown.
  • Now we need to create our logic flow by using this Palette. Click on the “receiveInput” in default BPEL flow and go to the property. You will see below screen just check that “Partner Link” should be ‘client’, Operation should be “FormulaCalculator” and variable should be “input”. This represents that we will receive input from here and executes operation “FormulaCalculator”.
  • Same as above we have to choose output by selecting the “replyOutput”.
  • Screen should look like as below.
  • In this flow now we are trying to invoke a service as mentioned below. To invoke a service we need to use invoke activity from the palette. Choose the invoke activity and provide a desired name, for this tutorial we have given “InvokeGetSquareService”.
  • After clicking the invoke property, we need to link it with service operation and its request & Response. If you see below screenshot, I have selected Invoke property. Partner link showing as GetSquarePartnerLink, operation is showing as GetSquare, request and response showing as GetSquarePartnerLinkRequest and GetSquarePartnerLinkResponse respectively. 
  • In a same way we have to use invoke activity for other service. Before calling any service we have to pass some parameter if service requires any parameter to be passed. This can be done from the “Assign Activity” so for every invoke service I have used Assign Activity before invoke activity. In order to calculate formula we have to invoke 4 times service and 5 times assign service. Flow should be as mentioned below. Wherever I have given name as Invoke Square service, I am using GetSquare Service and wherever I am using name as Invoke Add service, I am calling add service. And in same way we have to choose its request, response, operation and parameter.
  • After creating above flow, now time to assign parameter to the service and whatever response we get it from service as response, we can use it as parameter for other service. To assign first parameter to the service, select “AssignGetSquareInput” assign activity and open property tab.
  • Click on highlighted “New” button  and choose variable to variable (From: variable and to: variable)
  • Select “InputValue_X” from left part (From left section) and link it to the X variable in right part (Right Section). A pop up will appear asking that “variable does not have initializer, should it be generated”, click on “Yes” button. This is done when we are calling a service first time, otherwise it is not required. This is same as we initialize variable in java and in other programming language. Below screen comes.
  • Now default value is pointing to the parameter of GetSquare operation.
  • Now click on next Assign Activity (just after invoke activity) and choose GetSquareResponse to the Value_Y.  Because we know that after invoke service we should get a proper result and this result needs to be stored somewhere so using Value_Y for this.
  • In above screenshot, again we need to get square of second parameter so in a same way we have invoke Square service and pass the parameter  (InputValue_Y) to the service.
  • Now in below screenshot we are trying to get square response of “InputValue_Y” and storing it into another variable (Value_X).
  • In this stage we have got X^2 and Y^2 result which is stored in Value_X and Value_Y variable like X^2=Value_Y and Y^2=Value_X. now we need to add these value, to do that I am passing this value to AddNumberService as mentioned below. I am passing Value_X to X parameter of Add Service, to do that click on below highlighted activity flow.
  • Once you click on assign activity, open property window and assign variables.
  • Once you pass this variable to AddNumberService parameter, it will ask you to initialize this service variable because as of now it is not initialized. Click “Yes” to get this initialized.
  • Now pass other parameter Value_Y to Y in AddNumberService as mentioned below.
  • Now stored the response of AddNumberService into an “AddedResult” variable. To do that click below highlighted assign activity and go to property window.
  • In below screen I am setting AddResult to AddedResult variable.
  • In this position we have reached to X^2+Y^2 and result of this value is stored in “AddedResult” variable and now just need to get square of this variable. In a same way as above pass the AddedResult to value X (Right side) and get the square of it by invoking the Square Service.
  • Just after invoking Square Service store the response in Formula Result. This will be the final result we want to get. To do that just select Square Result and assign it to “FormulaResult” as defined in below screenshot.
  • Our flow is done and this service is now needed to be deployed. If you want to see the code written behind this flow just click on source tab and see the code.

  • This service needs a deployment descriptor file for the deployment in which we define the endpoints references and other details. For adding deployment descriptor file: Go to the File-->New -->others and chose “Apache ODE Deployment Descriptor” as mentioned below.

  • Click Next.
  • Click Finish. File looks as below screenshot.
  • In Inbound Interfaces (Services), open dropdown by clicking Associated Port and select FormulaCalculatorPort as mentioned below.
  • In Outbound Interfaces which we need to invoke (in our case AddNumberService and GetSquareService) so select the reference Endpoints of these services.
  • For deploying this BPS process, we have to make .zip file and upload it from the console UI. So copy all the required file and make a zip of that.
  • Start the BPS server and login to console UI.
  • Click on the highlighted “Add BPEL” link above page will be open. Click on browse button and select the .zip file and click on upload.
  • If everything goes successful then above screen will popup saying “BPEL package uploaded successfully”.
  • If you now check the List of services then FormulaCalculator will be shown as above screenshot. Now to test this service just click on the “Try this Service” below screen will come and test it with desired parameter.
  • In above screenshot I have passed values 3 and 4 and result is coming right as 625
(X^2+Y^2)^2 = (3^2+4^2)^2 = 625
  • This is what we wanted to achieve.