how to use event handler and conversations in BW 6.x.

how to use event handler and conversations in BW 6.x.

book

Article ID: KB0081183

calendar_today

Updated On:

Products Versions
TIBCO ActiveMatrix BusinessWorks BW 6x

Description

 

In this Article, I am going to explain the functionality of online shopping service. This service has 3 operations viz. place order, confirm order and cancel order. Since many buyers are placing, confirming and cancelling the orders at same time and many instances of this service can be created at run time to allow load balancing, I am using Event handlers to allow asynchronous event processing.

The Cancel order operation is defined in the event handler. Both the 'place order' and 'confirm order' operations have a correlation key obtained from the response to the operation 'place order'. After placing an order, if the buyer invokes the 'cancel order' operation with the correlation key before confirming the order, the event handler containing the implementation for the 'cancel order' operation is executed in parallel to the main process. The engine does not wait for the 'confirm order' operation to be executed.

Note: I have attached the project for your reference, please refer activities for their configuration details

Step-by-step guide:

Step 1: Create new project of shopping service.

Step 2: Create service WSDL as below,


 

Step 3: Create process and add the scope in process. Once done, implement the place order and confirm order operations in created scope,

PlaceOrderIn will receive item name and item id as request. As a response, it will generate the unique order ID, update the cart status and PlaceOrderOut will send those details back to buyer. I have used random function to generate order id and static value for cart status.

For ConfirmOrderIn, the create instance box is not checked i.e. It is not an independent operation. This operation will receive the checkout confirmation and order ID from client. Once done, the order will be confirmed and shipment ID will be generated. This shipment ID will be sent back to buyer via ConfirmOrderOut activity. I have used random number to generate shipment ID.

Note here that PlaceOrder and ConfirmOrder are implemented in syncronous way.

 

Step 4: To establish the correlation between place and confirm operation, I am using conversation between these operations , click on PlaceOrderOut activity > Conversation tab and create new conversation. In newly created conversation, map the value of generated order id.

 

Step 5: Similarly, select ConfirmOrderIn. Instead of initiating new conversation, this activity will participate in one which is initiated by PlaceOrderOut. Map the received order ID i.e. C_OrderID in conversation key

This will establish the link between these operations. Also, it will ensure that confirm operation will confirm only order if received order ID matches with conversation key (i.e. Order id that was sent via PlaceOrderOut)


 

Step 6: In this implementation, there is no way to place the cancel order operation since the flow shown above is synchronous and cannot be interrupted once started. Therefore, I have kept the cancel operation in event handler.

Create event handler for scope and select the OnEvent scope > conversation tab. Select join option as we want to have correlation between all implemented operation. Here, CancellationOrderID will be the conversation key.


Step 7: Refer the attached project and configure all rest of the activities. Ensure there are no validation errors in project.

In this service, generated OrderID is playing crucial role as confirmOrder and cancelOrder requires that as an input and that should match with their conversation key. Create HTTP connector resource, SOAP binding and generate concrete WSDL for clients. Service configuration is completed.

Step 8: Create new application as SOAP client and import the WSDL generated from step 7 above.

Step 9: Implement the operation as shown below,

In the client application, all three operations are implemented. Timer will be triggered after every 10 seconds and will be turned off after 10 attempts. This is to test that client application i.e. Buyer should place 10 order after interval of 10 seconds.

I have used hard coded values for item name and ID. InvokePlaceOrder will invoke the PlaceOrder operation and will get the order id and cart status from service. Once done, sleep will be executed and client application will wait for 7 seconds.

After sleep there is condition on transition. If generated random number is greater than 5, order will be confirmed else will be cancelled.

Based on the result, either confirm or cancel operation will be called.Cancel operation will need the CancellationOrderID to cancel the order whereas Confirm operation will need confirmation and order id for order confirmation and shipment ID generation.

Please refer activities in attached project and complete the rest of the configuration for logger and other activities. Client application configuration is completed. Ensure that there are no validation errors. Once you execute both applications in studio, you should see the logs in console as below,
 

++++++++++++
Ser_NewOrder - New order for Item ID: SHIRT123 & Name: Peter england formal shirt is being placed...

Cli_PlaceResponse - Order has been placed. Order id:- ORDER383 & item status is:- Added in cart

Ser_CancelOrder - Order ORDER383 is being cancelled...

ServiceFinished - ============Service execution finished============

Cli_CancelResponse - Order ORDER383 has been cancelled

Cli_ClientFinished - =============Client execution finished============

Ser_NewOrder - New order for Item ID: SHIRT123 & Name: Peter england formal shirt is being placed...

Cli_PlaceResponse - Order has been placed. Order id:- ORDER170 & item status is:- Added in cart

Ser_ConfirmOrder - Order ORDER170 is being confirmed...

ServiceFinished - ============Service execution finished============

Cli_ConfirmResponse - Order ORDER170 has been confirmed. Shipment ID is: SHIP802

Cli_ClientFinished - =============Client execution finished============
++++++++++++

Sample project is attached for your ready reference. 

Issue/Introduction

Article explains how to use event handler and conversations in BW 6.x.

Attachments

how to use event handler and conversations in BW 6.x. get_app