Step 8 of 21 · 3 minutes

Alert the Team

The Scenario

Sometimes batch jobs fail after processing thousands of items. We need automatic notifications when critical jobs fail — not just retries.

The ops team should know immediately, not discover it hours later.

The Challenge

How do we trigger error handling workflows when a job fails permanently?

The Solution

Use onFailure() to chain a notification job that runs only when the parent fails:


jobScheduler
    .startBatch(this::generateAllStatements)
    .onFailure(this::notifyOpsTeam);

The ops team gets a Slack/email notification immediately when the job fails. You can combine continueWith() and onFailure() for complete workflows!

Bonus: inside the notification job, use context.getAwaitedJob() to retrieve the failed batch job and include the total statement count in the alert.

Read the documentation →

Try It Yourself

Watch the summary job fail (it has a 50% failure rate!) and see the notification job trigger automatically. It did not fail? Then no worries, JobRunr moved the onFailure Job to NOT_APPLICABLE state.
You need to log in to perform write operations. You can still view the code solution and dashboard.