Exercise: Implementing Resilience in a Java Spring Application

Objective

After completing this lesson, you will be able to Implementing resilience in a Java Spring application.

Implementing resilience in a Java Spring application

In this exercise, you will learn how to implement resilience in SAP Cloud SDK application. We will use the Mock Server to simulate the Business Partners service of SAP S/4HANA Cloud, public edition.

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
    Copy code
    Switch to dark mode
    12
    cd ~/projects git clone https://github.com/SAP-samples/cloud-sdk-learning-journey.git
  3. Choose FileOpen Folder and open the /home/user/projects/cloud-sdk-learning-journey/ folder.

Steps

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

    1. Checkout the javaspring_v2.0_resilience Git branch by running the following commands in the terminal:

      Code Snippet
      Copy code
      Switch to dark mode
      12
      cd ~/projects/cloud-sdk-learning-journey git checkout -f javaspring_v2.1_resilience
  2. Build and run the project.

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

      Code Snippet
      Copy code
      Switch to dark mode
      12
      cd bpstandardvdm mvn clean install -Dmaven.test.skip=true
    2. Run the application by running the following commands in the terminal

      Code Snippet
      Copy code
      Switch to dark mode
      12
      cd application mvn spring-boot:run

      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
      Copy code
      Switch to dark mode
      12
      cd ~/projects/cloud-s4-sdk-book npm run start
  4. Query the Business Partner API of the Mock Server

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

    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

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

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

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

    9. 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
      Copy code
      Switch to dark mode
      123
      lsof -i:8081 kill <node PID>
  5. Optional: code walk-through.

    1. Review code in the following source files:

      File nameComments on code changes
      BusinessPartnerController.java

      Code logic is as follows...

      1. Annotate the method getBusinessPartners with @RequestMapping to handle the GET method
      2. Create a variable named destination of type HttpDestination with information about the Mock Server or API Business Hub
      3. Create a new instance of the utility class GetBusinessPartnersCommand and pass the destination as the constructor argument
      4. Call the execute method of the newly created instance to retrieve the list of Business Partners
      5. Return the list of business partners as part of the GET request
      GetBusinessPartnersCommand.java

      Code logic is as follows...

      1. 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
      2. An instance of ResilienceConfiguration of type BusinessPartnerSrvice is created. SAP Cloud SDK comes with a default resilience configuration
      3. 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
      4. 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.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 resilience to your application. https://developers.sap.com/tutorials/s4sdk-resilience.html

Log in to track your progress & complete quizzes