Skip to main content
The Workflow abstract class is the base class for all workflow implementations in Infinitic. Workflows orchestrate the execution of tasks and other workflows in a durable, fault-tolerant manner.

Package

io.infinitic.workflows.Workflow

Overview

Workflows in Infinitic are defined by extending the Workflow abstract class. The workflow implementation contains the orchestration logic that coordinates tasks, handles results, and manages the overall business process.

Static Properties

These properties provide context information about the currently executing workflow:
workflowName
String
The name of the workflow
workflowId
String
The unique identifier of the workflow instance
methodName
String
The name of the currently executing workflow method
methodId
String
The unique identifier of the workflow method execution
tags
Set<String>
The set of tags associated with the workflow instance
meta
Map<String, ByteArray>
Metadata associated with the workflow instance

Protected Methods

Task and Workflow Creation

newService
<T> T
Creates a stub for dispatching tasks to a serviceParameters:
  • klass: Class<out T> - The service interface class
  • tags: Set<String>? - Optional tags to apply to tasks (default: null)
  • meta: Map<String, ByteArray>? - Optional metadata for tasks (default: null)
Returns: A proxy stub for the serviceExample:
val service = newService(MyService::class.java)
val deferred = dispatch(service::processData, data)
newWorkflow
<T> T
Creates a stub for dispatching a new child workflowParameters:
  • klass: Class<out T> - The workflow interface class
  • tags: Set<String>? - Optional tags to apply to the workflow (default: null)
  • meta: Map<String, ByteArray>? - Optional metadata for the workflow (default: null)
Returns: A proxy stub for the workflow
getWorkflowById
<T> T
Gets a stub for an existing workflow by its IDParameters:
  • klass: Class<out T> - The workflow interface class
  • id: String - The workflow instance ID
Returns: A proxy stub for the existing workflow
getWorkflowByTag
<T> T
Gets a stub for an existing workflow by its tagParameters:
  • klass: Class<out T> - The workflow interface class
  • tag: String - The workflow tag
Returns: A proxy stub for the existing workflow

Dispatching Tasks and Workflows

dispatch
<R> Deferred<R>
Dispatches a task or workflow method and returns a Deferred for the resultOverloads:
  • dispatch(method: () -> R): Deferred<R> - No parameters
  • dispatch(method: (P1) -> R, p1: P1): Deferred<R> - 1 parameter
  • dispatch(method: (P1, P2) -> R, p1: P1, p2: P2): Deferred<R> - 2 parameters
  • Up to 9 parameters supported
Example:
val service = newService(MyService::class.java)
val deferred = dispatch(service::calculate, input)
val result = deferred.await()
dispatchVoid
Deferred<Void>
Dispatches a task or workflow method that returns voidOverloads: Support 0 to 9 parameters using Consumer interfaces

Timers

timer
Deferred<Instant>
Creates a timer that completes after a specified duration or at a specific instantOverloads:
  • timer(duration: Duration): Deferred<Instant> - Timer based on duration
  • timer(instant: Instant): Deferred<Instant> - Timer based on absolute time
Example:
// Wait for 5 minutes
timer(Duration.ofMinutes(5)).await()

// Wait until specific time
timer(Instant.parse("2024-12-31T23:59:59Z")).await()

Channels

channel
<T> Channel<T>
Creates a channel for sending and receiving signals within the workflowReturns: A new Channel instanceExample:
val approvalChannel = channel<ApprovalSignal>()
val approval = approvalChannel.receive(ApprovalSignal::class.java).await()

Inline Execution

inline
<S> S
Executes a task inline (synchronously) without dispatchingParameters:
  • task: () -> S - The task to execute
Returns: The result of the taskExample:
val result = inline {
  // This runs synchronously within the workflow
  computeValue()
}
inlineVoid
Unit
Executes a void task inline (synchronously) without dispatchingParameters:
  • task: Consumer0 - The task to execute

Usage Example

import io.infinitic.workflows.Workflow
import java.time.Duration

interface BookingWorkflow {
  fun processBooking(bookingId: String): BookingResult
}

class BookingWorkflowImpl : Workflow(), BookingWorkflow {
  override fun processBooking(bookingId: String): BookingResult {
    // Create service stubs
    val paymentService = newService(PaymentService::class.java)
    val inventoryService = newService(InventoryService::class.java)
    val notificationService = newService(NotificationService::class.java)
    
    // Reserve inventory
    val reservationDeferred = dispatch(inventoryService::reserve, bookingId)
    val reservation = reservationDeferred.await()
    
    // Process payment with timeout
    val paymentDeferred = dispatch(paymentService::charge, reservation.amount)
    timer(Duration.ofMinutes(5)).await() // Wait up to 5 minutes
    
    if (paymentDeferred.isCompleted()) {
      val payment = paymentDeferred.await()
      
      // Send confirmation
      dispatch(notificationService::sendConfirmation, bookingId)
      
      return BookingResult.success(payment, reservation)
    } else {
      // Cancel reservation if payment times out
      dispatch(inventoryService::cancelReservation, reservation.id)
      return BookingResult.timeout()
    }
  }
}

Notes

  • All workflow methods must be deterministic
  • Use @Ignore annotation on properties that should not be persisted
  • Workflows are automatically retried on failure according to the retry policy
  • Channel names are automatically set based on the getter method name

See Also

Build docs developers (and LLMs) love