JVM SDK¶
The JVM SDK provides Kotlin-first, Java-friendly support for Heddle processor
workers. It includes the heddle-sdk-core and heddle-sdk-nats packages.
Add the package¶
The SDK is built with Gradle. For a local checkout, add the project to your
settings.gradle.kts:
And add the dependency to your build.gradle.kts:
dependencies {
implementation("heddle:core:0.1.0") // core models and worker base
implementation("heddle:nats:0.1.0") // NATS transport adapter
}
Define payload and output models¶
Kotlin¶
Java¶
public class EchoPayload {
public String message;
}
public class EchoOutput {
public String response;
public EchoOutput(String response) { this.response = response; }
}
Implement a worker¶
Kotlin¶
class KotlinEchoWorker(transport: HeddleTransport) : HeddleWorker<EchoPayload, EchoOutput>(
workerType = "echo",
payloadClass = EchoPayload::class.java,
outputClass = EchoOutput::class.java,
transport = transport
) {
override suspend fun process(payload: EchoPayload, metadata: Map<String, Any?>): WorkerOutput<EchoOutput> {
return WorkerOutput(EchoOutput("Echo: ${payload.message}"))
}
}
Java¶
public class JavaEchoWorker extends HeddleWorker<EchoPayload, EchoOutput> {
public JavaEchoWorker(HeddleTransport transport) {
super("echo", ModelTier.STANDARD, EchoPayload.class, EchoOutput.class, transport);
}
@Override
public Object process(EchoPayload payload, Map<String, ?> metadata, Continuation<? super WorkerOutput<EchoOutput>> $completion) {
return new WorkerOutput<>(new EchoOutput("Echo: " + payload.message));
}
}
Run with a transport¶
The core SDK defines the HeddleTransport interface. You can use the
in-memory transport for tests or the NATS transport for live interop.
Kotlin (Coroutines)¶
fun main() = runBlocking {
val transport = NatsHeddleTransport.connect("nats://localhost:4222")
val worker = KotlinEchoWorker(transport)
worker.run()
}
Java (runBlocking bridge)¶
public static void main(String[] args) {
NatsHeddleTransport transport = NatsHeddleTransport.Companion.connect("nats://localhost:4222");
JavaEchoWorker worker = new JavaEchoWorker(transport);
BuildersKt.runBlocking(GlobalScope.INSTANCE.getCoroutineContext(), (scope, continuation) -> {
return worker.run(continuation);
});
}
JVM notes¶
- Kotlin-first: The SDK is authored in Kotlin to leverage null-safety and coroutines, but APIs are designed to be idiomatic for Java.
- Wire format: Messages are
WireEnvelopeframes, read and written byHeddleWire(parse,wrap,serialize) over Jackson trees with strict, hand-checked rules; Jackson's lenient binding is not used for the frame or bodies._-prefixed lane keys are copied from each task frame to its result. - Enums: Strict. An unknown
model_tier,priorityorstatusvalue is a body error, and the worker skips the message (onMalformedMessage). - Hooks: override
reset()(called after every task) andonMalformedMessage(data, error)(called for a message that fails the frame, the body, or thepayload_typecheck). - No schema validation yet: unlike the .NET and Swift worker bases, the JVM
worker takes no input or output schema. It checks only that the output
serializes to a JSON object. Validate inside
processif you need more. - Transport: The NATS adapter uses the official
nats-io/jnatsclient. - Android: The core SDK is compatible with Android. If Android-specific transport constraints arise, use a specialized transport adapter.