What You’ll Build
A simple workflow that concatenates strings using a service, demonstrating:- Service definition and implementation
- Workflow orchestration
- Task execution
- Client usage
package com.example
interface HelloWorldService {
fun concat(str1: String, str2: String): String
fun reverse(str: String): String
}
package com.example
class HelloWorldServiceImpl : HelloWorldService {
override fun concat(str1: String, str2: String): String {
return str1 + str2
}
override fun reverse(str: String): String {
return str.reversed()
}
}
package com.example
import io.infinitic.workflows.Workflow
class HelloWorldWorkflowImpl : Workflow(), HelloWorldWorkflow {
// Create a stub for the service
private val service = newService(HelloWorldService::class.java)
override fun greet(name: String): String {
// Orchestrate multiple service calls
val hello = service.concat("Hello", " ")
val greeting = service.concat(hello, name)
val excited = service.concat(greeting, "!")
return excited
}
}
transport: inMemory
services:
- name: HelloWorldService
class: com.example.HelloWorldServiceImpl
workflows:
- name: HelloWorldWorkflow
class: com.example.HelloWorldWorkflowImpl
package com.example
import io.infinitic.worker.InfiniticWorker
fun main() {
// Create worker from configuration
val worker = InfiniticWorker.fromConfigFile("infinitic.yml")
// Start processing
worker.start()
println("Worker started and ready to process tasks...")
}
package com.example
import io.infinitic.client.InfiniticClient
fun main() {
// Create client from configuration
val client = InfiniticClient.fromConfigFile("infinitic.yml")
// Get a workflow stub
val workflow = client.newWorkflow(HelloWorldWorkflow::class.java)
// Execute the workflow synchronously
val result = workflow.greet("World")
println(result) // Prints: Hello World!
// Clean up
client.close()
}
val client = InfiniticClient.fromConfigFile("infinitic.yml")
val workflow = client.newWorkflow(HelloWorldWorkflow::class.java)
// Dispatch asynchronously
val deferred = client.dispatch(workflow::greet, "World")
println("Workflow dispatched with ID: ${deferred.id}")
// Wait for completion
val result = deferred.await()
println(result) // Prints: Hello World!
client.close()
Expected Output
When you run the workflow, you should see:Key Concepts
Services
Services
Services contain the actual business logic. Each method represents a task that can be executed independently, with automatic retry and timeout handling.
Workflows
Workflows
Workflows orchestrate services by defining the order and logic of task execution. They maintain state and can run for long periods, surviving system restarts.
Stubs
Stubs
Use
newService() to create service stubs within workflows. These stubs automatically handle task dispatching and result retrieval.Client
Client
The client is used to start workflows, send signals, and retrieve results. It can dispatch workflows synchronously or asynchronously.
Next Steps
- Learn about error handling in workflows
- Explore workflow methods for state management
- Try the booking workflow example for a more complex scenario