Exercise: Implementing caching in a Java Spring application

Objectives
After completing this lesson, you will be able to:

After completing this lesson, you will be able to:

  • Implementing caching in a Java Spring application

Implementing caching in a Java Spring application

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:

  1. Execute the following exercises:
    • Creating your Pay-As-You-Go Account in SAP BTP
    • Run the Mock Server on port 8081
    • Complete the exercise Implementing resilience in SAP Cloud SDK application

Steps

  1. Enable caching in the application you created in the Implementing resilience in SAP Cloud SDK application exercise.

    1. 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.:

      Code snippet
      pwd
      git checkout -f javaspring_v3.0_caching
      Copy code
  2. Build and run the project

    1. Build the application by running the following commands in the terminal

      Code snippet
      cd bpstandardvdm
      mvn clean install -Dmaven.test.skip=true
      Copy code
    2. Open the Application.java file. Press F5 to run the application

  3. Start the Mock Server

    1. In the Terminal, run the following commands

      Code snippet
      cd ~/projects/cloud-s4-sdk-book
      npm run start
      Copy code
  4. Query the Business Partner API of SAP S/4HANA Cloud

    1. Open the requests.http file

    2. Click on Send Request in the requests.http file to submit the GET request for business partners

    3. Verify that the list of business partners are returned from the Mock Server

    4. Stop the Mock Server by pressing Ctrl + C in the Mock Server project terminal

    5. Click on Send Request in the requests.http file to submit the GET request for business partners within 30 seconds

    6. Verify that the list of business partners are returned from the cache, since the Mock Server is not running

    7. Click on Send Request in the requests.http file to submit the GET request for business partners after 30 seconds

    8. Verify that an empty list of business partners is returned due to resilience configuration

  5. Optional: Review code in the following source files

    1. Note
      Check out the exercise on SAP Tutorial Navigator on Introducing caching to your application. https://developers.sap.com/tutorials/s4sdk-caching.html

      Code walk-through

      File nameComments on code changes
      GetBusinessPartnersCommand.java

      Code logic is as follows...

      1. Determine how long objects need to be cached. In our example, we will cache the objects for 30 seconds
      2. 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...

      1. Use the JCache adapter Caffeine, but you can use any implementation you like
      2. Add the Caffeine dependency to your application
      requests.httpProvides various HTTP requests that can be submitted to query the Business Partner API
      commands.txtProvides various commands that can be run on the terminal
  6. Optional: Manually create the project

    1. Create or update the following files in the table below

      File nameTypeContents
      GetBusinessPartnersCommand.java

      Path: bpstandardvdm/application/src/main/java/com/sap/cloud/sdk/tutorial/utils

      Modified

      Copy contents from here...

      pom.xml

      Path: bpstandardvdm/application

      Modified

      Copy contents from here...

Save progress to your learning plan by logging in or creating an account

Login or Register