class MyWorkflow : Workflow(), MyWorkflowInterface { // Basic service stub private val emailService = newService(EmailService::class.java) // Service with tags for routing private val premiumService = newService( EmailService::class.java, tags = setOf("premium", "priority-high") ) // Service with metadata private val trackedService = newService( EmailService::class.java, meta = mapOf("traceId" to "abc-123".toByteArray()) )}
class ParentWorkflow : Workflow(), ParentWorkflowInterface { private val childWorkflow = newWorkflow( ChildWorkflow::class.java, tags = setOf("child-task"), meta = mapOf("parentId" to workflowId.toByteArray()) ) override fun execute() { val result = childWorkflow.process() }}
Dispatch a service or workflow method asynchronously, returning a Deferred<R> object.
// No parametersprotected fun <R : Any?> dispatch(method: () -> R): Deferred<R>// With 1 parameterprotected fun <P1, R : Any?> dispatch( method: (p1: P1) -> R, p1: P1): Deferred<R>// With 2 parametersprotected fun <P1, P2, R : Any?> dispatch( method: (p1: P1, p2: P2) -> R, p1: P1, p2: P2): Deferred<R>// ... up to 9 parameters
The dispatch() method has overloads for 0-9 parameters.
class MyWorkflow : Workflow(), MyWorkflowInterface { private val service = newService(MyService::class.java) private val childWorkflow = newWorkflow(ChildWorkflow::class.java) override fun executeAsync() { // Dispatch service call val deferred1 = dispatch(service::processData, "input") // Dispatch workflow call val deferred2 = dispatch(childWorkflow::calculate, 42, "param2") // Wait for results val result1 = deferred1.await() val result2 = deferred2.await() }}
Dispatch a service or workflow method that returns void asynchronously.
// No parametersprotected fun dispatchVoid(method: Consumer0): Deferred<Void>// With 1 parameterprotected fun <P1> dispatchVoid( method: Consumer1<P1>, p1: P1): Deferred<Void>// ... up to 9 parameters
class MyWorkflow : Workflow(), MyWorkflowInterface { private val notificationService = newService(NotificationService::class.java) override fun sendNotifications() { // Dispatch void methods val d1 = dispatchVoid(notificationService::sendEmail, "[email protected]") val d2 = dispatchVoid(notificationService::sendSMS, "+1234567890") // Wait for completion d1.await() d2.await() }}
import java.time.Instantimport java.time.Durationclass MyWorkflow : Workflow(), MyWorkflowInterface { override fun scheduleForLater(scheduledTime: Instant) { // Wait until specific time timer(scheduledTime).await() // Execute at scheduled time service.executeScheduledTask() }}
See Timers for detailed usage including racing timers with other operations.