×

This example describes how to deploy a Java application by using devfile and connect it to a database service.

Prerequisites
  • A running cluster.

  • odo is installed.

  • A Service Binding Operator is installed in your cluster. To learn how to install Operators, contact your cluster administrator or see Installing Operators from OperatorHub.

  • A Dev4Devs PostgreSQL Operator Operator is installed in your cluster. To learn how to install Operators, contact your cluster administrator or see Installing Operators from OperatorHub.

Creating a project

Create a project to keep your source code, tests, and libraries organized in a separate single unit.

Procedure
  1. Log in to an OpenShift Container Platform cluster:

    $ odo login -u developer -p developer
  2. Create a project:

    $ odo project create myproject
    Example output
     ✓  Project 'myproject' is ready for use
     ✓  New project created and now using project : myproject

Creating a Java MicroServices JPA application

With odo, you can create and manage a sample Java MicroServices JPA application.

Procedure
  1. Clone the sample application:

    $ git clone -b jpa-sample https://github.com/redhat-developer/application-stack-samples.git
  2. Navigate to the application directory:

    $ cd ./application-stack-samples/jpa
  3. Initialize the project:

    $ odo create java-openliberty java-application
  4. Push the application to the cluster:

    $ odo push

    The application is now deployed to the cluster.

  5. View the status of the cluster by streaming the OpenShift Container Platform logs to the terminal:

    $ odo log

    Notice the test failures and UnknownDatabaseHostException error. This is because your application does not have a database yet:

    [INFO] [err] java.net.UnknownHostException: ${DATABASE_CLUSTERIP}
    [INFO] [err]    at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:220)
    [INFO] [err]    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:403)
    [INFO] [err]    at java.base/java.net.Socket.connect(Socket.java:609)
    [INFO] [err]    at org.postgresql.core.PGStream.<init>(PGStream.java:68)
    [INFO] [err]    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:144)
    [INFO] [err]    ... 86 more
    [ERROR] Tests run: 2, Failures: 1, Errors: 1, Skipped: 0, Time elapsed: 0.706 s <<< FAILURE! - in org.example.app.it.DatabaseIT
    [ERROR] testGetAllPeople  Time elapsed: 0.33 s  <<< FAILURE!
    org.opentest4j.AssertionFailedError: Expected at least 2 people to be registered, but there were only: [] ==> expected: <true> but was: <false>
            at org.example.app.it.DatabaseIT.testGetAllPeople(DatabaseIT.java:57)
    
    [ERROR] testGetPerson  Time elapsed: 0.047 s  <<< ERROR!
    java.lang.NullPointerException
            at org.example.app.it.DatabaseIT.testGetPerson(DatabaseIT.java:41)
    
    [INFO]
    [INFO] Results:
    [INFO]
    [ERROR] Failures:
    [ERROR]   DatabaseIT.testGetAllPeople:57 Expected at least 2 people to be registered, but there were only: [] ==> expected: <true> but was: <false>
    [ERROR] Errors:
    [ERROR]   DatabaseIT.testGetPerson:41 NullPointer
    [INFO]
    [ERROR] Tests run: 2, Failures: 1, Errors: 1, Skipped: 0
    [INFO]
    [ERROR] Integration tests failed: There are test failures.
  6. Create an ingress URL to access the application:

    $ odo url create --port 8080
  7. Push the changes to your cluster:

    $ odo push
  8. Display the created URL:

    $ odo url list
    Example output
    Found the following URLs for component mysboproj
    NAME               STATE      URL                                           PORT     SECURE     KIND
    java-application-8080     Pushed     http://java-application-8080.apps-crc.testing     8080      false      ingress

    The application is now deployed to the cluster and you can access it by using the URL that is created.

  9. Use the URL to navigate to the CreatePerson.xhtml data entry page and enter a username and age by using the form. Click Save.

    Note that you cannot see the data by clicking the View Persons Record List link since your application does not have a database connected yet.

Creating a database with odo

To create a database, you must have an access to the database Operator. For this example, Dev4Devs PostgreSQL Operator is used.

Procedure
  1. View the list of the services in your project:

    $ odo catalog list services
    Example output
    Operators available in the cluster
    NAME                                             CRDs
    postgresql-operator.v0.1.1                       Backup, Database
  2. Store the YAML of the service in a file:

    $ odo service create postgresql-operator.v0.1.1/Database --dry-run > db.yaml
  3. Add the following values under the metadata: section in the db.yaml file:

      name: sampledatabase
      annotations:
        service.binding/db.name: 'path={.spec.databaseName}'
        service.binding/db.password: 'path={.spec.databasePassword}'
        service.binding/db.user: 'path={.spec.databaseUser}'

    This configuration ensures that when a database service is started, appropriate annotations are added to it. Annotations help the Service Binding Operator in injecting the values for databaseName, databasePassword, and databaseUser into the application.

  4. Change the following values under the spec: section of the YAML file:

      databaseName: "sampledb"
      databasePassword: "samplepwd"
      databaseUser: "sampleuser"
  5. Create a database from the YAML file:

    $ odo service create --from-file db.yaml

    A database instance is now present in your project.

Connecting a Java application to a database

To connect your Java application to the database, use the odo link command.

Procedure
  1. Display the list of services:

    $ odo service list
    Example output
    NAME                        AGE
    Database/sampledatabase     6m31s
  2. Connect the database to your application:

    $ odo link Database/sampledatabase
  3. Push the changes to your cluster:

    $ odo push

    After the link has been created and pushed, a secret that contains the database connection data is created.

  4. Check the component for values injected from the database service:

    $ odo exec -- bash -c 'env | grep DATABASE'
    declare -x DATABASE_CLUSTERIP="10.106.182.173"
    declare -x DATABASE_DB_NAME="sampledb"
    declare -x DATABASE_DB_PASSWORD="samplepwd"
    declare -x DATABASE_DB_USER="sampleuser"
  5. Open the URL of your Java application and navigate to the CreatePerson.xhtml data entry page. Enter a username and age by using the form. Click Save.

    Note that now you can see the data in the database by clicking the View Persons Record List link.

    You can also use a CLI tool such as psql to manipulate the database.