Spring状态机错误处理不起作用 [英] Spring State Machine Error Handling not working

查看:1037
本文介绍了Spring状态机错误处理不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我完成了所有用于错误处理的设置

I did all the setup for error handling

@PostConstruct
public void addStateMachineInterceptor() {
    stateMachine.getStateMachineAccessor().withRegion().addStateMachineInterceptor(interceptor);
    stateMachine.getStateMachineAccessor().doWithRegion(errorinterceptor);
}

创建了拦截器来处理错误:

created interceptor to handle error:

@Service
public class OrderStateMachineFunction<T> implements StateMachineFunction<StateMachineAccess<String, String>> {
    @Override
    public void apply(StateMachineAccess<String, String> stringStringStateMachineAccess) {
        stringStringStateMachineAccess.addStateMachineInterceptor(
                new StateMachineInterceptorAdapter<String, String>() {
                    @Override
                    public Exception stateMachineError(StateMachine<String, String> stateMachine,
                                                       Exception exception) {
                        // return null indicating handled error
                        return exception;
                    }
                });
    }
}

但是当我们从动作中抛出异常时,我看不到调用进入OrderStateMachineFunction.

But I can't see the call going into OrderStateMachineFunction, when we throw the exception from the action.

然后,该状态机表现出某种有线方式,就像它在this.stateMachine.sendEvent(eventData);之后停止调用preStateChange方法一样.在您从操作中引发异常之后,状态机似乎崩溃了.

And after that state machine behave some wired way, like it stops calling preStateChange method after this.stateMachine.sendEvent(eventData);. It seems state machine breaks down after you throw the exception from the action.

    @Service
    public class OrderStateMachineInterceptor extends StateMachineInterceptorAdapter {

        @Override
        public void preStateChange(State newState, Message message, Transition transition, StateMachine stateMachine) {
            System.out.println("Manish");
    }
}

尝试了几下之后,我发现如果我对resetStateMachine进行注释,它会按预期工作,但是如果没有这样做,我将无法通知当前状态到状态机:

After trying few bit, I have seen that if I comment the resetStateMachine, it works as expected, but without that I am not able to inform the currentstate to state machine:

    public boolean fireEvent(Object data, String previousState, String event) {
        Message<String> eventData = MessageBuilder.withPayload(event)
                .setHeader(DATA_KEY, data)
                .build();
        this.stateMachine.stop();

//        this.stateMachine
//                .getStateMachineAccessor()
//                .withRegion()
//                .resetStateMachine(new DefaultStateMachineContext(previousState, event, eventData.getHeaders(), null));

        this.stateMachine.start();
        return this.stateMachine.sendEvent(eventData);
    }

推荐答案

不确定是否仍需要此功能.但是我碰到了类似的问题.我想将异常从状态机传播到调用方.我实现了StateMachineInterceptor.在状态机转换函数中,我正在设置:

Not sure if you still need this. But I bumped into similar issue. I wanted to propagate exception from state machine to the caller. I implemented StateMachineInterceptor. And inside the state machine transition functions I am setting:

   try
        {
            ..
        }
        catch (WhateverException e)
        {
            stateMachine.setStateMachineError(e);
            throw e;
        }

然后在拦截器的stateMachineError方法中,我在extendedState映射中添加了异常:

Then inside the interceptor's stateMachineError method, I have added the Exception in the extendedState map:

public Exception stateMachineError(StateMachine<States, Events> stateMachine, Exception exception)
{ 
stateMachine.getExtendedState().getVariables().put("ERROR", exception);
logger.error("StateMachineError", exception);
return exception;
}

在resetStateMachine内部,我已将拦截器添加到状态机中.

Inside resetStateMachine I have added the interceptor to the statemachine.

a.addStateMachineInterceptor(new LoggingStateMachineInterceptor());

然后,当我调用sendEvent方法时,我正在这样做:

Then when I am calling the sendEvent method, I am doing this:

 if (stateMachine.hasStateMachineError())
        {
            throw (Exception) svtStateMachine.getExtendedState().getVariables().get("ERROR");
        }

这会将WhateverException权限返回给调用方.在我的情况下,这是一个RestController.

This is returning the WhateverException right to the caller. Which in my case is a RestController.

这篇关于Spring状态机错误处理不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆