Technologies Used:
- Language: Java
- Build Tool: Maven
- Framework: Temporal
- Infrastructure: Docker compose
This project contains two sample applications using Temporal to orchestrate workflows:
- one is a banking application
- the other is an e-commerce order processing application
Please note that this applications do over simplify functional features that real application should provide. There are only a pretext to demonstrate Temporal.io features.
The project also includes configurations to run temporal in a Docker environment. (see docker-compose.yml in the src/main/docker directory).
To run the temporal server, you can use the provided docker-compose configuration:
cd src/main/docker
docker-compose up- The Temporal server will start on
localhost:7233. - The Web UI will be available at http://localhost:8088. You can use the Web UI to monitor the workflows and activities.
-
Workflows and Activities:
BankingWorkflowImpl: Implementation of the banking workflow, managing deposit, withdrawal, and balance checking.BankingActivitiesImpl: Implementation of the activities associated with the banking workflow.
-
Workers:
BankTransferWorker: Starts a Temporal worker to execute banking workflows and activities.
-
Starter:
BankTransferStarter: Starts a banking workflow with specific parameters (account ID, initial balance, deposit amount, withdrawal amount).
-
workflow description
The workflow manages the transfer of money between two bank accounts. Here are the main steps of the workflow:
Debit the Account: Debits the specified amount from the source account. If the activity fails (insufficient funds), the workflow stops.Credit the Account: Credits the specified amount to the destination account. If the activity fails (e.g., account blocked), handles the error and refunds the source account.
Error Handling:
- If funds are insufficient, logs the error and throws an exception.
- If the account is blocked, logs the error, refunds the source account, and throws an exception.
- The workflow uses retry options for the activities, with maximum attempts and backoff intervals configured.
- Ensure temporal server is started:
- Start the Worker:
- Run the
BankTransferWorkerclass to start a worker that executes banking workflows and activities.
- Run the
- Start the Starter:
- Run the
BankTransferStarterclass to start a banking workflow. - The workflow will execute the steps of the banking workflow.
- Run the
-
Workflows and Activities:
OrderWorkflowImpl: Implementation of the order workflow, managing stock reservation, payment, stock checking, shipping, and cancellations.OrderActivitiesImpl: Implementation of the activities associated with the order workflow.
-
Workers:
ECommerceWorker: Starts a Temporal worker to execute order workflows and activities.
-
Starter:
ECommerceStarter: Starts an order workflow with specific parameters (order ID, product ID, quantity).
-
Stock Notification:
StockAvailableNotifier: Notifies the running workflow that stock is available by searching for runningOrderWorkflowinstances.- This code also shows how to search for workflow instances:
- Ensure temporal server is started:
- Start the Worker:
- Run the
ECommerceWorkerclass to start a worker that executes order workflows and activities.
- Run the
- Start the Starter:
- Run the
ECommerceStarterclass to start an order workflow. - The workflow will execute the steps of the order processing workflow.
- You can also, if necessary, run the
StockAvailableNotifierto notify the workflow that stock is available.
- Run the
- Run the tests in the
src/testdirectory to test the workflows.