Warning
This SDK is in an incubating phase and may change at any time.
A Java library for creating a Compute Module.
Below is a simple guide to get started with the java-compute-module.
From your terminal, initialize a new Gradle project with the Application plugin:
gradle init
This creates a basic Gradle structure with an Application plugin applied and a sample main class.
In your newly created project's build.gradle, add the necessary plugins, dependencies, and configurations:
plugins {
id 'application'
}
repositories {
mavenCentral()
}
dependencies {
// for testing
testImplementation 'junit:junit:4.13.2'
// common libraries
implementation 'com.google.guava:guava:31.1-jre'
implementation 'com.palantir.safe-logging:logger:3.7.0'
implementation 'com.palantir.safe-logging:preconditions:3.7.0'
implementation 'com.palantir.safe-logging:safe-logging:3.7.0'
implementation 'org.slf4j:slf4j-api:1.7.36'
// includes java-compute-module lib
implementation 'com.palantir.computemodules:lib:0.1.0'
// Jackson for JSON manipulation
implementation 'com.fasterxml.jackson.core:jackson-core:2.18.2'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.18.2'
}
java {
toolchain {
// specify Java version
languageVersion = JavaLanguageVersion.of(21)
}
}
application {
// define main class
mainClass = 'App'
}
Within the generated src/main/java folder, create (or update) a class named App. Below is a simple example:
import com.palantir.computemodules.functions.Context;
import com.palantir.computemodules.ComputeModule;
public class App {
public static void main(String[] args) {
ComputeModule.builder()
.add(App::hello, String.class, String.class, "hello")
.build()
.start();
}
static String hello(Context context, String name) {
return "hello " + name;
}
}
-
Importing Classes:
- We import the necessary classes from the
java-compute-module
library to utilize its functionality in our application.
- We import the necessary classes from the
-
Creating a ComputeModule Instance:
- We instantiate a
ComputeModule
using a builder pattern. - During this process, we attach the
hello
function to the module using the.add
method. - The method signature
.add(App::hello, String.class, String.class, "hello")
specifies:App::hello
: The function to be executed, referenced from theApp
class.String.class
(input type): The function accepts aString
as input.String.class
(output type): The function returns aString
."hello"
: A unique identifier for the function within the module.
- We instantiate a
-
Starting the Compute Module:
- When the application launches, the compute module is started by invoking the
.start()
method. - This initiates the module and makes the registered functions, like
hello
, available for execution.
- When the application launches, the compute module is started by invoking the
Containerize your application and then upload the resulting Docker image to Foundry. Once uploaded, you can reference your newly created image in a compute module. Example of Dockerfile:
FROM --platform=linux/amd64 gradle:jdk21 AS build
WORKDIR /src
COPY . /src
RUN gradle build --no-daemon
RUN unzip /src/app/build/distributions/app.zip -d /src
FROM --platform=linux/amd64 eclipse-temurin:21-jre-alpine
WORKDIR /app
COPY --from=build /src/app /app
USER 5000
CMD ["sh", "/app/bin/app"]
Steps to follow:
- Place this Dockerfile in your project's root directory (alongside the Gradle files).
- Run the build process:
docker build --platform=linux/amd64 -t your-image-name .
- Push the built image to your Foundry Docker registry.
- Use the image in a compute module.