Saturday, 7 December 2013

CompensateHandler_Soa11g


Hi Guys

Today I want to discuss about Compensate Handlers

Concept

Process and service activities can be divided into unit-of-works, called transactions that are either executed (i.e. committed) as a whole or undone as a whole (i.e. rollbacked) due to some error or an explicit rollback command. When the activities within the same unit-of-work span multiple resources (databases, Java components, JMS topics, and so on) these resources all need to support global (XA) transactions and need to enlist in the global transaction, to support commits and rollbacks on the transaction as a whole.

It is not always possible to use transactions as a means to commit, or rollback related activities as a whole. For example when the invoked services do not support global transactions. Next to this, long running processes execute several (implicit) commits since transactions shouldn't be kept open too long: this degrades performance and causes completed activities to remain invisible to others. Once a commit has been executed, the committed activities cannot be rollbacked. Consider an Order-to-Process in which we book the customer's credit card in advance and deliver the ordered goods a few days later. The process will dehydrate at some point causing a commit on the active transaction. If delivery of the ordered goods fails we cannot rollback the credit card booking.

Compensation provides a mechanism to undo already committed activities by means of invoking opposite activities (i.e. compensation) in reverse order. For example booking the opposite amount of money to the credit card that was initially charged. SOA Suite provides the following activities to support compensation:
  • Compensation handler. Compensation handlers contain the activities that need to be executed as part of the compensation flow. These handlers are defined per scope, similar to catch blocks. Per scope you need decide if you need a compensation handler. 
  • Compensate activity. The activity that triggers compensation for a SOA Composite. Executing this activity will cause the invocation of compensation handlers for all successfully completed scopes that have defined a handler, and are not yet compensated. Only compensation handlers of scopes that are directly enclosed by the scope that contains the compensate activity will be executed. The handlers are executed in reverse order, so the handler of the last completed scope is executed first. 
Before we jump to the demo some considerations when using compensation:
  • Compensate activities can only be executed from catch blocks and compensation handlers.
  • Compensation activities either trigger compensation for all enclosed and completed scopes using the compensate activity (supported in BPEL 1.1 and 2.0), or can trigger compensation for one specific scope using the compensateScope activity (only BPEL 2.0).
  • Compensation handlers can only be defined on scope level, not on sequence level.
Demo


Here i am working on Simple example of mathematicalOperations.The composite contains a single, asynchronous BPEL component called CompensateDemo. After receiving the input message the process executes the following scopes and sequences:

first scope defines a normal add logic and compensatehadlers triggers when compensate activity has to be executed.



for divide logic , I designed the flow as below



Let me get in to the code in detail:

If the result of Divide='Infinity'  throws an error 'ThrowErr_DivByZero'  which is catched by catch activity
and reset the value  and throw the exception which is handled by Catchall Block




Compensate All activity cares for  Scopes to excecute their compensate Handlers









Happy Learning


                                                                                                                     By DeepthiReddy

No comments:

Post a Comment