Skip to main content
This example demonstrates the basics of creating a workflow and service with Infinitic. You’ll learn how to define interfaces, implement services, build workflows, and execute them.

What You’ll Build

A simple workflow that concatenates strings using a service, demonstrating:
  • Service definition and implementation
  • Workflow orchestration
  • Task execution
  • Client usage
1
Define the Service Interface
2
Create an interface for your service tasks:
3
package com.example

interface HelloWorldService {
    fun concat(str1: String, str2: String): String
    fun reverse(str: String): String
}
4
Implement the Service
5
Implement the service with your business logic:
6
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()
    }
}
7
Define the Workflow Interface
8
Create an interface for your workflow:
9
package com.example

interface HelloWorldWorkflow {
    fun greet(name: String): String
}
10
Implement the Workflow
11
Implement the workflow by orchestrating service calls:
12
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
    }
}
13
Configure Infinitic
14
Create an infinitic.yml configuration file:
15
transport: inMemory

services:
  - name: HelloWorldService
    class: com.example.HelloWorldServiceImpl

workflows:
  - name: HelloWorldWorkflow
    class: com.example.HelloWorldWorkflowImpl
16
Start Workers
17
Create and start workers to process tasks:
18
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...")
}
19
Execute the Workflow
20
Create a client to start the workflow:
21
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()
}
22
Run Asynchronously
23
For long-running workflows, dispatch them asynchronously:
24
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:
Hello World!

Key Concepts

Services contain the actual business logic. Each method represents a task that can be executed independently, with automatic retry and timeout handling.
Workflows orchestrate services by defining the order and logic of task execution. They maintain state and can run for long periods, surviving system restarts.
Use newService() to create service stubs within workflows. These stubs automatically handle task dispatching and result retrieval.
The client is used to start workflows, send signals, and retrieve results. It can dispatch workflows synchronously or asynchronously.

Next Steps

Build docs developers (and LLMs) love