Friday, April 09, 2010

Understanding Catch and CatchAll activity in a BPEL process

Most of you must be familiar with the java concept and the fault handling mechanism in java how we used to handle exceptions and then throw it.

We have some similar concept in BPEL.In BPEL we have mainly two kind of exception

1>Runtime Exception

Runtime exception is something we don't have control in,like server crash,memory or jvm issue network connectivity.It is not possible to catch these issues easily as the reasons could be anything -(minus) business exception

2>Business exception

Business exception is something which occurs in our business process,lets suppose we have a credit card validation process,then what can be the business exception ,it can be an exception when you are giving an invalid number or it can be when you are trying to make a transaction when you don't have any amount in your account and many more,so these business exception are already known to us that can exist when we design our business process so we must design our program is such a way to handle these issues.A business excetion occurs when an application executes a throw activity or when an invoke activity receives a fault as a response. The fault name of a business fault is specified by the BPEL process. The messageType is defined in the WSDL. A business fault can be caught with a faultHandler using the faultName and a faultVariable.


We will design a business process and try to understand how the catch works.

Lets create a simple asynchronous BPEL process.I just gave it name CatchDemo so it looks like



Now click on the variables field and then this box will be opened.



Now click on create and create a new variable ,i am calling it as an error variable and then i will choose the message type and select the CatchDemoRequestMessage



So my variable will appear some thing like this



Click apply and so ok on the dialogue box.

Now drag and drop an assign activity between the receive and callbackclient and create a copy opeartion, there in assign a string value to the error variable as shown below.



Now again drag and drop a scope after the assign activity.

Expand the scope activity and drag and drop a throw activity inside the scope so your flow till now should look like this.




Now double click the throw activity and in the namespace uri provide

http://xmlns.oracle.com/CatchDemo

and local part as "InvalidFlow"

Again in the faule variable click on torch icon and select the error variable we have created.So the fault should have setting like this



So this fault block that we have defined over here means that we have received a fault on error variable and for that we are throwing a fault "InvalidFlow".

Here we are just trying to replicate the real time scenario,here InvalidFlow fault is thrown by the process if we will not handle this or catch this the process will terminate here and the end user will not know what happened to his request which is not a gud designing approach so we will catch this error using a catch block.

In the scope left hand side click on the catch and a catch block will be attached to the flow automatically.



Now double click on the catch activity and provide the same details as you have provided in the Throw

that is namespace URI is

http://xmlns.oracle.com/CatchDemo

Local part is InvalidFlow

and the variable is error.

so the catch block should look like




Now drag and drop an assign acitivity after the catch block ,make a copy operation and assign some data that you want the end user to see in case this falue is occured.

I will put some values to it




So say ok and apply all these changes,save all the changes and your full flow should look something like this.




Now just deploy this process to the bpel server and check for the output
I invoked the process with the some random input and here is the output in BPEL console





and this is the output that end user will see in the callback client.

callbackClient
[2010/04/09 08:55:40]
Skipped callback "onResult" on partner "client".
- <outputVariable>
- <part xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="payload">
- <CatchDemoProcessResponse xmlns="http://xmlns.oracle.com/CatchDemo">

<result>

Mistake in input variable

</result>

</CatchDemoProcessResponse>

</part>
</outputVariable>


If we had not defined the catch the process wud have terminated and the end user will had no clue wat was the issue in this scenario he knows that the mistake is in input so he can try again.So i believe this demo sud have given you some idea about the catch.Now we will futher make some modification in the code and try to understand catchAll in the same flow.


Now just add a catch all block in the flow and drag and drop an assign activity after that ,make a copy operation and assign some value to output.



So now your flow should look like this



Just a quick word about catchAll you must be asking why we are using a catchAll block it is because there may be other business exception also which we are not aware or so in order to catch them we define a cathAll block ,That is if a fault is not catch by a catch block then finally it can be caught by a caughtAll block.
so we can have multiple catch block but only one cathAll.

Now we will just make slight change in the throw activity.

Double click on throw activity

and change the localPart from InvalidFlow to some other name.



Now deploy the process again and check the flow.i got the following output in the BPEL console






Now you can see i am getting the output which i have defined in catchAll block



So the bottom line is catch all the exception which you believe can occur and for other use a catchAll.

No comments: