🥝Component event
LiteFlow supports component event. There are 2 events supported: success event and failure event.
# Success event
If you override the onSuccess
method in the component, this method will be called back when the component succeeds.
@LiteflowComponent("a")
public class ACmp extends NodeComponent {
@Override
public void process() {
DefaultContext context = this.getContextBean(DefaultContext.class);
//do your biz
}
@Override
public void onSuccess() throws Exception {
DefaultContext context = this.getContextBean(DefaultContext.class);
//do your biz
}
}
In the success event, you can get the context in the same way.
# Failure event
If you override the onError
method in the component, the component will call back this method when an exception occurs.
@Component("d")
public class DCmp extends NodeComponent {
@Override
public void process() throws Exception{
//mock throw exception
throw new NullPointerException();
}
@Override
public void onError() throws Exception {
DefaultContext context = this.getContextBean(DefaultContext.class);
//do your biz
}
}
# Note 1
After the onError
method is executed, because the main chain throws an exception, the entire chain is still in a failed state. Errors thrown by the main process are still recorded in LiteFlowResponse.
# Note 2
If the onError
method itself throws an error, then the final error thrown is the error in the main chain, and the exception generated by the onError
method will be printed, but not thrown. for example:
@Component("d")
public class DCmp extends NodeComponent {
@Override
public void process() throws Exception{
//mock throw exception
throw new NullPointerException();
}
@Override
public void onError() throws Exception {
throw new IllegalAccessException("The error event itself throws an exception");
}
}
Then the exception in the final response will be NullPointerException
instead of IllegalAccessException
, but the IllegalAccessException
exception will be printed.
# Note 2
After the onError
method is executed, will the afterProcess
method still execute (assuming both are implemented)?
Yes, the afterProcess
method will be executed whether or not an error is thrown.