Implementing caching in SAP Cloud SDK application
In this exercise, you will learn how to implement caching 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
- 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
Enable caching in the application you created in the previous exercise.
Checkout the initial Git branch
javaspring_v3.0_caching
. To do this, open a Terminal and run the following commands, make sure current working directory is /home/user/projects/cloud-sdk-learning-journey.:
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
Start the Mock Server
In the Terminal, 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
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
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 caching to your application. https://developers.sap.com/tutorials/s4sdk-caching.htmlCode walk-through
File name Comments on code changes GetBusinessPartnersCommand.java Code logic is as follows...
- Determine how long objects need to be cached. In our example, we will cache the objects for 30 seconds
- Declare parameters that need to be stored together with cached data. In our example, we are building the cache without any parameters
pom.xml Code logic is as follows...
- Use the JCache adapter Caffeine, but you can use any implementation you like
- Add the Caffeine dependency to your application
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 Implementing resilience in a Java Spring application exercise.Create or update the following files in the table below
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
.