Skip to main content
Temporal Java SDK

Simulate failures

~5 minutesTemporal beginnerHands-on tutorial
  1. Understand the application
  2. Run the application
  3. Simulate failures

Despite your best efforts, sometimes things go wrong - a network glitch, a server going offline, or a new bug in your code. One of Temporal's most important features is its ability to maintain Workflow state when something fails. Simulate some failures and see how Temporal responds.

Recover from a server crash

Unlike many modern applications that require complex processes and external databases to handle failure, Temporal automatically preserves the state of your Workflow even if the server is down.

  1. Make sure your Worker is stopped before proceeding. Press CTRL+C in the Worker terminal.
  2. Verify the Workflow is running in the Web UI. If finished, restart it with the Maven command.
  3. Shut down the Temporal Server with CTRL+C in the server terminal.
  4. After it stops, restart it with temporal server start-dev and reload the UI.

Your Workflow is still listed and running:

The Workflow still appears in the list

If the Temporal Cluster goes offline, you can pick up where you left off when it comes back online.

Recover from an unknown error in an Activity

This demo application makes a call to an external service in an Activity. If that call fails due to a bug in your code, the Activity produces an error.

To test this out, simulate a bug in the deposit Activity method:

  1. Stop the Worker by pressing CTRL+C in its terminal.
  2. Open AccountActivityImpl and modify the deposit method so activityShouldSucceed is set to false.
  3. Save your changes and switch back to the Worker terminal.
  4. Verify the Workflow is running in the Web UI. If finished, restart it with the Maven command.
  5. Start the Worker again:
mvn clean install \
-Dorg.slf4j.simpleLogger.defaultLogLevel=info 2>/dev/null
mvn compile exec:java \
-Dexec.mainClass="moneytransferapp.MoneyTransferWorker" \
-Dorg.slf4j.simpleLogger.defaultLogLevel=warn

Note that you must restart the Worker every time the code changes. You'll see the Worker complete the withdraw Activity but error on deposit - and keep retrying:

Withdrawing $32 from account 612849675.
[ReferenceId: d3d9bcf0-a897-4326]
Deposit failed
Deposit failed
Deposit failed
Deposit failed

The Workflow keeps retrying using the RetryPolicy defined earlier. View progress in the Web UI:

note

Traditionally, you'd implement timeout and retry logic in your service code itself - repetitive and error-prone. With Temporal, you specify timeout configurations in the Workflow code as Activity options.

Your Workflow is running, but only the withdraw Activity has succeeded. In any other application, the whole process would be abandoned and rolled back. With Temporal, you can debug and resolve the issue while the Workflow is running.

Pretend that you found a fix. Switch activityShouldSucceed back to true and save your changes.

How can you update a Workflow that's already halfway complete? You restart the Worker. Cancel the Worker with CTRL+C, then restart it:

mvn clean install \
-Dorg.slf4j.simpleLogger.defaultLogLevel=info 2>/dev/null
mvn compile exec:java \
-Dexec.mainClass="moneytransferapp.MoneyTransferWorker" \
-Dorg.slf4j.simpleLogger.defaultLogLevel=warn

On the next scheduled attempt, the Worker picks up right where the Workflow was failing and successfully executes the newly compiled deposit Activity:

Depositing $32 into account 872878204.
[ReferenceId: d3d9bcf0-a897-4326]
[d3d9bcf0-a897-4326] Transaction succeeded.

Visit the Web UI again and you'll see the Workflow has completed.

You have just fixed a bug in a running application without losing the state of the Workflow or restarting the transaction.

Conclusion

You now know how to run a Temporal Workflow and understand some of the value Temporal offers. You explored Workflows and Activities, you started a Workflow Execution, and you ran a Worker. You also saw how Temporal recovers from failures and retries Activities.

Key advantages Temporal offers:

  1. Temporal gives you full visibility in the state of your Workflow and code execution.
  2. Temporal maintains the state of your Workflow, even through server outages and errors.
  3. Temporal lets you time out and retry Activity code using options that exist outside your business logic.
  4. Temporal enables you to perform "live debugging" of your business logic while the Workflow is running.

Further exploration

Try the following before moving on:

  • Change the Retry Policy so it only retries 1 time. Does the Workflow place the money back into the original account?

Get notified when we launch new educational content

New courses, tutorials, and learning resources - straight to your inbox.

Subscribe
Feedback