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

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:

  1. 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.
  2. 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 snippet
    cd ~/projects
    git clone https://github.com/SAP-samples/cloud-sdk-learning-journey.git
    Expand
  3. Choose FileOpen Folder and open the /home/user/projects/cloud-sdk-learning-journey/ folder.

Steps

  1. Enable caching in the application you created in the previous exercise.

    1. Checkout the javaspring_v3.1_caching Git branch by running the following commands in the terminal:

      Code snippet
      cd ~/projects/cloud-sdk-learning-journey
      git checkout -f javaspring_v3.1_caching
      Expand
  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
      Expand
    2. Run the application by running the following commands in the terminal

      Code snippet
      cd application
      mvn spring-boot:run
      Expand
      Note
      As an alternative, to run the application, you can open the Application.java file. Choose RunStart Debugging or press F5.
  3. Make sure that the Mock Server is up and running as described in the related previous exercise.

    1. In case the Mock server is installed but not started, you need to open a new terminal window and run the following commands:

      Code snippet
      cd ~/projects/cloud-s4-sdk-book
      npm run start
      Expand
  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

    9. Re-start the mock server and re-run the query to make sure it works again.

    10. Stop the running application using Ctrl + C in the corresponding terminal window.

    11. Note
      If you are not sure how to stop the mock server, you can run the following command from a new terminal window.
      Code snippet
      
      lsof -i:8081
      kill <node PID>
      Expand
  5. Optional: code walk-through.

    1. Review code in the following source files:

      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
      Note
      Review the exercise on SAP Tutorial Navigator on Introducing caching to your application. https://developers.sap.com/tutorials/s4sdk-caching.html

Log in to track your progress & complete quizzes