Oracle by Example brandingManaging Oracle Event Hub Cloud Service - Topic using a Swagger-Generated Java Client

section 0Before You Begin

This tutorial shows you how to generate Java client using REST API and to manage Oracle Event Hub Cloud Service - Topics using this generated Java client. This tutorial takes approximately 15 minutes to complete.

Background

Oracle Event Hub Cloud Service provides a highly available and scalable messaging platform for loading and analyzing data. Oracle Event Hub Cloud Service - Topic instances help users to produce to and consume from streams of data like a messaging system. It also helps store streams of data in a distributed and scalable cluster. Users can access these instances also using REST API. This OBE describes managing instances using REST API.

What Do You Need?

  • Swagger specification for the Oracle Event Hub Cloud Service - Topic. Download from REST API for Oracle Event Hub Cloud Service in Oracle Help Center page.
  • A working Oracle Event Hub Cloud Service - Platform cluster.
  • Oracle Event Hub Cloud Service account login credentials.
  • Java 8 or above installed.
  • Maven 3 or above installed.
  • Eclipse Integrated Development Environment (IDE) that supports Maven installed.
  • Access to editor2.swagger.io web page.
    Note: Swagger editor version 2.X is recommended. Refer github.com/swagger-api/swagger-editor for additional information.

section 1Generate Java Client for the REST API

Perform the following steps to generate Java Client from the editor2.swagger.io web site.

  1. Go to editor.swagger.io website.
  2. From the File menu, select Import File.
  3. In the Upload File dialog box, click Browse, select the required swagger file and then select Open File.

    Note: You would see the swagger code on the left hand side pane and the User Interface on the right hand side pane.

  4. Select Generate Client menu and then select Java. The java client file will be downloaded to the local directory.

    Note: You could generate client program in any of the programming languages that are listed in the menu. We have chosen Java for the purpose of this demo.

  5. Extract the downloaded zip file.

section 2Set up the Project in Eclipse IDE

  1. Open Eclipse IDE. From the File menu, select Import.
    Eclipse IDE File Menu
    Description of the illustration b01.jpg
  2. The Import wizard appears. Expand General, select Projects from Folder or Archive option and click Next.
    Eclipse IDE Import Source Wizard
    Description of the illustration b02.jpg
  3. The Import Projects from File System or Archive window appears. In the Import Source, input the path of the extracted zip file. After you give a valid path, you could see the java-client project is selected. Click Finish.
    Eclipse IDE Import Source Wizard
    Description of the illustration b03.jpg
  4. The java-client project opens up in the Package Explorer. Observe the structure of the project.
  5. Open ApiClient.java file present in io.swagger.client package. Go to the isJsonMime(String mime) method and update the return statement with the below code.
    return mime != null && mime.matches("(?i)application\\/(.*)json(;.*)?");
    
    isJsonMime method
    Description of the illustration b04.jpg

section 3Create an Oracle Event Hub Cloud Service - Topic instance

Perform the following steps to create a Java program that would create an Oracle Event Hub Cloud Service - Topic instance.

  1. Right Click on the java-client project. Expand New and then select Class option.
    new class generate menu
    Description of the illustration z01.jpg
  2. The Java Class creation wizard appears. Enter the Package as mydemo and then enter the Name as CreateInstanceDemo. Click Finish.
    Create a new Java Class Wizard
    Description of the illustration c01.jpg
  3. The CreateInstanceDemo.java file is created and then opens up. Observe the contents.
  4. Copy this snippet of code to the CreateInstanceDemo class.
    CreateInstanceDemo.java program
    Description of the illustration c02.jpg
  5. Update the following variables in the CreateInstanceDemo program with actual values from your Oracle Event Hub Cloud Service instance.
    • path: path Base path of the service URL (e.g. http://localhost/paas/api/v1.1)
    • serviceName: Name of the Topic that you want to create
    • systemName: Name of the Platform cluster in which you want to create the topic
    • X_ID_TENANT_NAME: Identity domain Id for Oracle Event Hub Cloud Service account.
    • authorization: Base64 encoding of the user name and password of the user making the request.
    • identityDomainId: Identity domain Id for Oracle Event Hub Cloud Service account.
  6. Right click on the program. Expand Run As and then select Java Application.
  7. Observe the output in the Console. On successful execution, a jobId will be displayed. You can use this jobId to track the status of this job.
    Class Details {
        message: Submitted job to create service [topic-demo1] in domain [bigdata007].
        jobId: 5293
    }

section 4View the job status of create operation

  1. Right Click on the java-client project. Expand New and then select Class option.
    new class generate menu
    Description of the illustration z01.jpg
  2. The Java Class creation wizard appears. Enter the Package as mydemo and then enter the Name as ViewJobDemo. Click Finish.
    Create a new Java Class Wizard
    Description of the illustration d01.jpg
  3. The ViewJobDemo.java file gets created and then opens up. Observe the contents.
  4. Copy this snippet of code to the ViewJobDemo class.
    ViewJobDemo.java program
    Description of the illustration d02.jpg
  5. Update the following variables in the program with actual values from your Oracle Event Hub Cloud Service instance.
    • path: path Base path of the service URL (e.g. http://localhost/paas/api/v1.1)
    • authorization: Base64 encoding of the user name and password of the user making the request.
    • identityDomainId: Identity domain Id for Oracle Event Hub Cloud Service account.
    • jobId: Job Id of an operation.
  6. Right click on the program. Expand Run As and then select Java Application.
  7. Observe the output in the Console. The status details of the jobId is displayed.
    topic-demo1 : CREATE_SERVICE : SUCCEED

section 5View all the service instances

  1. Right Click on the java-client project. Expand New and then select Class option.
    new class generate menu
    Description of the illustration z01.jpg
  2. The Java Class creation wizard appears. Enter the Package as mydemo and then enter the Name as ViewAllServicesDemo. Click Finish.
    Create a new Java Class Wizard
    Description of the illustration e01.jpg
  3. The ViewAllServicesDemo.java file gets created and then opens up. Observe the contents.
  4. Copy this snippet of code to the ViewAllServicesDemo class.
    ViewAllServicesDemo.java program
    Description of the illustration e02.jpg
  5. Update the following variables in the program with actual values from your Oracle Event Hub Cloud Service instance.
    • path: path Base path of the service URL (e.g. http://localhost/paas/api/v1.1)
    • X_ID_TENANT_NAME: Identity domain Id for Oracle Event Hub Cloud Service account.
    • authorization: Base64 encoding of the user name and password of the user making the request.
    • identityDomainId: Identity domain Id for Oracle Event Hub Cloud Service account.
  6. Right click on the program. Expand Run As and then select Java Application.
  7. Observe the output in the Console. All the services instances in the domain is displayed.
    mydemo
    topic-demo1

more informationWant to Learn More?