Java is one of the supported languages that can be used to build a component to run on elastic.io platform. Our Java API can be consulted accessed on java-API GitHub repository.

To help you create a component in Java we have created a simple Petstore component in Java which you can use to get started as well as Java API documentation if you need more details.

Java Component Structure Overview

Here is the structure of the example Hello World component on our GitHub repository:

File Name Type Required Description
logo.png image suggested Logo of your component
component.json
JSON Yes Description of the component structure
gradle/wrapper directory Yes Gradle wrapper directory
build.gradle Gradle required Gradle build file
src/main directory suggested the main component directory

Logo

The file called logo.png should be present in the component. We include a generic hello world logo to be replaced for customisation. If the logo is not included or removed a replacement will be added from our system as a placeholder and most likely will not be adequate for your needs. Therefore, we suggest you add a descriptive logo for your component.

Here are the requirements for the logo file:

  • Do not change the name logo.png (PNG format)
  • The logo should have 64 x 64 pixels dimension

component.json

In component.json we practically describe the main blocks of your component. This configuration file should include the title of your component along with the description and the method of credential verification if any.

It should also include the information and dependencies of the trigger(s) and action(s) that your component has.

At least one trigger or action should be present: Please note that your component.json file should describe at least one trigger or action. This is the bare minimum that you should have.

{
  "title": "Petstore API (Java)",
  "description": "elastic.io component for the Petstore API",
  "docsUrl": "https://github.com/elasticio/petstore-component-java",
  "credentials": {
    "fields": {
      "apiKey": {
        "label": "API key",
        "required": true,
        "viewClass": "TextFieldWithNoteView",
        "note": "Please use <strong>elasticio</strong> as API key. For more details see <a href="https://petstore.elastic.io/docs/" target="_blank">Petstore API docs</a>."
      }
    },
    "verifier": "io.elastic.petstore.ApiKeyVerifier"
  },
  "triggers": {
    "getPetsByStatus": {
      "main": "io.elastic.petstore.triggers.GetPetsByStatus",
      "type": "polling",
      "title": "Get Pets By Status (HttpClient)",
      "description": "Retrieves pets from the Petstore API by given pet status using Apache HttpClient",
      "fields": {
        "status": {
          "label": "Pet Status",
          "required": true,
          "viewClass": "SelectView",
          "model": {
            "available": "Available",
            "pending": "Pending",
            "sold": "Sold"
          },
          "prompt": "Select Pet Status"
        }
      },
      "metadata": {
        "out": "./schemas/getPetsByStatus.out.json"
      }
    },
    "getPetsByStatusJaxRs": {
      "main": "io.elastic.petstore.triggers.GetPetsByStatusJaxRs",
      "type": "polling",
      "title": "Get Pets By Status (JAX-RS)",
      "description": "Retrieves pets from the Petstore API by given pet status using Java API for RESTful Web Services (JAX-RS)",
      "fields": {
        "status": {
          "label": "Pet Status",
          "required": true,
          "viewClass": "SelectView",
          "model": {
            "available": "Available",
            "pending": "Pending",
            "sold": "Sold"
          },
          "prompt": "Select Pet Status"
        }
      },
      "metadata": {
        "out": "./schemas/getPetsByStatus.out.json"
      }
    },
    "getPetsByStatusWithDynamicSelectModel": {
      "main": "io.elastic.petstore.triggers.GetPetsByStatus",
      "type": "polling",
      "title": "Get Pets By Status With Dynamic Select Model",
      "description": "Retrieves pets from the Petstore API by given pet status. The available statuses are retrieved from the Petstore API dynamically.",
      "fields": {
        "status": {
          "label": "Pet Status",
          "required": true,
          "viewClass": "SelectView",
          "model": "io.elastic.petstore.providers.PetStatusModelProvider",
          "prompt": "Select Pet Status"
        }
      },
      "metadata": {
        "out": "./schemas/getPetsByStatus.out.json"
      }
    }
  },
  "actions": {
    "createPet": {
      "main": "io.elastic.petstore.actions.CreatePet",
      "title": "Create a Pet",
      "description": "Creates a new Pet",
      "metadata": {
        "in": "./schemas/createPet.in.json",
        "out": "./schemas/createPet.out.json"
      }
    }
  }
}

We are using Gradle wrapper

To build this component we are using a Gradle wrapper for convenience and cross-platform implementation since this wrapper is a batch script on Windows (gradlew.bat), and a shell script (gradlew) for other operating systems.

When you start a Gradle build via the wrapper, Gradle will be automatically downloaded and used to run the build. When the Gradle wrapper is installed it will add several files into your repository automatically which should not be removed. Here are the files and their purpose:

  • gradle/wrapper - directory which contains two files:
    • gradle-wrapper.jar
    • gradle-wrapper.properties

build.gradle

This is Gradle build file which includes the rules of the build.

apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'idea'
apply plugin: 'eclipse'
apply plugin: 'java-library-distribution'

group = 'io.elastic'
version = '1.0.0'

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    maven {
        url "https://oss.sonatype.org/content/repositories/snapshots"
    }
    mavenCentral()
    mavenLocal()
}

dependencies {
    compile "io.elastic:sailor-jvm:2.0.1"
    compile "org.glassfish.jersey.core:jersey-client:2.25.1"
    compile "org.glassfish.jersey.media:jersey-media-json-processing:2.25.1"
}

uploadArchives {
    repositories {
        mavenLocal()
    }
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.0'
}

src/main

This directory contains all the relevant dependency files and programs necessary for this component to work:

  • java/io/elastic/petstore
    • actions
      • CreatePet.java
    • providers
      • PetStatusModelProvider.java
    • triggers
      • GetPetsByStatus.java
      • GetPetsByStatusJaxRs.java
    • ApiKeyVerifier.java
    • Constants.java
    • HttpClientUtils.java
  • schemas
    • getHello.out.json
    • updateHello.in.json
    • updateHello.out.json