Demos various aspects of Temporal using the Java SDK.
A Typescript SDK version of this example is also available.
The sample is configured by default to connect to a local Temporal Server running on localhost:7233.
To instead connect to Temporal Cloud, set the following environment variables, replacing them with your own Temporal Cloud credentials:
TEMPORAL_ADDRESS=testnamespace.sdvdw.tmprl.cloud:7233
TEMPORAL_NAMESPACE=testnamespace.sdvdw
TEMPORAL_CERT_PATH="/path/to/file.pem"
TEMPORAL_KEY_PATH="/path/to/file.key"
(optional) set a task queue name
export TEMPORAL_MONEYTRANSFER_TASKQUEUE="MoneyTransferJava"
Note: Use a Java 18 SDK.
Start a worker:
ENCRYPT_PAYLOADS=true ./gradlew -q execute -PmainClass=io.temporal.samples.moneytransfer.AccountTransferWorker --console=plain
Run the money transfer web UI:
ENCRYPT_PAYLOADS=true ./gradlew -q execute -PmainClass=io.temporal.samples.moneytransfer.web.WebServer --console=plain
Then navigate to http://localhost:7070/
A dropdown menu simulates the following scenarios
- The transfer will run to completion
The transfer will pause and wait for approval. If the user doesn't approve the transfer within a set time, the workflow will fail.
Approve a transfer using Signals
# where TRANSFER-EZF-249 is the workflowId
./gradlew -q execute -PmainClass=io.temporal.samples.moneytransfer.TransferApprover -Parg=TRANSFER-XXX-XXX
You can also do this through the temporal
cli:
temporal workflow signal \
--env prod \
--query 'WorkflowId="TRANSFER-XXX-XXX"' \
--name approveTransfer \
--reason 'approving transfer'
Approve a transfer using Updates
You can do this through the temporal
cli:
temporal workflow update \
--env prod \
--workflow-id TRANSFER-XXX-XXX \
--name approveTransferUpdate
The workflow's Update function has a validator. It will reject an Update if:
- The transfer isn't waiting for approval
- The transfer has already been approved
Comment out the RuntimeException in the workflow code (AccountTransferWorkflowImpl.java
) and restart the worker to fix the 'bug'.
Will introduce artifical delays in the withdraw
activity's API calls. This will cause activity retries. After 5 retries, the delay will be removed and the workflow will proceed.
Introduces an unrecoverable failure in the deposit
activity (invalid account). The workflow will fail after running compensation activities (undoWithdraw
).
Creates a Schedule that will run a set of workflows on a cadence.
Produces a schedule ID, which you can inspect in the Temporal UI's "Schedules" menu.
temporal workflow list --env prod -q 'ExecutionStatus="Failed" OR ExecutionStatus="Terminated"'
Remove the ENCRYPT_PAYLOADS
variable in each command to run without encryption.
You can decrypt these payloads in Temporal Cloud's UI/cli using the codec server: https://codec.tmprl-demo.cloud
(source). Ensure you switch on "Pass the user access token with your endpoint". Note: The codec server is only compatible with workflows running in Temporal Cloud.
Example command (run from root directory)
./gradlew -q execute -PmainClass=io.temporal.samples.moneytransfer.Replayer \
-Parg=../workflowHistories/human-in-loop-approved.json
Introduce a non-determinism error by adding Workflow.Sleep or re-arranging activity executions:
error=io.temporal.worker.NonDeterministicException:
Failure handling event 15 of type 'EVENT_TYPE_ACTIVITY_TASK_SCHEDULED' during replay.
No command scheduled that corresponds to event_id: 15
Note: This replayer doesn't work with histories using ENCRYPT_PAYLOADS=true