Realization of a sync/async bridge in SAP CPI

From SAP PI/PO we already know the case of a sync/async bridge. Here I try to show a way how this can be realized with the SAP CPI (Integration Suite).

A sync/async bridge is used to link a synchronous sender with an asynchronously communicating recipient. The challenge is to keep the incoming connection open until the target system sends the response.

Flow%20of%20Sync/Async%20Bridge

Flow of Sync/Async Bridge

The very first thing I’ll show here is our target image to get an overview. On the top left (Sync: Entry) we receive the synchronous message from the sender. Then we forward this request via a Process Call to the bottom left (Async: Request). We then start our wait step as a loop at the top right (Sync: Wait for Response). There we try to read the response from the datastore as soon as it is available. If no response is available, the process is repeated. At the bottom right (Async: Response) we accept all incoming responses and write them to the datastore. After the response is received, it is automatically transferred to the sender and the process is finished.

Flow%20how%20it%20should%20look%20in%20the%20end

Flow how it should look in the end

Sync: Entry

Here we only define the input for the synchronous sender and link the “Async: Request” via a “Process Call” and the “Sync: Wait for Response” via a “Looping Process Call”.

Sync%3A%20Entry%20Elements

Sync: Entry Elements

For the Looping Call we set the following settings:

Looping%20Process%20Call%20-%20Settings

Looping Process Call – Settings

Hint: The “Max. Number of Iterations” must be multiplied by our sleep step of 1 second. So the flow will iterate for 3 minutes until it stops with an exception. The maximum should be 5 minutes.

Sync: Wait for Response

This “local integration process” is executed repetitively by a loop. With each iteration, an attempt is made to read the corresponding response message of the asynchronous system from the DateStore. For this we use the MessageId as reference.
Then we check, in the “Router”, whether an entry was found in the DataStore. If no entry is found, a Groovy script executes a sleep of 1 second, because there is currently no other way to configure the waiting time between the iterations.

Sync%3A%20Wait%20for%20Response%20Elements

Sync: Wait for Response Elements

Settings%20for%20Find%20Response%20Message

Settings for “Find Response Message”

Settings%20for%20Router

Settings for “Router”

/* Function: Sleep for 1 Second Author: Robert Fels / Contiva GmbH / www.contiva.com / robert.fels@contiva.com Version 1.0
*/
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
def Message processData(Message message) { sleep(1000); return message;
}

Code for “Wait for 1sec”

Async: Request

This “Local Integration Process” is used to send the request message to the asynchronous system. Before the message is sent, we insert a reference so that the response message can later be assigned to the current process. For this we use the ${header.SAP_MessageProcessingLogID}. This is automatically generated by SAP for each incoming synchronous message and is unique. Of course you can also transfer it via a specific mapping.

Async%3A%20Request%20Elements

Async: Request Elements

Setting%20of%20Set%20Reference

Setting of “Set Reference”

Async: Response

We catch the Asynchronous Response with an “Integration Process” and not a “Local Integration Process”. This could also theoretically be placed in its own flow and is instantiated specifically when the asynchronous system calls with the response. We take the reference out of the payload and write it into a header value in order to be able to set the “EntryID” when writing to the DataStore.

Async%3A%20Response%20Elements

Async: Response Elements

Settings%20of%20Get%20Reference

Settings of “Get Reference”

Settings%20of%20Write%20Response

Settings of “Write Response”

I hope this helps you to successfully build a sync/async bridge in the SAP CPI. Currently I currently don’t see any better or other way to implement it.

It would be nice if we could configure the waiting time between the iterations already in the loop call instead of doing this via a Groovy script, but unfortunately this is currently not implemented and possible.

You should also think about an exception handling in case of an timeout on the synchronous side. Then maybe the asynchronous system should be informed about a rollback.

I am glad if you follow me on SAP People (Follow me). If you have any ideas, suggestions or questions, please feel free to contact me in the comments.

Be also sure to check out the other posts and follow the SAP Integration Suite Environment topic page, ask and answer questions or read other posts on the topic.

Cheers

Robert Fels