Wednesday, March 18, 2015

Correlation with a BPEL process

Correlation is basically matching an inbound message with the BPEL engine, for a specific process instance. this comes in handy in certain use cases where you might use the same id for different operations. BPEL engine needs to realize which process instance needs to go to which operation. 

For example, if we are creating a purchase order through process1 and updating an order with process2 using pruchase order id. In such a case, BPEL engine needs to select the exact process instance that was  created before, to be sent to process2. When there are multiple purchase orders executed choosing the correct process instance can be an issue. This is where correlation comes into play. 

BPEL correlation sets can solve this by providing property aliases for one or more message types.We will look into how this can be done in a BPEL process.  We are going to use WSO2 BPS(business process server) to create BPEL process, therefore you would need following resources. 

2. Eclipse (Kepler)
Follow the guide provided in the link to download and install the resources. 

Once the prerequisites are met, in eclipse go to File->New->Other->BPEL2.0 and create a new BPEL project. Next, right click on the project New->Other->BPEL2.0 and create a new BPEL  process file inside the created project. When this is done you will be provided with a template to draw your BPEL process. Create a BPEL process as below. 
Next go to .wsdl file created, and from design view you can  add a new process( -> add operation) named 'update ' as shown below. After that generate a binding for the new process by>generate binding content.

Go back to your.bpel file and click on first Reply step. In properties tab of that element , go to details section, and select operation as 'process' and get the output variable set as below.(double click on testResponseMessage so the variable field will be auto updated. )

 Next go to step 'Receive' element and go to its properties - > details section. Fill the details so that new process 'update' is selected as operation and updateRequest is set as the variable. At save, it will ask if you need to initialize, click yes. 
Go to final 'Replyoutput' step , properties-details section and fill the necessary details so that the final output will be set to updateResponse output of update operation. 

Above steps are basic steps that you would follow when creating a BPEL process, by setting where reply and receive steps should send/store the request/response. Now we move to the section where we create a correlation set at each receive step. 

1. Select first 'Receiveinput' element, and on to your right create a new correlation set. Once it's created click on Add -> New. You will be provided with the following screen. Set the name property as 'id' and select simple type from radio buttons and click on browse. From the provided list select type string. 

2.Now you have created a message property for type string. Now you need to map this message property to relevant section of the messages at your receive request. For that you need to create property aliases.  Click on New under Aliases section as shown above. Select 'testResponseMessage' input variable and create a new aliases. Same as that, create another new alias for 'updateRequest' input variable as well. 

Since you have created a correlation set and required aliases , now you need to set this correlation set at required receive steps. By doing that you will tell BPEL engine to map the relevant alias value to the created correlation property. For example map value of testResponseMessage' input variable to correlation prperty 'id'. 

3. Select first 'ReceieveInput' element and from properties tab select 'correlation' and click on Add. Make sure to set 'Initiation' property to yes in this element. That is to say that you need to initiate the correlation property in this set. 
4. Again you need to set the correlation set at your next 'receive' step. So click on that and go to 'correlations' in properties tab and click on Add. Here you need to set initiation property to No. This is because you need to use the previously initiated id at the first receive step. 

Now that you have set correlation properties, click on first Assign element and go to details section.Here we will get the input received from the first receive step, and set a message so that you could see the work of correlation when running the process. 
Click on New in details section and under From property select type expression and enter below line.
concat($input.payload/tns:input,"initiated process")
In To section select  'testResponseMessage' -> result variable. 

Go to next 'Assign1' step and in details create a new expression similar to above, but with following line. Here we will take the output of the first operation, and set it to update operation .
concat($result.payload/tns:result, "updated process") 
In To section select ' updateResponse' -> out variable. 

Finally on your project go to New->Other and add a deployment descriptor file. Select 'testPort' as associated port as below and save.
Export this project as .zip and upload it to BPS BPEL list and after deploying you could see the process instance. Go to 'try it' interface and send following values as input under operation 'process' .You would be provided with following output.

input : request1             output: request1 initiated process.
input: request2              output: request2 initiated process.

Now go to operation update and provide given input, which will give you following output values.
input: request1             output: request1 updated process.
input: request2            output: request2 updated process. 

By analyzing the output you can see that the BPEL engine was able to select the correct process instance at the operation output. This was a simple example, you could look into much advanced examples which will explain the use of correlation in BPEL processes. 

No comments:

Post a Comment