Search in sources :

Example 1 with Instruction

use of io.seata.saga.proctrl.Instruction in project seata by seata.

the class DefaultRouterHandler method route.

@Override
public void route(ProcessContext context) throws FrameworkException {
    try {
        ProcessType processType = matchProcessType(context);
        if (processType == null) {
            if (LOGGER.isWarnEnabled()) {
                LOGGER.warn("Process type not found, context= {}", context);
            }
            throw new FrameworkException(FrameworkErrorCode.ProcessTypeNotFound);
        }
        ProcessRouter processRouter = processRouters.get(processType.getCode());
        if (processRouter == null) {
            LOGGER.error("Cannot find process router by type {}, context = {}", processType.getCode(), context);
            throw new FrameworkException(FrameworkErrorCode.ProcessRouterNotFound);
        }
        Instruction instruction = processRouter.route(context);
        if (instruction == null) {
            LOGGER.info("route instruction is null, process end");
        } else {
            context.setInstruction(instruction);
            eventPublisher.publish(context);
        }
    } catch (FrameworkException e) {
        throw e;
    } catch (Exception ex) {
        throw new FrameworkException(ex, ex.getMessage(), FrameworkErrorCode.UnknownAppError);
    }
}
Also used : ProcessType(io.seata.saga.proctrl.ProcessType) FrameworkException(io.seata.common.exception.FrameworkException) ProcessRouter(io.seata.saga.proctrl.ProcessRouter) Instruction(io.seata.saga.proctrl.Instruction) FrameworkException(io.seata.common.exception.FrameworkException)

Example 2 with Instruction

use of io.seata.saga.proctrl.Instruction in project seata by seata.

the class StateMachineProcessRouter method route.

@Override
public Instruction route(ProcessContext context) throws FrameworkException {
    StateInstruction stateInstruction = context.getInstruction(StateInstruction.class);
    State state;
    if (stateInstruction.getTemporaryState() != null) {
        state = stateInstruction.getTemporaryState();
        stateInstruction.setTemporaryState(null);
    } else {
        StateMachineConfig stateMachineConfig = (StateMachineConfig) context.getVariable(DomainConstants.VAR_NAME_STATEMACHINE_CONFIG);
        StateMachine stateMachine = stateMachineConfig.getStateMachineRepository().getStateMachine(stateInstruction.getStateMachineName(), stateInstruction.getTenantId());
        state = stateMachine.getStates().get(stateInstruction.getStateName());
    }
    String stateType = state.getType();
    StateRouter router = stateRouters.get(stateType);
    Instruction instruction = null;
    List<StateRouterInterceptor> interceptors = null;
    if (router instanceof InterceptableStateRouter) {
        interceptors = ((InterceptableStateRouter) router).getInterceptors();
    }
    List<StateRouterInterceptor> executedInterceptors = null;
    Exception exception = null;
    try {
        if (CollectionUtils.isNotEmpty(interceptors)) {
            executedInterceptors = new ArrayList<>(interceptors.size());
            for (StateRouterInterceptor interceptor : interceptors) {
                executedInterceptors.add(interceptor);
                interceptor.preRoute(context, state);
            }
        }
        instruction = router.route(context, state);
    } catch (Exception e) {
        exception = e;
        throw e;
    } finally {
        if (CollectionUtils.isNotEmpty(executedInterceptors)) {
            for (int i = executedInterceptors.size() - 1; i >= 0; i--) {
                StateRouterInterceptor interceptor = executedInterceptors.get(i);
                interceptor.postRoute(context, state, instruction, exception);
            }
        }
        // if 'Succeed' or 'Fail' State did not configured, we must end the state machine
        if (instruction == null && !stateInstruction.isEnd()) {
            EngineUtils.endStateMachine(context);
        }
    }
    return instruction;
}
Also used : TaskStateRouter(io.seata.saga.engine.pcext.routers.TaskStateRouter) EndStateRouter(io.seata.saga.engine.pcext.routers.EndStateRouter) StateMachine(io.seata.saga.statelang.domain.StateMachine) Instruction(io.seata.saga.proctrl.Instruction) FrameworkException(io.seata.common.exception.FrameworkException) State(io.seata.saga.statelang.domain.State) StateMachineConfig(io.seata.saga.engine.StateMachineConfig)

Aggregations

FrameworkException (io.seata.common.exception.FrameworkException)2 Instruction (io.seata.saga.proctrl.Instruction)2 StateMachineConfig (io.seata.saga.engine.StateMachineConfig)1 EndStateRouter (io.seata.saga.engine.pcext.routers.EndStateRouter)1 TaskStateRouter (io.seata.saga.engine.pcext.routers.TaskStateRouter)1 ProcessRouter (io.seata.saga.proctrl.ProcessRouter)1 ProcessType (io.seata.saga.proctrl.ProcessType)1 State (io.seata.saga.statelang.domain.State)1 StateMachine (io.seata.saga.statelang.domain.StateMachine)1