In this exercise, you will learn how to implement resilience in SAP Cloud SDK application. We will use the Mock Server instead of the actual SAP S/4HANA Cloud.
Prerequisites
For the complete execution of current exercise, you must execute the following activities first, using SAP Business Application Studio:
- Execute the following exercises:
- Creating your free trial account in SAP BTP
- Setting up the mock server, so that you have the Mock server program available in the
cloud-s4-sdk-book
folder.
- Run the following commands in a terminal, in case you didn't do it already in previous exercises, to clone the Git repository for the course:Code snippetExpand
cd ~/projects git clone https://github.com/SAP-samples/cloud-sdk-learning-journey.git
- Choose File → Open Folder and open the
/home/user/projects/cloud-sdk-learning-journey/
folder.
Steps
Checkout the branch tsnestjs_v2.0_resilience in SAP Business Application Studio
Open SAP Business Application Studio.
Run the following commands in the terminal.
Build and run the project
Run the application by executing the following commands in the terminal
Make sure that the Mock Server is up and running as described in the related previous exercise.
In case the Mock server is installed but not started, you need to open a new terminal window and run the following commands:
Query the Business Partner API of SAP S/4HANA Cloud
Open the requests.http file
Click on Send Request in the requests.http file to submit the GET request for business partners
Verify that the list of business partners are returned from the Mock Server. Note: If the Mock Server is not running, make sure to run the Mock Server on port 8081 by following the prerequisites at the top
Stop the Mock Server by pressing Ctrl + C in the Mock Server project terminal
Click on Send Request in the requests.http file to submit the GET request for business partners within 30 seconds.
Verify that the list of business partners are returned from the cache, since the Mock Server is not running
Click on Send Request in the requests.http file to submit the GET request for business partners after 30 seconds. The Business Partners are not returned anymore.
Re-start the mock server and re-run the query to make sure it works again.
Stop the running application using Ctrl + C in the corresponding terminal window.
Optional: code walk-through.
Review code in the following source files:
File name Comments on code changes business-partner.service.ts Code logic is as follows...
- Import resilience from the @sap-cloud-sdk/resilience module
- Use the middleware function to build a resilience middleware. In our example, we will use the following values
- timeout: 30 seconds
- circuitBreaker: true
- retry: true
package.json New SAP Cloud SDK modules have been installed for the project requests.http Provides various HTTP requests that can be submitted to query the Business Partner API commands.txt Provides various commands that can be run on the terminal Note
Check out the SAP Cloud SDK documentation on Executing an OData GET request using SAP Cloud SDK for JavaScript. https://sap.github.io/cloud-sdk/docs/js/tutorials/getting-started/execute-an-odata-request
Optional: Manually modify the project in
~/projects/cloudsdk_bp_ts
to use resilience.Open the
projects/cloudsdk_bp_ts
folder in the workspace, by choosing File → Open Folder....Create or update the following files in the table below
File name Type Contents business-partner.service.ts Path: src/business-partner
Modified Copy contents from here...
Build and run the project in
projects/cloudsdk_bp_ts
:Stop the running application using Ctrl + C in the corresponding terminal window.