Spring状态机错误处理无法正常工作

时间:2016-06-16 10:17:38

标签: java spring spring-boot spring-statemachine

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

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

创建拦截器来处理错误:

@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。

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

    @Service
    public class OrderStateMachineInterceptor extends StateMachineInterceptorAdapter {

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

尝试了几下之后,我看到如果我对resetStateMachine发表评论,它会按预期工作,但如果没有,我无法将当前状态通知状态机:

    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);
    }

1 个答案:

答案 0 :(得分:1)

不确定你是否仍然需要这个。但我碰到了类似的问题。我想将异常从状态机传播到调用者。我实现了StateMachineInterceptor。在我设置的状态机转换功能中:

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

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

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

在resetStateMachine中我已经将拦截器添加到了statemachine。

a.addStateMachineInterceptor(new LoggingStateMachineInterceptor());

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

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

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