Compensation can be used to undo or compensate for updates that have been completed. This compensation service backs out committed updates or takes an alternative action. For example, if an activity in a workflow updates a database, the corresponding compensation action reverses the update. If the activity is to send an e-mail to a customer, then the compensation action sends another e-mail to inform the customer that the previous e-mail is void.
In workflow, each service activity might have a compensation service associated with it. This service activity is called the forward service and is invoked when the process runs forward. The compensation service is invoked only if the forward service has been completed. The optional compensation service is executed to compensate the actions of the forward service when a fault happens in the process. See the sidebar How compensation works for more details.
Compensation service in the OTMPS scenario
In our OTMPS scenario, customers can submit a batch of orders for fulfillment. They expect either all the orders to be forwarded to the manufacturing plants, or none to be forwarded. After validation of all the orders, each order is sent to the correct manufacturing plant for fulfillment. If any of the manufacturing plants reject or fail to process an order, then the complete manufacturing process for the batched orders is revoked, and the system notifies the customer of the problem. As shown in Figure 1 below, this part of the workflow is composed of a "while" loop called "Manufacturing Loop". This loop continues until all the orders are processed. Each order is sent to the corresponding manufacturing plant inside the while loop, according to manufacturing plant policy through service "Manufacture Plant 1".
Figure 1. OMPTS process workflow
In OTMPS, you need to add a compensation service to the order process micro-flow, specifically to Manufacture Plant 1 service. This compensation service sends an e-mail to the manufacturing plant administrator to inform him or her to void the previous order.
To add a compensation service to an activity, follow these steps:
- Create an Interface for the compensation service.
- Implement the Compensation service.
- Add a Partner Link for the compensation service.
- Specify Compensation for the forward service.
- Specify a Compensation Sphere attribute for the process.
Let's break these down with more detail:
Step 1: Create an interface for the compensation service
A compensation service is similar to a forwarding service. If it doesn't exist, you need to create a service interface for your compensation service. To define an interface, you can either directly use the Web Services Description Language (WSDL) wizard, or build it from a Java™ class using IBM® WebSphere® Studio Application Developer Integration Edition tooling.
You can define an input message for the compensation service in the same way that you would for a forward service. The information returned by the compensation service and its output message does not have any effect on the process, and is therefore discarded by the Business Process Choreographer (the BPEL workflow engine). Please refer to How is the compensation service input message assembled? for more information.
In our OTMPS scenario, you need to create a service interface for the compensation service of Manufacture Plant 1 service. The name of the interface file is OrderCompensation.wsdl, as shown in Figure 2 below. This interface has one operation called "revokeOrder". The operation has no output message. The input message of the compensation service provides the information the service needs to send the e-mail. This information is available in the input message "ManufactureIn" for the forward service. So, set the input message of the compensation service to the input message of the forward service.
Figure 2. Interface for the compensation service
Step 2: Implement the compensation service
In this step, you'll use WebSphere Studio Application Developer Integration Edition to generate the necessary service skeleton for the service interface you created in the last step.
- Right-click on the Service Interface file.
- Choose menu item Build from service.
- In the New Service Skeleton window, choose the Service Type.
In our example, we implemented the compensation service as a Java service. So, you need to choose "Java Service Skeleton". From this step, the Java class "OrderCompensation" is generated. Listing 1 shows the generated Java class.
Listing 1. Java service skeleton for the compensation service
public class OrderCompensation {
/**
* @generated
*/
public OrderCompensation() {
// user code begin {constructor_content}
// user code end
}
/**
* revokeOrder
* @generated
*/
public void revokeOrder(Services.ManufactureIn argServiceRequestPart)
{
// user code begin {method_content}
// user code end
}
}
|
Between "user code begin" and "user code end", add the code to revoke the order. This implementation could invoke functions to update the database, or it could execute other business processes to compensate the previous execution. In our example, there is no direct service you can call to revoke the submitted orders. The implementation is sending an e-mail to the manufacturing plant administrator to inform him or her to void the previous order.
Step 3: Add a Partner Link for the compensation service
You need a Partner Link to add the compensation service into the process. So, add Partner Link RevokeOrder in the process. Figure 3 shows the implementation screen of the Partner Link.
Figure 3. Add a Partner Link
Step 4: Specify compensation for the forward service
Next, you add the compensation service to Manufacture Plant 1 service. Follow these steps:
- Select Manufacture Plant 1 activity.
- Click the Compensation Property tab.
- Set Partner Link, Port Type, and Operation to RevokeOrder, OrderCompensation, and revokeOrder, respectively.
- The data for compensation operation revokeOrder is already available in the input message of the forward service. So, you use the input variable ManufacturingLoopManufacturePlant1InputCriteriaVariable of the forward service for the compensation service.
The Manufacture Plant 1 service activity doesn't take part in the transaction. As shown in Figure 4 below, the property "Activity Takes Part in Transaction" should be unchecked. In a micro-flow, an activity that takes part in a transaction is rolled back if the transaction fails. In such cases, there is no need for compensation service activities inside the transaction. Only activities that are not part of a transaction need the compensation service, for example, sending e-mails. Other types of activities that need compensation are those that invoke services on remote servers that do not support distributed transactions. In our case, the workflow invokes the remote manufacturing plant service through the protocols, such as FTP or MQ. This activity doesn't participate in the global transaction.
Figure 4. Specify the compensation service
Step 5: Specify a Compensation Sphere attribute for the process
In order to trigger the compensation, a Compensation Sphere must be available. When a business process is started, the scope for the set of compensable activities performed by the business process is set up. The defined scope is called the Compensation Sphere. As shown in Figure 5 below, you can specify the settings for Compensation Sphere on the property sheet of the process in the Server settings.
Figure 5. Compensation Sphere settings
The possible choices for micro-flow are "Supports" and "Required". If the Compensation Sphere setting is Supports, it will use the parent process's Compensation Sphere. If it is the top process, the compensation service is disabled. So in order to enable compensation service, you have to use the setting Required. In this case, if there is no Compensation Sphere, a new one is created.
Once an activity completes successfully, its compensation service becomes available to be executed. You can invoke compensation explicitly or implicitly. Implicit compensation is triggered when a fault is thrown, and the fault is either not caught or caught in a fault handler that is defined for the top-level process.
Explicit invocation might occur anywhere. You can explicitly terminate the process instance with a request for compensation using the Process Choreographer API (application program interface). For information about Process Choreographer API, please refer to WebSphere Application Server API specifications under package com.ibm.bpe.api. A macro-flow's compensation service can be triggered either explicitly or implicitly, while a micro-flow's compensation service can only be triggered implicitly.
For example, "OrderProcessSystem" process is a micro-flow in the OTMPS scenario. When an exception happens, the whole workflow fails, and the transaction rolls back. Since Compensation Sphere is enabled, all the successfully completed activities, which are not part of the global transaction, will be compensated. As described earlier, the Manufacture Plant 1 activity is not part of the global transaction, and therefore the operation revokeOrder defined in compensation service OrderCompensation will be invoked. This compensation service is programmed to send an e-mail to the manufacturing plant administrator to inform him or her to void the previous order.
This article described the compensation service in Process Choreographer and its usage in the OTMPS scenario. It also illustrated how to add compensation in a workflow and how to trigger compensation services.
The authors wish to thank Joshy Joseph and Susan Herrmann for their assistance with this article.
- Read all parts of the On demand business process life cycle, and keep track as we add new installments.
- Learn more about compensation with the following articles and resources:
- Business process choreography in WebSphere: Combining the power of BPEL and J2EE, (IBM Systems Journal, 2004).
- Websphere Application Server Information Center.
- Modeling compensation in WebSphere Business Integration Server Foundation Process Choreographer, (IBM WebSphere Developer Technical Journal, 2004).
- Create compensation in a business process, (developerWorks tutorial, June 2004).
- How is the compensation service input message assembled? (IBM Support, Technote, 2004).
- Browse for books on these and other technical topics.
- Get involved in the developerWorks community by participating in
developerWorks blogs.
- Want more? The developerWorks SOA and Web services zone hosts hundreds of informative articles and introductory, intermediate, and advanced tutorials on how to develop Web services applications.
Wei Liu is a Software Engineer from IBM Software Group. She is currently working with the IBM On Demand development team. Her area of expertise includes WebSphere Application Server, Web Services, and workflow development. You can contact her at liuwei@us.ibm.com.
Dr. Germán Goldszmidt is a Senior Technical Staff Member working in IBM Software Group, with focus on architecture of an integrated platform for delivering, customizing, and deploying on demand solutions. Previously, he was a researcher at the IBM T.J. Watson Research Center, and led the design and implementation of several technologies, including Oceano, the first prototype autonomic computing eUtility, and Network Dispatcher, the load balancer component of WebSphere. You can contact him at gsg@us.ibm.com.




