Implementing resilience in SAP Cloud SDK application
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 Pay-As-You-Go Account in SAP BTP
- Run the Mock Server on port 8081
- 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
Enable resilience in the application you created in the previous exercise.
Checkout the initial Git branch
javaspring_v2.0_resilience
. To do this, open a Terminal and run the following commands, make sure current working directory is /home/user/projects/cloud-sdk-learning-journey.:Code snippetExpandcd ~/projects/cloud-sdk-learning-journey git checkout -f javaspring_v2.0_resilience
Build and run the project.
Build the application by running the following commands in the terminal
Open the
Application.java
file. Press F5 to run the application
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
Verify that an empty list of business partners is returned due to resilience configuration
Optional: Review code in the following source files
Note
Check out the exercise on SAP Tutorial Navigator on Introducing resilience to your application. https://developers.sap.com/tutorials/s4sdk-resilience.htmlCode walk-through
File name Comments on code changes BusinessPartnerController.java Code logic is as follows...
- Annotate the method getBusinessPartners with @RequestMapping to handle the GET method
- Create a variable named destination of type HttpDestination with information about the Mock Server or API Business Hub
- Create a new instance of the utility class GetBusinessPartnersCommand and pass the destination as the constructor argument
- Call the execute method of the newly created instance to retrieve the list of Business Partners
- Return the list of business partners as part of the GET request
GetBusinessPartnersCommand.java Code logic is as follows...
- The constructor takes the destination as one of the arguments. It then calls the overloaded constructor passing the destination and an instance of the DefaultBusinessPartnerService class
- An instance of ResilienceConfiguration of type BusinessPartnerSrvice is created. SAP Cloud SDK comes with a default resilience configuration
- In our example, isolation mode is set to optional tenant + user, the bulkhead maximum concurrent calls is set to 20 and the execution timeout is set to 10000 milliseconds
- The executeSupplier method of ResilienceDecorator supports an optional 3rd parameter as a fallback function. In our example, a lambda function that returns an empty list is used
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
Optional: Manually create the project. The following steps create an exact replica of the project you just checked out using Git.
Note
Before you execute this step, you must execute the corresponding optional step, described in the previous Querying SAP APIs in a Java Spring application exercise.Create or update the following files in the table below
File name Type Contents BusinessPartnerController.java Path: ~/projects/bpstandardvdm/application/src/main/java/com/sap/cloud/sdk/tutorial/controllers
Modified Copy contents from here...
GetBusinessPartnersCommand.java Path: ~/projects/bpstandardvdm/application/src/main/java/com/sap/cloud/sdk/tutorial/utils
New Copy contents from here...
Build and run the project in
projects/bpstandardvdm
in the same exact way you did in the previous steps with project inprojects/cloud-sdk-learning-journey/bpstandardvdm
.