Authoring Java SDK Tasks
The Airflow Java SDK implements the language-SDK model for the JVM: your DAG stays in Python, and each task instance runs in a short-lived JVM subprocess. This skill covers the Java-specific native API. The shared model — the Python @task.stub pattern, ID matching, and the XCom-as-JSON contract — lives in authoring-language-sdk-tasks; read that first if you're new to language SDKs.
Experimental. The Java SDK is in preview. Artifact coordinates and APIs may change.
Related skills: authoring-language-sdk-tasks (shared Python stub + concepts), configuring-airflow-language-sdks (route the queue to
JavaCoordinator), deploying-java-sdk-bundles (compile and ship the JAR).
Recap: the Python side
Java tasks are paired with Python stubs that carry no logic — they declare the task, queue, dependency graph, and retries. IDs must match the Java annotations exactly, and an upstream argument on a stub only declares the dependency (the value is fetched in Java). Full rules are in authoring-language-sdk-tasks; the minimal shape:
Java side: two APIs
Both APIs produce identical runtime behavior; pick by style, and you can mix them in one bundle.
Annotation-based API (recommended)
Annotate a plain class; an annotation processor generates the wiring (<ClassName>Builder) at compile time.
Annotation reference:
A task method's return value is automatically pushed as that task's return_value XCom. A method may declare throws Exception; any uncaught exception fails the task instance (which triggers retries if the stub configured them).
Interface-based API
Implement Task directly when you want full control over registration and XCom handling.
Register tasks manually in a Dag and expose it through a BundleBuilder:
Each Task class needs a public no-arg constructor. Task IDs must be unique within a DAG, and DAG IDs unique within a bundle.
The entry point
Every bundle has a main that hands your DAGs to the SDK server. The server connects to the coordinator, runs one task instance, and exits.
Server.create(args) parses the connection details Airflow passes on the command line — don't construct them by hand. Record this main class as the bundle's main class when you build it (see deploying-java-sdk-bundles).
Talking to Airflow from a task: Client
A Client is passed into every task and is scoped to the current DAG run and task instance.
Context
The Context parameter exposes run metadata: context.dagRun (dagId, runId) and context.ti (dagId, runId, taskId, mapIndex, tryNumber). tryNumber is useful for retry-aware logic.
XCom: Java types
XComs cross the boundary as JSON (the shared contract is in authoring-language-sdk-tasks). When you read one back in Java you get:
Declare @Builder.XCom parameter types to match. A mismatch (e.g. declaring int when the value is a String) fails the task.
Logging
Declare a logger as a static field named after the class — the conventional pattern regardless of framework:
For records to reach Airflow's task log store (and show in the UI), the bundle must include one of the SDK logging integration artifacts (airflow-sdk-jpl, airflow-sdk-slf4j, airflow-sdk-log4j2, or airflow-sdk-jul). The dependencies and per-framework setup are in the logging integration section of deploying-java-sdk-bundles. System.Logger (JPL) with airflow-sdk-jpl is the lightest option and needs no configuration.
A complete worked example ships with the SDK
The SDK repository includes a runnable example under java-sdk/example/:
src/resources/dags/java_examples.py— Python DAGs pairing Python tasks with Java stubs, including aloadstub withretries=1.src/java/.../AnnotationExample.java— annotation API, including a task that fails ontryNumber == 1and succeeds on retry.src/java/.../InterfaceExampleBuilder.java— the same tasks via theTaskinterface andDag.addTask(...).src/java/.../ExampleBundleBuilder.java— aBundleBuilderreturning both DAGs plus themainentry point.
Point users there for an end-to-end reference.
Java-specific pitfalls
- Cast
Objectreturns deliberately.getVariableandgetXComreturnObject; match the cast to the JSON type (see the table above). @Builder.XComparameter types must match the stored JSON type, or the task fails at runtime.- The annotation processor must be on the build for the annotation API (generates
<ClassName>Builder); it is not needed for the interface API. See deploying-java-sdk-bundles. - See authoring-language-sdk-tasks for the language-agnostic pitfalls (ID matching, one JVM per task instance, queue/retries on the stub).
Related Skills
- authoring-language-sdk-tasks: Shared Python-stub pattern and concepts (read first).
- configuring-airflow-language-sdks: Route the
javaqueue toJavaCoordinatorand set JRE/coordinator options. - deploying-java-sdk-bundles: Build the bundle (Gradle/Maven) and place the JAR where Airflow can find it.
- authoring-dags: General Airflow DAG authoring.

