The Trigger interface also gives the ability to listen for TriggerFailedEvent and TriggerExpiredEvent directly in the trigger, without the need to filter:
@BeanPersistentTask<String>myTask(){returnnewPersistentTask<>(){@Overridepublicvoidaccept(@NullableStringstate){// any code}/** * Callback handler which is invoked once <b>after</b>: * <ul> * <li> if the trigger is finally failed * <li> or the trigger is abandoned * </ul> * <br> * This method is not invoked for expired triggers waiting for an signal. * * @param state the state, could be <code>null</code> if the state could be parsed * @param e the exception reason - could also be a {@link FailTaskNoRetryException} * @see <a href="https://spring-persistent-task.sterl.org/failed-spring-triggers/">Failed trigger</a> */publicvoidafterTriggerFailed(Stringstate,Exceptione){// custom error handler}}}
Each trigger goes through a trigger lifecycle with the corresponding events. One of these events, TriggerFailedEvent, allows you to hook into a trigger’s error handling.
Info
Events are fired for all triggers, so a filter is needed to ensure the event corresponds to the current trigger.
Events are fired even if a trigger will retry; a check is needed to determine whether the handler should only execute when no retries are planned.
@TransactionalEventListener(phase=TransactionPhase.AFTER_COMMIT)voidonTriggerFailed(TriggerFailedEventfailed){if(TASK_NAME.equals(failed.key().getTaskName())&&failed.isDone()){// trigger will not retry anymore}}
@TransactionalEventListener(phase=TransactionPhase.BEFORE_COMMIT)voidonTriggerFailed(TriggerFailedEventfailed){if(TASK_NAME.equals(failed.key().getTaskName())&&failed.isDone()){// trigger will not retry anymore}}