Skip to main content
Version: 0.0.20

Step 4: Write your node

Step 4 of 6About 30 minutes

You don't start from a blank file. Take the template's Hello World node (or Hello Java) and rewrite it into your own: rename the files and the class, then replace the ports, settings and logic. The running example here is a Word Counter that counts the words in a text column and appends the count as a new column.

Every code sample on this page was compiled and its tests were run against platform 0.0.20. Pick your language in the tabs; the choice sticks across the page.

A node is three files that share a name. Do them in this order:

  1. The node class
  2. The help page
  3. The unit test

Once those work, More node recipes shows how to add a second node, write a source node, use credentials and more.

1. The node class​

Rename HelloWorldNodeModel.kt (Java: HelloJavaNodeModel.java) to WordCounterNodeModel.kt (or .java) and the class inside it, then replace the contents. The class has six parts:

@ContinuumNode ← required, never remove
class WordCounterNodeModel : ProcessNodeModel() {
1. ports what goes in and out
2. categories where it appears in the palette
3. propertiesSchema what settings exist, and what is valid
4. propertiesUiSchema how the settings form looks
5. metadata name, description, icon, defaults
6. execute() the logic
}

Here is the finished Word Counter, with the six parts marked. It lives in the package <package>.node, which is com.acme.demo.node in this example:

package com.acme.demo.node

import org.projectcontinuum.core.commons.annotation.ContinuumNode
import org.projectcontinuum.core.commons.exception.NodeRuntimeException
import org.projectcontinuum.core.commons.model.ContinuumWorkflowModel
import org.projectcontinuum.core.commons.node.ProcessNodeModel
import org.projectcontinuum.core.commons.protocol.progress.NodeProgressCallback
import org.projectcontinuum.core.commons.utils.NodeInputReader
import org.projectcontinuum.core.commons.utils.NodeOutputWriter
import org.springframework.http.MediaType.APPLICATION_OCTET_STREAM_VALUE
import tools.jackson.core.type.TypeReference
import tools.jackson.databind.ObjectMapper

@ContinuumNode
class WordCounterNodeModel(
private val objectMapper: ObjectMapper
) : ProcessNodeModel() {

private val whitespace = Regex("\\s+")

// 1. Ports. The map keys ("input", "output") are the port IDs you use in execute().
final override val inputPorts = mapOf(
"input" to ContinuumWorkflowModel.NodePort(name = "Input Table", contentType = APPLICATION_OCTET_STREAM_VALUE)
)
final override val outputPorts = mapOf(
"output" to ContinuumWorkflowModel.NodePort(name = "Table with word counts", contentType = APPLICATION_OCTET_STREAM_VALUE)
)

// 2. Palette category
override val categories = listOf("Demo")

// 3. JSON Schema: the settings and their rules
private val propertiesSchema: Map<String, Any> = objectMapper.readValue(
"""
{
"type": "object",
"properties": {
"inputCol": { "type": "string", "title": "Input Column" },
"outputCol": { "type": "string", "title": "Output Column" },
"minWordLength": { "type": "integer", "title": "Minimum Word Length", "minimum": 1, "default": 1 }
},
"required": ["inputCol", "outputCol"]
}
""".trimIndent(),
object : TypeReference<Map<String, Any>>() {}
)

// 4. JSON Forms UI schema: the layout of the settings form
private val propertiesUiSchema: Map<String, Any> = objectMapper.readValue(
"""
{
"type": "VerticalLayout",
"elements": [
{ "type": "Control", "scope": "#/properties/inputCol" },
{ "type": "Control", "scope": "#/properties/outputCol" },
{ "type": "Control", "scope": "#/properties/minWordLength" }
]
}
""".trimIndent(),
object : TypeReference<Map<String, Any>>() {}
)

// 5. Metadata: what the Workbench and the registry see
override val metadata = ContinuumWorkflowModel.NodeData(
id = this.javaClass.name,
nodeModel = this.javaClass.name,
title = "Word Counter",
subTitle = "Count words per row",
description = "Counts the words in a text column and appends the count as a new column",
icon = """<svg xmlns="http://www.w3.org/2000/svg" fill="none" stroke="currentColor" stroke-width="1.5" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" d="M4 6h16M4 12h10M4 18h6M18 14v6m-3-3h6"/></svg>""",
inputs = inputPorts,
outputs = outputPorts,
properties = mapOf("inputCol" to "text", "outputCol" to "word_count", "minWordLength" to 1),
propertiesSchema = propertiesSchema,
propertiesUISchema = propertiesUiSchema
)

// 6. The logic
override fun execute(
properties: Map<String, Any>?,
inputs: Map<String, NodeInputReader>,
nodeOutputWriter: NodeOutputWriter,
nodeProgressCallback: NodeProgressCallback
) {
val inputCol = properties?.get("inputCol") as String?
?: throw NodeRuntimeException(isRetriable = false, workflowId = "", nodeId = "", message = "inputCol is not provided")
val outputCol = properties["outputCol"] as String?
?: throw NodeRuntimeException(isRetriable = false, workflowId = "", nodeId = "", message = "outputCol is not provided")
val minWordLength = (properties["minWordLength"] as Number?)?.toInt() ?: 1

val reader = inputs["input"] ?: throw NodeRuntimeException(
isRetriable = false, workflowId = "", nodeId = "", message = "Input port 'input' is not connected"
)
val totalRows = reader.getRowCount()

nodeOutputWriter.createOutputPortWriter("output").use { writer ->
reader.use {
var rowNumber = 0L
while (true) {
val row = reader.read() ?: break
val text = row[inputCol]?.toString() ?: ""
val count = text.trim().split(whitespace).count { it.length >= minWordLength }

writer.write(rowNumber, row.toMutableMap().apply { this[outputCol] = count })
rowNumber++
if (totalRows > 0) nodeProgressCallback.report((rowNumber * 100 / totalRows).toInt())
}
}
}
}
}

Rules the platform enforces​

Break one of these and the build fails, the node doesn't load, or workflows break later.

RuleWhy
Annotate with @ContinuumNode, not @Component.It makes the bean prototype-scoped, so each run gets a fresh instance. The build fails without it (validateContinuumNodeAnnotations).
Extend ProcessNodeModel (has inputs) or TriggerNodeModel (source node, outputs only).TriggerNodeModel.execute() has no inputs parameter. See a source node.
Set metadata.id and metadata.nodeModel to the class's full name (this.javaClass.name / getClass().getName()).The full class name is the node's identity in saved workflows. Renaming or moving the class later breaks workflows that use it.
Put the class in a package that ends in .node.By convention. The platform removes that last segment to get the feature ID (com.acme.demo.node β†’ com.acme.demo). Without it, the whole package name becomes the feature ID, and the node still loads.
Import Jackson from tools.jackson.*.The platform uses Jackson 3. com.fasterxml.jackson.* (Jackson 2) won't compile.

Two notes on ObjectMapper:

  • In Kotlin you can take it as a constructor parameter and Spring injects it. In unit tests, pass jacksonObjectMapper().
  • In Java, the example creates a static mapper, which works because the schemas are built once when the class loads. You can also take an ObjectMapper in a constructor, as in Kotlin.

If you'd rather not parse JSON strings at all, you can write the schemas as nested mapOf(...) / Map.of(...) values. The source node recipe does that.

Ports​

inputPorts and outputPorts are maps:

  • The key (here "input" and "output") is the port ID you use in execute() to read or write data. The template's Hello World node uses the same two IDs, so the rest of the platform's examples look familiar.
  • The value is a ContinuumWorkflowModel.NodePort with a name (shown in the Workbench) and a contentType.

Table ports use APPLICATION_OCTET_STREAM_VALUE, as in the running example. The template's example nodes do the same.

A node can have several ports. Add more entries to the map, give each a different key, and use those keys in inputs["…"] and createOutputPortWriter("…"). A node with two inputs (for example "left" and "right") gets both readers in the inputs map; check each key you need, because a port that isn't connected is missing from the map.

Settings and validation​

  • properties in the metadata holds the default values used when a user drops the node on the canvas.
  • Before execute() runs, the platform checks the user's settings against propertiesSchema. Values that break required, type or minimum fail the node before your code starts. Checking again in execute() is still worthwhile because it lets you give a clearer error message.
  • propertiesUiSchema is a JSON Forms layout, used only to draw the form. For credential pickers, see Credentials. Design and try the form first in the JSONForms Playground; see Design the settings dialog.

Reading and writing rows​

  • inputs["input"] is a NodeInputReader. read() returns the next row as a Map<String, Any>, or null at the end. If a port isn't connected, its key is missing from inputs.
  • createOutputPortWriter("output") returns a writer. write(rowNumber, row) takes a Long row number that starts at 0.
  • Close both when you are done: Kotlin's .use { } or Java's try { … } finally { … }. The output file is only complete once the writer is closed.

Cell values must be one of these types, and never null. Anything else makes write() throw IllegalArgumentException: Unsupported type. Leave a key out instead of storing null.

Kotlin / Java typeStored as
Stringapplication/vnd.continuum.x-string
Int, Longapplication/vnd.continuum.x-int, x-long
Float, Doubleapplication/vnd.continuum.x-float, x-double
Booleanapplication/vnd.continuum.x-boolean
List<*>, Map<*, *>application/json
SVG org.w3c.dom.Documentimage/svg+xml

Convert anything else first. For example, call toString() on a LocalDate, and convert a BigDecimal to a Double or a String.

Errors, retries and progress​

Every node runs as a Temporal activity, so an exception thrown from execute() is retried by default (with exponential backoff). Throw NodeRuntimeException(isRetriable = false, ...) (Java: new NodeRuntimeException(false, "", "", message)) for errors a retry can't fix, such as:

  • Missing or invalid settings (user error)
  • An unconnected input port
  • Invalid input data that can't be recovered from

Empty strings are fine for workflowId and nodeId in the exception.

To change the retry behaviour of one node, see per-node retry options.

Call nodeProgressCallback.report(percent) (0 to 100) as you go. The Workbench shows it as live progress on the node.

2. The help page​

Rename HelloWorldNodeModel.doc.md (Java: HelloJavaNodeModel.doc.md) to WordCounterNodeModel.doc.md and rewrite it. The Workbench shows this file next to the node.

The path must match the node's package and class name exactly. For the class com.acme.demo.node.WordCounterNodeModel, the file is:

src/main/resources/com/acme/demo/node/WordCounterNodeModel.doc.md

To build the path, take the class's full name, replace each . with / and add .doc.md. The node folder is part of the package, not an extra segment.

If the file is missing or misplaced, the node still works, but its help page reads "Documentation not found for WordCounterNodeModel", and the build doesn't warn you.

A good help page has four parts, as in the template's Hello World page: what the node does, a Configuration table, a Ports table, and a small Example with input and output tables.

3. The unit test​

Rename HelloWorldNodeModelTest.kt (Java: HelloJavaNodeModelTest.java) to WordCounterNodeModelTest.kt (or .java). Nodes are plain classes, so you can test execute() with Mockito mocks. No Spring, Kafka or Docker is needed.

package com.acme.demo.node

import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertThrows
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.mockito.kotlin.any
import org.mockito.kotlin.argumentCaptor
import org.mockito.kotlin.mock
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import org.projectcontinuum.core.commons.exception.NodeRuntimeException
import org.projectcontinuum.core.commons.utils.NodeInputReader
import org.projectcontinuum.core.commons.utils.NodeOutputWriter
import tools.jackson.module.kotlin.jacksonObjectMapper

class WordCounterNodeModelTest {
private val outputWriter = mock<NodeOutputWriter>()
private val portWriter = mock<NodeOutputWriter.OutputPortWriter>()
private val node = WordCounterNodeModel(jacksonObjectMapper())

@BeforeEach
fun setUp() {
whenever(outputWriter.createOutputPortWriter("output")).thenReturn(portWriter)
}

@Test
fun `counts words and keeps original columns`() {
val reader = mockReader(listOf(mapOf("id" to 1, "text" to "the quick brown fox")))

node.execute(mapOf("inputCol" to "text", "outputCol" to "word_count"), mapOf("input" to reader), outputWriter, mock())

val rows = argumentCaptor<Map<String, Any>>()
verify(portWriter).write(any(), rows.capture())
assertEquals(4, rows.firstValue["word_count"])
assertEquals(1, rows.firstValue["id"])
}

@Test
fun `ignores words shorter than the minimum length`() {
val reader = mockReader(listOf(mapOf("text" to "a quick brown fox")))

node.execute(
mapOf("inputCol" to "text", "outputCol" to "word_count", "minWordLength" to 4),
mapOf("input" to reader), outputWriter, mock()
)

val rows = argumentCaptor<Map<String, Any>>()
verify(portWriter).write(any(), rows.capture())
assertEquals(2, rows.firstValue["word_count"])
}

@Test
fun `fails without retry when the input port is not connected`() {
val error = assertThrows(NodeRuntimeException::class.java) {
node.execute(mapOf("inputCol" to "text", "outputCol" to "word_count"), emptyMap(), outputWriter, mock())
}
assertEquals(false, error.isRetriable)
}

private fun mockReader(rows: List<Map<String, Any>>): NodeInputReader {
val iterator = rows.iterator()
return mock<NodeInputReader>().also { r ->
whenever(r.read()).thenAnswer { if (iterator.hasNext()) iterator.next() else null }
whenever(r.getRowCount()).thenReturn(rows.size.toLong())
}
}
}

The template's Hello World test has more cases to copy, including metadata checks and progress reporting.

note

These tests call execute() directly, so they skip the schema validation. To test validation as well, build a ContinuumWorkflowModel.Node and call node.run(...) instead.

Next: test and run your node.