Skip to content

Using Java functions

Istemi Ekin Akkus edited this page Jul 30, 2020 · 1 revision

Java functions

In KNIX, the Java functions will have the following template:

import org.microfunctions.mfnapi.MicroFunctionsAPI;

public class HelloWorld
{
    public Object handle(Object event, MicroFunctionsAPI context)
    {
        context.log("Hello, World!");
        return event;
    }
}

Note that your function name should match your class name. In the above example, it would be HelloWorld.

There are various ways to use Java functions in KNIX workflows.

GUI template

You can use the GUI to modify a Java function following the given template.

Source zip

You can also upload a zip of source files, which will be compiled by KNIX. Note that you can also use packages for your function.

package foo;

import org.microfunctions.mfnapi.MicroFunctionsAPI;

public class HelloWorld
{
    public Object handle(Object event, MicroFunctionsAPI context)
    {
        context.log("Hello, World!");
        return event;
    }
}

Note, however, in this case, the function name should match the full path of the class name. In this example, it would be foo.HelloWorld.

Class zip

You can also upload a zip of class files. Note that the class files must be in the folder target/classes (similar to maven compilations). When using packages for your function, its name should include the package name (like in the Source zip example above).

Dependencies

One can also upload dependencies in the form of a maven pom file. Note that KNIX will only use the <dependencies> section.

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>io.knix</groupId>
  <artifactId>example_maven</artifactId>
  <version>1.0.0</version>

  <name>JavaRequestHandlerMavenTest</name>
  <url>https://knix.io/</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.json</groupId>
      <artifactId>json</artifactId>
      <version>20180813</version>
    </dependency>
  </dependencies>

</project>

Alternatively, you can download your own maven dependencies and extract them into the target/classes and upload your zip package.

Workflows

One can create workflows with Java functions as resources. It is not different than creating workflows with python functions. One can also use a mix of python and Java functions in the same workflow. In the below example, start is a python function and foo.HelloWorld is a Java function.

{
    "Comment": "wf_python_java Workflow",
    "StartAt": "StartWithPython",
    "States": {
        "StartWithPython": {
            "Type": "Task",
            "Resource": "start",
            "Next": "ContinueWithJava"
        },
        "ContinueWithJava": {
            "Type": "Task",
            "Resource": "foo.HelloWorld",
            "End": true
        }
    }
}

Tutorial

For a general tutorial, you can check out the Java functions tutorial.

Clone this wiki locally