|
1 | | -# dbos-transact-java |
2 | | -DBOS Transact Java SDK |
| 1 | +<div align="center"> |
3 | 2 |
|
4 | | -## Setting up dev environment |
| 3 | +# DBOS Transact: Lightweight Durable Workflows |
5 | 4 |
|
6 | | -Install a recent OpenJDK. I use OpenJDK 21. |
7 | | -https://adoptium.net/en-GB/temurin/releases/?os=any&arch=any&version=21 |
| 5 | +#### [Documentation](https://docs.dbos.dev/) • [Examples](https://docs.dbos.dev/examples) • [Github](https://github.com/dbos-inc) • [Discord](https://discord.com/invite/jsmC6pXGgX) |
| 6 | +</div> |
8 | 7 |
|
9 | | -Recommended IDE IntelliJ (Community edition is fine). |
10 | | -But feel free to use vi or VSCode, if you are more comfortable with it. |
| 8 | +--- |
11 | 9 |
|
12 | | -Postgres docker container with |
13 | | -localhost |
14 | | -port 5432 |
15 | | -user postgres |
| 10 | +## What is DBOS? |
16 | 11 |
|
17 | | -export PGPASSWORD = password for postgres user |
| 12 | +DBOS provides lightweight durable workflows built on top of Postgres. |
| 13 | +Instead of managing your own workflow orchestrator or task queue system, you can use DBOS to add durable workflows and queues to your program in just a few lines of code. |
18 | 14 |
|
19 | | -## build |
| 15 | +To get started, follow the [quickstart](https://docs.dbos.dev/quickstart) to install this open-source library and connect it to a Postgres database. |
| 16 | +Then, annotate workflows and steps in your program to make it durable! |
| 17 | +That's all you need to do—DBOS is entirely contained in this open-source library, there's no additional infrastructure for you to configure or manage. |
20 | 18 |
|
21 | | -./gradlew clean build |
| 19 | +## When Should I Use DBOS? |
22 | 20 |
|
23 | | -## run tests |
| 21 | +You should consider using DBOS if your application needs to **reliably handle failures**. |
| 22 | +For example, you might be building a payments service that must reliably process transactions even if servers crash mid-operation, or a long-running data pipeline that needs to resume seamlessly from checkpoints rather than restart from the beginning when interrupted. |
24 | 23 |
|
25 | | -./gradlew clean test |
| 24 | +Handling failures is costly and complicated, requiring complex state management and recovery logic as well as heavyweight tools like external orchestration services. |
| 25 | +DBOS makes it simpler: annotate your code to checkpoint it in Postgres and automatically recover from any failure. |
| 26 | +DBOS also provides powerful Postgres-backed primitives that makes it easier to write and operate reliable code, including durable queues, notifications, scheduling, event processing, and programmatic workflow management. |
26 | 27 |
|
27 | | -## publish to local maven repository |
| 28 | +## Features |
28 | 29 |
|
29 | | -./gradlew publishToMavenLocal |
| 30 | +<details open><summary><strong>💾 Durable Workflows</strong></summary> |
30 | 31 |
|
31 | | -## import transact into your application |
| 32 | +#### |
32 | 33 |
|
33 | | -Add to your build.gradle.kts |
| 34 | +DBOS workflows make your program **durable** by checkpointing its state in Postgres. |
| 35 | +If your program ever fails, when it restarts all your workflows will automatically resume from the last completed step. |
| 36 | + |
| 37 | +You add durable workflows to your existing Java program by annotating ordinary functions as workflows and steps: |
| 38 | + |
| 39 | +```java |
| 40 | + |
| 41 | +public interface SimpleWorkflowService { |
| 42 | + |
| 43 | + void setSimpleWorkflowService(SimpleWorkflowService e); |
| 44 | + String exampleWorkflow(String input) ; |
| 45 | + void stepOne() ; |
| 46 | + void stepTwo() ; |
| 47 | +} |
| 48 | + |
| 49 | +public class SimpleWorkflowServiceImpl implements SimpleWorkflowService { |
| 50 | + |
| 51 | + public void setSimpleWorkflowService(SimpleWorkflowService simpleWorkflow) { |
| 52 | + this.simpleWorkflowService = simpleWorkflow; |
| 53 | + } |
| 54 | + |
| 55 | + @Workflow(name = "exampleWorkflow") |
| 56 | + public String exampleWorkflow(String input) { |
| 57 | + return input + input; |
| 58 | + } |
| 59 | + @Step(name = "stepOne") |
| 60 | + public void stepOne() { |
| 61 | + logger.info("Executed stepOne") ; |
| 62 | + } |
| 63 | + @Step(name = "stepTwo") |
| 64 | + public void stepTwo() { |
| 65 | + logger.info("Executed stepTwo") ; |
| 66 | + } |
| 67 | + |
| 68 | +} |
| 69 | + |
| 70 | +public class Demo { |
| 71 | + |
| 72 | + public static void main(String[] args) { |
| 73 | + |
| 74 | + DBOSConfig dbosConfig = new DBOSConfig.Builder() |
| 75 | + .name("demo") |
| 76 | + .dbHost("localhost") |
| 77 | + .dbPort(5432) |
| 78 | + .dbUser("postgres") |
| 79 | + .sysDbName("demo_dbos_sys") |
| 80 | + .build() ; |
| 81 | + |
| 82 | + DBOS.initialize(dbosConfig); |
| 83 | + DBOS dbos = DBOS.getInstance(); |
| 84 | + dbos.launch(); |
| 85 | + |
| 86 | + SimpleWorkflowService syncExample = dbos.<SimpleWorkflowService>Workflow() |
| 87 | + .interfaceClass(SimpleWorkflowService.class) |
| 88 | + .implementation(new SimpleWorkflowServiceImpl()) |
| 89 | + .build(); |
| 90 | + syncExample.setSimpleWorkflowService(syncExample); |
| 91 | + |
| 92 | + String output = syncExample.exampleWorkflow("HelloDBOS") ; |
| 93 | + System.out.println("Sync result: " + output); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | + |
| 98 | +``` |
| 99 | + |
| 100 | +Workflows are particularly useful for |
| 101 | + |
| 102 | +- Orchestrating business processes so they seamlessly recover from any failure. |
| 103 | +- Building observable and fault-tolerant data pipelines. |
| 104 | +- Operating an AI agent, or any application that relies on unreliable or non-deterministic APIs. |
| 105 | + |
| 106 | +[Read more ↗️]() |
| 107 | + |
| 108 | +</details> |
| 109 | + |
| 110 | +<details><summary><strong>📒 Durable Queues</strong></summary> |
| 111 | + |
| 112 | +#### |
| 113 | + |
| 114 | +DBOS queues help you **durably** run tasks in the background. |
| 115 | +You can enqueue a task (which can be a single step or an entire workflow) from a durable workflow and one of your processes will pick it up for execution. |
| 116 | +DBOS manages the execution of your tasks: it guarantees that tasks complete, and that their callers get their results without needing to resubmit them, even if your application is interrupted. |
| 117 | + |
| 118 | +Queues also provide flow control, so you can limit the concurrency of your tasks on a per-queue or per-process basis. |
| 119 | +You can also set timeouts for tasks, rate limit how often queued tasks are executed, deduplicate tasks, or prioritize tasks. |
| 120 | + |
| 121 | +You can add queues to your workflows in just a couple lines of code. |
| 122 | +They don't require a separate queueing service or message broker—just Postgres. |
| 123 | + |
| 124 | +```java |
| 125 | + |
| 126 | + |
| 127 | + public void queuedTasks() { |
| 128 | + Queue q = new DBOS.QueueBuilder("childQ").build(); |
| 129 | + |
| 130 | + for (int i = 0; i < 3; i++) { |
| 131 | + |
| 132 | + String wid = "child" + i; |
| 133 | + DBOSOptions options = new DBOSOptions.Builder(wid).queue(q).build(); |
| 134 | + try (SetDBOSOptions o = new SetDBOSOptions(options)) { |
| 135 | + simpleService.childWorkflow(wid); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + for (int i = 0 ; i < 3 ; i++) { |
| 140 | + String wid = "child"+i; |
| 141 | + WorkflowHandle h = DBOS.retrieveWorkflow(wid); |
| 142 | + System.out.println(h.getResult()); |
| 143 | + } |
| 144 | + } |
| 145 | +``` |
| 146 | + |
| 147 | +[Read more ↗️](https://docs.dbos.dev/python/tutorials/queue-tutorial) |
| 148 | + |
| 149 | +</details> |
| 150 | + |
| 151 | + |
| 152 | +## Getting Started |
| 153 | + |
| 154 | +To get started, follow the [quickstart](https://docs.dbos.dev/quickstart) to install this open-source library and connect it to a Postgres database. |
| 155 | +Then, check out the [programming guide](https://docs.dbos.dev/python/programming-guide) to learn how to build with durable workflows and queues. |
| 156 | + |
| 157 | +## Documentation |
| 158 | + |
| 159 | +[https://docs.dbos.dev](https://docs.dbos.dev) |
| 160 | + |
| 161 | +## Examples |
| 162 | + |
| 163 | +[https://docs.dbos.dev/examples](https://docs.dbos.dev/examples) |
| 164 | + |
| 165 | +## DBOS vs. Other Systems |
| 166 | + |
| 167 | +<details><summary><strong>DBOS vs. Temporal</strong></summary> |
| 168 | + |
| 169 | +#### |
| 170 | + |
| 171 | +Both DBOS and Temporal provide durable execution, but DBOS is implemented in a lightweight Postgres-backed library whereas Temporal is implemented in an externally orchestrated server. |
| 172 | + |
| 173 | +You can add DBOS to your program by installing this open-source library, connecting it to Postgres, and annotating workflows and steps. |
| 174 | +By contrast, to add Temporal to your program, you must rearchitect your program to move your workflows and steps (activities) to a Temporal worker, configure a Temporal server to orchestrate those workflows, and access your workflows only through a Temporal client. |
| 175 | +[This blog post](https://www.dbos.dev/blog/durable-execution-coding-comparison) makes the comparison in more detail. |
| 176 | + |
| 177 | +**When to use DBOS:** You need to add durable workflows to your applications with minimal rearchitecting, or you are using Postgres. |
| 178 | + |
| 179 | +**When to use Temporal:** You don't want to add Postgres to your stack, or you need a language DBOS doesn't support yet. |
| 180 | + |
| 181 | +</details> |
| 182 | + |
| 183 | +<details><summary><strong>DBOS vs. Airflow</strong></summary> |
| 184 | + |
| 185 | +#### |
| 186 | + |
| 187 | +DBOS and Airflow both provide workflow abstractions. |
| 188 | +Airflow is targeted at data science use cases, providing many out-of-the-box connectors but requiring workflows be written as explicit DAGs and externally orchestrating them from an Airflow cluster. |
| 189 | +Airflow is designed for batch operations and does not provide good performance for streaming or real-time use cases. |
| 190 | +DBOS is general-purpose, but is often used for data pipelines, allowing developers to write workflows as code and requiring no infrastructure except Postgres. |
| 191 | + |
| 192 | +**When to use DBOS:** You need the flexibility of writing workflows as code, or you need higher performance than Airflow is capable of (particularly for streaming or real-time use cases). |
| 193 | + |
| 194 | +**When to use Airflow:** You need Airflow's ecosystem of connectors. |
| 195 | + |
| 196 | +</details> |
| 197 | + |
| 198 | +<details><summary><strong>DBOS vs. Celery/BullMQ</strong></summary> |
| 199 | + |
| 200 | +#### |
| 201 | + |
| 202 | +DBOS provides a similar queue abstraction to dedicated queueing systems like Celery or BullMQ: you can declare queues, submit tasks to them, and control their flow with concurrency limits, rate limits, timeouts, prioritization, etc. |
| 203 | +However, DBOS queues are **durable and Postgres-backed** and integrate with durable workflows. |
| 204 | +For example, in DBOS you can write a durable workflow that enqueues a thousand tasks and waits for their results. |
| 205 | +DBOS checkpoints the workflow and each of its tasks in Postgres, guaranteeing that even if failures or interruptions occur, the tasks will complete and the workflow will collect their results. |
| 206 | +By contrast, Celery/BullMQ are Redis-backed and don't provide workflows, so they provide fewer guarantees but better performance. |
| 207 | + |
| 208 | +**When to use DBOS:** You need the reliability of enqueueing tasks from durable workflows. |
| 209 | + |
| 210 | +**When to use Celery/BullMQ**: You don't need durability, or you need very high throughput beyond what your Postgres server can support. |
| 211 | +</details> |
| 212 | + |
| 213 | +## Community |
| 214 | + |
| 215 | +If you want to ask questions or hang out with the community, join us on [Discord](https://discord.gg/fMwQjeW5zg)! |
| 216 | +If you see a bug or have a feature request, don't hesitate to open an issue here on GitHub. |
| 217 | +If you're interested in contributing, check out our [contributions guide](./CONTRIBUTING.md). |
34 | 218 |
|
35 | | -implementation("dev.dbos:transact:1.0-SNAPSHOT") |
36 | | -implementation("ch.qos.logback:logback-classic:1.5.6") |
37 | 219 |
|
38 | | -Annotations @Workflow, @Transaction, @Step need to be on implementation class methods. |
39 | 220 |
|
40 | 221 |
|
41 | 222 |
|
|
0 commit comments