Learn how workflows orchestrate services and manage complex business processes in Infinitic
Workflows are the orchestration layer in Infinitic. They coordinate the execution of services, manage state, and define the business logic of your application using the full power of programming languages.
Workflows are defined by creating an interface and an implementation that extends Workflow:
interface OrderWorkflow { fun processOrder(orderId: String): OrderResult}class OrderWorkflowImpl : Workflow(), OrderWorkflow { // Service stubs private val paymentService = newService(PaymentService::class.java) private val inventoryService = newService(InventoryService::class.java) private val shippingService = newService(ShippingService::class.java) override fun processOrder(orderId: String): OrderResult { // Reserve inventory val reservation = inventoryService.reserve(orderId) // Process payment val payment = paymentService.charge(orderId, reservation.amount) // Ship order val shipment = shippingService.ship(orderId, reservation) return OrderResult(payment.transactionId, shipment.trackingNumber) }}
The workflow implementation class must extend io.infinitic.workflows.Workflow from the source code at /infinitic-common/src/main/kotlin/io/infinitic/workflows/Workflow.kt
Workflows can dispatch service calls asynchronously using the dispatch method:
class ParallelWorkflowImpl : Workflow(), ParallelWorkflow { private val service = newService(DataService::class.java) override fun processData(items: List<String>): List<Result> { // Dispatch all calls in parallel val deferreds = items.map { item -> dispatch(service::process, item) } // Wait for all results return deferreds.map { it.await() } }}
The Deferred<T> class represents an asynchronous computation. It provides methods to:
await(): Wait for completion and get the result
status(): Check current status (ONGOING, COMPLETED, FAILED, CANCELED, UNKNOWN)
isCompleted(), isFailed(), isCanceled(): Check specific states
val deferred = dispatch(service::longRunning, params)// Check status without blockingif (deferred.isOngoing()) { // Do something else}// Wait for resultval result = deferred.await()
You can combine multiple deferred objects using and and or operators:
// Wait for all to completeval all = and(deferred1, deferred2, deferred3)val results = all.await() // Returns List<T>// Wait for any to completeval any = or(deferred1, deferred2, deferred3)val firstResult = any.await() // Returns T
Workflows can create timers for delays and scheduling:
import java.time.Durationimport java.time.Instantclass TimerWorkflowImpl : Workflow(), TimerWorkflow { override fun processWithDelay() { // Wait for 1 hour timer(Duration.ofHours(1)).await() // Or schedule for specific time timer(Instant.parse("2026-12-31T23:59:59Z")).await() }}
class ParentWorkflowImpl : Workflow(), ParentWorkflow { override fun orchestrate() { // Create a new child workflow val childWorkflow = newWorkflow(ChildWorkflow::class.java) // Execute child workflow method val result = childWorkflow.process() // Or dispatch asynchronously val deferred = dispatch(childWorkflow::process) val result = deferred.await() }}