This example shows a process waiting for human approval and resuming with a new engine after the original engine's workers have stopped. It uses Spring Boot, PostgreSQL, and two TBBPM definitions:
| Process | Behavior |
|---|---|
durable.sample.order |
Timer → payment Effect → approval Wait → result |
durable.sample.publication |
prepare draft → wait for approval → delay publication → publish |
The complete runnable path is the test suite below. Starting DurableSampleApplication alone starts the engine;
application code must still create a Run and submit its approval result.
Prerequisites: JDK 17, 21, or 25, and either Docker or a dedicated disposable PostgreSQL database.
The test suite starts the application, creates Runs, submits approval results, and checks recovery. Run it from the repository root:
./mvnw test \
-pl examples/spring-boot-durable-postgresql \
-Pexamples -am \
-Dtest=DurableOutboxExtensionTest,DurableSampleApplicationTest,DurableSampleDatabaseSelectionTest \
-Dsurefire.failIfNoSpecifiedTests=falseWhen Docker is available, Testcontainers starts an isolated PostgreSQL instance. Alternatively, set all three variables before running the same command:
export COMPILEFLOW_DURABLE_EXAMPLE_TEST_POSTGRES_URL=jdbc:postgresql://localhost:5432/compileflow_durable_example
export COMPILEFLOW_DURABLE_EXAMPLE_TEST_POSTGRES_USERNAME=compileflow
export COMPILEFLOW_DURABLE_EXAMPLE_TEST_POSTGRES_PASSWORD=change-meDurableSampleApplicationTest uses the dedicated database above. It applies migrations and writes Durable state and publication results. If no external database configuration is supplied and Docker is unavailable, these five tests are skipped. When using an external database, supply all three settings; an explicitly empty password is allowed. Incomplete configuration or a non-PostgreSQL JDBC URL fails rather than falling back to Docker. The three tests in DurableOutboxExtensionTest and the eleven tests in DurableSampleDatabaseSelectionTest can run without a database.
Check all three test-class summaries. The two database-independent classes should report three and eleven passing tests respectively;
DurableSampleApplicationTest should report the following. The overall Maven exit status alone does not prove that
the database tests ran:
Tests run: 5, Failures: 0, Errors: 0, Skipped: 0
The tests close their application contexts, including workers and connection pools, before stopping their PostgreSQL container. If you supplied an external database, it remains running and its tables and example data remain there. No application process needs to be left running after this command.
DurableSampleApplicationTest verifies:
- The order process's Timer, Effect, Wait, and successful completion.
- Approval after replacing the original engine. Recovery uses the saved definition without calling the version source. If the first publication reply is lost, the second call uses the same Effect ID and the application table retains one result. Repeating the approval leaves the output unchanged.
- Rejection completes without publishing the draft.
- The publication service reuses an existing result after it is recreated and rejects the same Effect ID with a different request.
- The application validates the existing Durable schema with automatic migrations and workers disabled.
The recovery test stops the original engine's workers and uses a new engine and application context in the same JVM. It does not terminate or restart the JVM; the lost-reply behavior is configured by the test.
Apply example/publication.sql through your application's migrations. Configure the application DataSource and initialize Durable as described in Add the starter.
The integration test applies this schema separately from the Durable migrations. Application startup leaves the example schema unchanged.
The review flow keeps a draft while it waits for a human decision. Rejection ends the flow; approval waits briefly and then publishes that same draft.
durable-publication.bpm uses PublicationService to prepare a draft from submitted text and publish it after approval. Both steps are Effects. The draft step saves the text with a Draft: prefix, and the Run retains that result while it waits for approval.
publicationService is a Spring bean that uses the application's DataSource. Durable looks it up by the bean name and type declared in the flow.
The Outbox adapter uses the extension framework: RecordingOutboxSink declares @ExtensionRealization and is listed in
src/main/resources/META-INF/services/com.alibaba.compileflow.common.extension.Extension. Each engine constructs its own instance. The integration tests read notifications by Run ID from the application's DemoWaitNotifications fixture.
The caller supplies submittedText. The Outbox sink delivers the approval Wait token. Call completeWait(...) with approved=true to schedule publication after a short delay, or approved=false to finish with publicationId=not-published.
ProcessRunId runId = ProcessRunId.random(); // Save this ID before start so a lost reply can be checked.
ProcessRun run = engine.start(
runId, DurableSampleApplication.PUBLICATION_PROCESS,
Map.of("submittedText", "Orders placed by noon ship the same day."));
// waitToken is the WaitToken received through the authenticated review channel.
// Authenticate the reviewer and authorize this Run before accepting its response.
engine.completeWait(runId, waitToken, Map.of("approved", true));
ProcessRunResult result = engine.getRunResult(run.runId());completeWait commits the reviewer's response. Workers run the remaining steps asynchronously, so poll getRunResult at a bounded interval with an application timeout. Stop when a terminal result is returned and handle NotFound separately.
Repeating the same token and completion payload succeeds while the Run and completed Wait remain queryable; a different payload is rejected. Cleanup can remove that completion record while the Run is still active. The token allows completion of one Wait occurrence and must be protected by the application's review channel.
Both actions map metadata="effectId" to their effectId argument. The application table stores one result per Effect ID. On retry, INSERT ... ON CONFLICT DO NOTHING followed by a read returns the saved result. Reusing the ID for a different operation or payload fails.
The publication table is application-owned. In this sample, the insert and subsequent read use JdbcTemplate outside
the Durable Store transaction; the insert is committed independently of the Kernel's Effect result. The primary key
protects the recorded result even if the worker loses the reply. The declared recovery="retry" policy relies on the
service accepting retries under the same Effect ID. This does not make an unrelated external side effect atomic with
the insert. When integrating an external service, require its own idempotency guarantee or choose reconciliation or
manual recovery.
Recovery uses the saved definition and committed execution state. The application must still supply the classes, beans, dependencies, and business types required by its active Runs.
RecordingOutboxSink and DemoWaitNotifications provide an in-memory notification fixture for the tests. The fixture loses notifications when the JVM exits. DurableOutboxExtensionTest verifies discovery through the standard service declaration and framework construction, per-Run notification lookup, event deduplication across separate engine instances, and rejection of malformed Wait notifications without requiring PostgreSQL.
A production sink must return only after an authenticated destination has durably accepted the event. Its review channel must protect Wait tokens, authorize reviewers, and deduplicate repeated delivery by eventId. Replace the in-memory fixture with that channel before using the example for production delivery, and keep credentials out of source control.
The sample uses short worker polling, lease, maintenance, and retry intervals for demonstration, not as production tuning recommendations.
DemoChargeAction records order IDs in memory and returns a synthetic receipt; it does not contact a payment gateway. The order process's payment Effect uses the default manual recovery policy. In a real integration, an unknown payment result must be investigated with the payment system.