Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

On demand business process life cycle, Part 12: Implement a compensation service

Wei Liu (liuwei@us.ibm.com), Software Engineer, IBM
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.
Germán Goldszmidt (gsg@us.ibm.com), Senior Technical Staff Member, IBM
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.

Summary:  Learn how to implement a Business Process Execution Language (BPEL) workflow with a process compensation service. Wei Liu and Dr. Germán Goldszmidt describe how to create and invoke a compensation activity to undo the updates made by a process activity. Part 1 of this series introduced an on demand business process scenario for an Order to Manufacturing Processing System (OTMPS). This article expands upon what you've learned in that initial article, as well as remaining parts, and describes how to extend the workflow with compensation services.

Date:  06 Jul 2005
Level:  Advanced

Activity:  2993 views
Comments:  

Compensation service

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.

How compensation works

When a service activity completes, the compensation service and its input data is registered with the Compensation Sphere of the workflow if a compensation service has been defined. At the end of the process instance, the Compensation Sphere checks to see if a fault has been thrown. If the process completes successfully, then there is no need to compensate any of the activities. On the other hand, if the process fails, you need to perform the compensation. If any process compensation is needed, the Compensation Sphere invokes its registered compensation services in a last-in first-out sequence (in reverse order to when the forward services completed).

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
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.


Adding compensation service

To add a compensation service to an activity, follow these steps:

  1. Create an Interface for the compensation service.
  2. Implement the Compensation service.
  3. Add a Partner Link for the compensation service.
  4. Specify Compensation for the forward service.
  5. 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
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
Figure3. Add 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
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
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.


How to trigger compensation

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.


Conclusion

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.


Acknowledgement

The authors wish to thank Joshy Joseph and Susan Herrmann for their assistance with this article.


Resources

About the authors

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.

Report abuse help

Report abuse

Thank you. This entry has been flagged for moderator attention.


Report abuse help

Report abuse

Report abuse submission failed. Please try again later.


developerWorks: Sign in


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

Choose your display name

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


Rate this article

Comments

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=SOA and Web services
ArticleID=88106
ArticleTitle=On demand business process life cycle, Part 12: Implement a compensation service
publish-date=07062005
author1-email=liuwei@us.ibm.com
author1-email-cc=flanders@us.ibm.com
author2-email=gsg@us.ibm.com
author2-email-cc=flanders@us.ibm.com

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

For articles in technology zones (such as Java technology, Linux, Open source, XML), Popular tags shows the top tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), Popular tags shows the top tags for just that product zone.

For articles in technology zones (such as Java technology, Linux, Open source, XML), My tags shows your tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), My tags shows your tags for just that product zone.

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Try IBM PureSystems. No charge.

Special offers