Develop with SAP Cloud SDK for Java

Objectives

After completing this lesson, you will be able to:

  • Identify the developer tools required to build Java applications in SAP Cloud SDK.
  • Describe basic concepts to develop with the SAP Cloud SDK for Java.

SAP Cloud SDK developer tools

A minimal set of prerequisites must be installed on our developer machine to build applications using the SAP Cloud SDK.

Introducing Apache TomEE

Apache TomEE (pronounced "Tommy") is the Java Enterprise Edition of Apache Tomcat (Tomcat + Java EE = TomEE) that combines several Java enterprise projects including Apache OpenEJB, Apache OpenWebBeans, Apache OpenJPA, Apache MyFaces, and others. In October 2011, the project obtained certification by Oracle Corporation as a compatible implementation of the Java EE 6 Web Profile.

You can find detailed information about Apache TomEE at https://tomee.apache.org.

Introducing the SAP buildpack for Java

SAP provides an enhanced buildpack for Java. The SAP buildpack provides additional containers for TomEE and TomEE7.

The standard Cloud Foundry buildpack for Java is called java_buildpack. This buildpack provides a container for Tomcat. You can find detailed information at: https://github.com/cloudfoundry/java-buildpack

SAP extended the standard buildpack and created a new one called sap_java_buildpack. This buildpack provides containers for Tomcat, TomEE, TomEE 7. You can find detailed information at https://help.sap.com/viewer/product/BTP then navigate to DevelopDevelop ApplicationsDevelopment in the Cloud Foundry EnvironmentDeveloping Applications and ServicesDevelopment LanguagesDeveloping Java in the Cloud Foundry Environment(link).

Introducing Apache Maven

Maven is a build automation tool used primarily for Java projects. The Maven project is hosted by the Apache Software Foundation.

Maven addresses two aspects of building software: how software is built, and its dependencies.

Unlike earlier tools like Apache Ant, Maven uses conventions for the build procedure, and only exceptions need to be written down. An XML file (named pom.xml) describes the software project being built, its dependencies on other external modules and components, the build order, directories, and required plug-ins. It comes with pre-defined targets for performing certain well-defined tasks such as compilation of code and its packaging.

Maven dynamically downloads Java libraries and Maven plug-ins from one or more repositories, such as, the Maven 2 Central Repository, and stores them in a local cache. This local cache of downloaded artifacts can also be updated with artifacts created by local projects. Public repositories can also be updated.

The basic Maven command you'll use to build a Java application is:

Code Snippet
Copy code
Switch to dark mode
1
mvn clean install -U

Further information to use Maven can be found in the official web site at https://maven.apache.org.

Deploying your application to Cloud Foundry

The project is deployed to Cloud Foundry based on the deployment information specified in the manifest.yml file by executing cf push. For example, given the information specified in the figure:

  1. The "java-hello.war" file is uploaded to Cloud Foundry.
  2. Based on the given buildpack, a proper runtime environment is prepared for the application (the application is "staged").
  3. The application is created and started based on provided name, memory, route, environment variables, and so on.

Further information about the Cloud Foundry push command is available at: https://docs.cloudfoundry.org/devguide/push.html

Introducing Spring

The Spring Framework is an open source application framework for the Java platform.

It provides comrehensive infrastructure support for developing Java applications.

It also handles the infrastructure so that the developers can focus on the application.

It is developed by Pivotal Software and distributed under the open source Apache Licence 2.0.

Spring Boot is used to build stand-alone and production-ready spring applications. In particular, Spring Boot Web applications are recognized by common development environments and run with one click, helping the developer to be more productive.

Additional information about Spring is available at http://spring.io.

Remote debugging of a Java application

Debug with Visual Studio Code

Let's examine debugging using the Visual Studio Code debugger.

To debug using Visual Studio Code debugger, complete the following steps:

  1. Choose RunOpen ConfigurationsJava.
  2. Enter the following configuration:
    Code Snippet
    Copy code
    Switch to dark mode
    12345678910
    { "type": "java", "name": "Attach Java Debugger", "request": "attach", "hostName": "localhost", "port": 5005, "sourcePaths": [ "${workspaceFolder}" ] }
  3. Switch to the debugger by choosing the proper icon.
  4. Choose Attach Java Debugger from the drop-down control.
  5. Choose Start Debugging.

Debug with IntelliJ IDEA

Let us move on to debugging using the IntelliJ IDEA debugger.

To debug using IntelliJ IDEA debugger, complete the following steps:

  1. Choose RunEdit Configurations.
  2. In the Run/Debug Configurations window, add a new configuration based on the following information:
    • Name: Attach Java Debugger
    • Debugger mode: Attach to remote JVM
    • Host: localhost
    • Port: 5005
    • User module classpath: << your root project folder>>
  3. Choose Attach Java Debugger from the drop-down control.
  4. Choose Debug 'Attach Java Debugger'.

Remote Debugging of Applications Running in Cloud Foundry

We wish to debug the application running in Cloud Foundry using the debugger installed on our local computer, for example, Eclipse, IntelliJ IDEA, or Visual Studio Code.

Use the following steps:

  1. Enable the Java application for debug by executing the following:
    Code Snippet
    Copy code
    Switch to dark mode
    1
    cf set-env <<application name>> JBP_CONFIG_JAVA_OPTS "[java_opts: '-Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=n']"
  2. Enable the Cloud Foundry application for debug by executing the following:
    Code Snippet
    Copy code
    Switch to dark mode
    12
    cf enable-ssh <<application name>> cf restage <<application name>>
  3. Establish an SSH connection by executing the following:
    Code Snippet
    Copy code
    Switch to dark mode
    1
    cf ssh -N -T -L <<localport>>:localhost:8000 <<application name>>
  4. Attach the Debugger.

Log in to track your progress & complete quizzes