diff --git a/compatible/src/main/java/io/seata/saga/statelang/domain/State.java b/compatible/src/main/java/io/seata/saga/statelang/domain/State.java index 348445389a5..ff1a7b5e960 100644 --- a/compatible/src/main/java/io/seata/saga/statelang/domain/State.java +++ b/compatible/src/main/java/io/seata/saga/statelang/domain/State.java @@ -43,7 +43,7 @@ public interface State { * * @return the state type */ - String getType(); + StateType getType(); /** * next state name diff --git a/compatible/src/main/java/io/seata/saga/statelang/domain/StateInstance.java b/compatible/src/main/java/io/seata/saga/statelang/domain/StateInstance.java index ad11bdf7877..cf8f8099b42 100644 --- a/compatible/src/main/java/io/seata/saga/statelang/domain/StateInstance.java +++ b/compatible/src/main/java/io/seata/saga/statelang/domain/StateInstance.java @@ -72,14 +72,14 @@ public interface StateInstance { * * @return state instance type */ - String getType(); + StateType getType(); /** * set type * * @param type state instance type */ - void setType(String type); + void setType(StateType type); /** * get service name diff --git a/compatible/src/main/java/io/seata/saga/statelang/domain/StateType.java b/compatible/src/main/java/io/seata/saga/statelang/domain/StateType.java new file mode 100644 index 00000000000..22a9856efb4 --- /dev/null +++ b/compatible/src/main/java/io/seata/saga/statelang/domain/StateType.java @@ -0,0 +1,145 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.seata.saga.statelang.domain; + +/** + * StateType + */ +@Deprecated +public enum StateType { + + /** + * ServiceTask State + */ + SERVICE_TASK("ServiceTask"), + + /** + * Choice State + */ + CHOICE("Choice"), + + /** + * Fail State + */ + FAIL("Fail"), + + /** + * Succeed State + */ + SUCCEED("Succeed"), + + /** + * CompensationTrigger State + */ + COMPENSATION_TRIGGER("CompensationTrigger"), + + /** + * SubStateMachine State + */ + SUB_STATE_MACHINE("SubStateMachine"), + + /** + * CompensateSubMachine State + */ + SUB_MACHINE_COMPENSATION("CompensateSubMachine"), + + /** + * ScriptTask State + */ + SCRIPT_TASK("ScriptTask"), + + /** + * LoopStart State + */ + LOOP_START("LoopStart"); + + + private String value; + + StateType(String value) { + this.value = value; + } + + public String getValue() { + return value; + } + + public static StateType getStateType(String value) { + for (StateType stateType : values()) { + if (stateType.getValue().equalsIgnoreCase(value)) { + return stateType; + } + } + + throw new IllegalArgumentException("Unknown StateType[" + value + "]"); + } + + + public static StateType wrap(org.apache.seata.saga.statelang.domain.StateType target) { + if (target == null) { + return null; + } + switch (target) { + case SERVICE_TASK: + return SERVICE_TASK; + case CHOICE: + return CHOICE; + case FAIL: + return FAIL; + case SUCCEED: + return SUCCEED; + case COMPENSATION_TRIGGER: + return COMPENSATION_TRIGGER; + case SUB_STATE_MACHINE: + return SUB_STATE_MACHINE; + case SUB_MACHINE_COMPENSATION: + return SUB_MACHINE_COMPENSATION; + case SCRIPT_TASK: + return SCRIPT_TASK; + case LOOP_START: + return LOOP_START; + default: + throw new IllegalArgumentException("Cannot convert " + target.name()); + } + } + + public org.apache.seata.saga.statelang.domain.StateType unwrap() { + switch (this) { + case SERVICE_TASK: + return org.apache.seata.saga.statelang.domain.StateType.SERVICE_TASK; + case CHOICE: + return org.apache.seata.saga.statelang.domain.StateType.CHOICE; + case FAIL: + return org.apache.seata.saga.statelang.domain.StateType.FAIL; + case SUCCEED: + return org.apache.seata.saga.statelang.domain.StateType.SUCCEED; + case COMPENSATION_TRIGGER: + return org.apache.seata.saga.statelang.domain.StateType.COMPENSATION_TRIGGER; + case SUB_STATE_MACHINE: + return org.apache.seata.saga.statelang.domain.StateType.SUB_STATE_MACHINE; + case SUB_MACHINE_COMPENSATION: + return org.apache.seata.saga.statelang.domain.StateType.SUB_MACHINE_COMPENSATION; + case SCRIPT_TASK: + return org.apache.seata.saga.statelang.domain.StateType.SCRIPT_TASK; + case LOOP_START: + return org.apache.seata.saga.statelang.domain.StateType.LOOP_START; + default: + throw new IllegalArgumentException("Cannot convert " + this.name()); + } + } + +} diff --git a/compatible/src/main/java/io/seata/saga/statelang/domain/impl/StateImpl.java b/compatible/src/main/java/io/seata/saga/statelang/domain/impl/StateImpl.java index 29f31b9f68b..3e31881f1d8 100644 --- a/compatible/src/main/java/io/seata/saga/statelang/domain/impl/StateImpl.java +++ b/compatible/src/main/java/io/seata/saga/statelang/domain/impl/StateImpl.java @@ -20,6 +20,7 @@ import io.seata.saga.statelang.domain.State; import io.seata.saga.statelang.domain.StateMachine; +import io.seata.saga.statelang.domain.StateType; /** * The type State. @@ -45,8 +46,8 @@ public String getComment() { } @Override - public String getType() { - return actual.getType(); + public StateType getType() { + return StateType.wrap(actual.getType()); } @Override diff --git a/compatible/src/main/java/io/seata/saga/statelang/domain/impl/StateInstanceImpl.java b/compatible/src/main/java/io/seata/saga/statelang/domain/impl/StateInstanceImpl.java index cfc0f49701e..f29e28ed5ea 100644 --- a/compatible/src/main/java/io/seata/saga/statelang/domain/impl/StateInstanceImpl.java +++ b/compatible/src/main/java/io/seata/saga/statelang/domain/impl/StateInstanceImpl.java @@ -21,6 +21,7 @@ import io.seata.saga.statelang.domain.ExecutionStatus; import io.seata.saga.statelang.domain.StateInstance; import io.seata.saga.statelang.domain.StateMachineInstance; +import io.seata.saga.statelang.domain.StateType; /** * state execution instance @@ -67,13 +68,17 @@ public void setName(String name) { } @Override - public String getType() { - return actual.getType(); + public StateType getType() { + return StateType.wrap(actual.getType()); } @Override - public void setType(String type) { - actual.setType(type); + public void setType(StateType type) { + if (type == null) { + actual.setType(null); + } else { + actual.setType(type.unwrap()); + } } @Override diff --git a/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/config/AbstractStateMachineConfig.java b/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/config/AbstractStateMachineConfig.java index f61c3777554..96446594775 100644 --- a/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/config/AbstractStateMachineConfig.java +++ b/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/config/AbstractStateMachineConfig.java @@ -54,6 +54,7 @@ import org.apache.seata.saga.proctrl.impl.ProcessControllerImpl; import org.apache.seata.saga.proctrl.process.impl.CustomizeBusinessProcessor; import org.apache.seata.saga.statelang.domain.DomainConstants; +import org.apache.seata.saga.statelang.domain.StateType; import javax.script.ScriptEngineManager; import java.io.InputStream; @@ -214,20 +215,8 @@ public void init() throws Exception { } public ProcessControllerImpl createProcessorController(ProcessCtrlEventPublisher eventPublisher) throws Exception { - StateMachineProcessRouter stateMachineProcessRouter = new StateMachineProcessRouter(); - stateMachineProcessRouter.initDefaultStateRouters(); - loadStateRouterInterceptors(stateMachineProcessRouter.getStateRouters()); - - StateMachineProcessHandler stateMachineProcessHandler = new StateMachineProcessHandler(); - stateMachineProcessHandler.initDefaultHandlers(); - loadStateHandlerInterceptors(stateMachineProcessHandler.getStateHandlers()); - - DefaultRouterHandler defaultRouterHandler = new DefaultRouterHandler(); - defaultRouterHandler.setEventPublisher(eventPublisher); - - Map processRouterMap = new HashMap<>(1); - processRouterMap.put(ProcessType.STATE_LANG.getCode(), stateMachineProcessRouter); - defaultRouterHandler.setProcessRouters(processRouterMap); + StateMachineProcessHandler stateMachineProcessHandler = buildStateMachineProcessHandler(); + DefaultRouterHandler defaultRouterHandler = buildDefaultRouterHandler(eventPublisher); CustomizeBusinessProcessor customizeBusinessProcessor = new CustomizeBusinessProcessor(); @@ -245,7 +234,28 @@ public ProcessControllerImpl createProcessorController(ProcessCtrlEventPublisher return processorController; } - public void loadStateHandlerInterceptors(Map stateHandlerMap) { + private StateMachineProcessHandler buildStateMachineProcessHandler() { + StateMachineProcessHandler stateMachineProcessHandler = new StateMachineProcessHandler(); + stateMachineProcessHandler.initDefaultHandlers(); + loadStateHandlerInterceptors(stateMachineProcessHandler.getStateHandlers()); + return stateMachineProcessHandler; + } + + private DefaultRouterHandler buildDefaultRouterHandler(ProcessCtrlEventPublisher eventPublisher) { + DefaultRouterHandler defaultRouterHandler = new DefaultRouterHandler(); + defaultRouterHandler.setEventPublisher(eventPublisher); + + StateMachineProcessRouter stateMachineProcessRouter = new StateMachineProcessRouter(); + stateMachineProcessRouter.initDefaultStateRouters(); + loadStateRouterInterceptors(stateMachineProcessRouter.getStateRouters()); + + Map processRouterMap = new HashMap<>(2); + processRouterMap.put(ProcessType.STATE_LANG.getCode(), stateMachineProcessRouter); + defaultRouterHandler.setProcessRouters(processRouterMap); + return defaultRouterHandler; + } + + public void loadStateHandlerInterceptors(Map stateHandlerMap) { for (StateHandler stateHandler : stateHandlerMap.values()) { if (stateHandler instanceof InterceptableStateHandler) { InterceptableStateHandler interceptableStateHandler = (InterceptableStateHandler) stateHandler; @@ -259,7 +269,7 @@ public void loadStateHandlerInterceptors(Map stateHandlerM } } - public void loadStateRouterInterceptors(Map stateRouterMap) { + public void loadStateRouterInterceptors(Map stateRouterMap) { for (StateRouter stateRouter : stateRouterMap.values()) { if (stateRouter instanceof InterceptableStateRouter) { InterceptableStateRouter interceptableStateRouter = (InterceptableStateRouter) stateRouter; diff --git a/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/impl/ProcessCtrlStateMachineEngine.java b/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/impl/ProcessCtrlStateMachineEngine.java index d18e72f8be8..645fc8e656d 100644 --- a/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/impl/ProcessCtrlStateMachineEngine.java +++ b/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/impl/ProcessCtrlStateMachineEngine.java @@ -38,6 +38,7 @@ import org.apache.seata.saga.proctrl.ProcessContext; import org.apache.seata.saga.proctrl.ProcessType; import org.apache.seata.saga.statelang.domain.DomainConstants; +import org.apache.seata.saga.statelang.domain.StateType; import org.apache.seata.saga.statelang.domain.ExecutionStatus; import org.apache.seata.saga.statelang.domain.State; import org.apache.seata.saga.statelang.domain.StateInstance; @@ -280,7 +281,7 @@ protected StateMachineInstance forwardInternal(String stateMachineInstId, Map stateIn continue; } - if (DomainConstants.STATE_TYPE_SUB_STATE_MACHINE.equals(stateInstance.getType())) { + if (StateType.SUB_STATE_MACHINE.equals(stateInstance.getType())) { StateInstance finalState = stateInstance; diff --git a/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/StateMachineProcessHandler.java b/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/StateMachineProcessHandler.java index 30cdbf22c9d..462d9b1a055 100644 --- a/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/StateMachineProcessHandler.java +++ b/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/StateMachineProcessHandler.java @@ -33,7 +33,7 @@ import org.apache.seata.saga.engine.pcext.handlers.SucceedEndStateHandler; import org.apache.seata.saga.proctrl.ProcessContext; import org.apache.seata.saga.proctrl.handler.ProcessHandler; -import org.apache.seata.saga.statelang.domain.DomainConstants; +import org.apache.seata.saga.statelang.domain.StateType; import org.apache.seata.saga.statelang.domain.State; /** @@ -43,13 +43,13 @@ */ public class StateMachineProcessHandler implements ProcessHandler { - private final Map stateHandlers = new ConcurrentHashMap<>(); + private final Map stateHandlers = new ConcurrentHashMap<>(); @Override public void process(ProcessContext context) throws FrameworkException { StateInstruction instruction = context.getInstruction(StateInstruction.class); State state = instruction.getState(context); - String stateType = state.getType(); + StateType stateType = state.getType(); StateHandler stateHandler = stateHandlers.get(stateType); List interceptors = null; @@ -84,28 +84,27 @@ public void process(ProcessContext context) throws FrameworkException { } public void initDefaultHandlers() { - if (stateHandlers.isEmpty()) { - stateHandlers.put(DomainConstants.STATE_TYPE_SERVICE_TASK, new ServiceTaskStateHandler()); - - stateHandlers.put(DomainConstants.STATE_TYPE_SCRIPT_TASK, new ScriptTaskStateHandler()); - - stateHandlers.put(DomainConstants.STATE_TYPE_SUB_MACHINE_COMPENSATION, new ServiceTaskStateHandler()); + if (!stateHandlers.isEmpty()) { + return; + } - stateHandlers.put(DomainConstants.STATE_TYPE_SUB_STATE_MACHINE, new SubStateMachineHandler()); + stateHandlers.put(StateType.SERVICE_TASK, new ServiceTaskStateHandler()); + stateHandlers.put(StateType.SCRIPT_TASK, new ScriptTaskStateHandler()); + stateHandlers.put(StateType.SUB_MACHINE_COMPENSATION, new ServiceTaskStateHandler()); + stateHandlers.put(StateType.SUB_STATE_MACHINE, new SubStateMachineHandler()); + stateHandlers.put(StateType.CHOICE, new ChoiceStateHandler()); + stateHandlers.put(StateType.SUCCEED, new SucceedEndStateHandler()); + stateHandlers.put(StateType.FAIL, new FailEndStateHandler()); + stateHandlers.put(StateType.COMPENSATION_TRIGGER, new CompensationTriggerStateHandler()); + stateHandlers.put(StateType.LOOP_START, new LoopStartStateHandler()); - stateHandlers.put(DomainConstants.STATE_TYPE_CHOICE, new ChoiceStateHandler()); - stateHandlers.put(DomainConstants.STATE_TYPE_SUCCEED, new SucceedEndStateHandler()); - stateHandlers.put(DomainConstants.STATE_TYPE_FAIL, new FailEndStateHandler()); - stateHandlers.put(DomainConstants.STATE_TYPE_COMPENSATION_TRIGGER, new CompensationTriggerStateHandler()); - stateHandlers.put(DomainConstants.STATE_TYPE_LOOP_START, new LoopStartStateHandler()); - } } - public Map getStateHandlers() { + public Map getStateHandlers() { return stateHandlers; } - public void setStateHandlers(Map stateHandlers) { + public void setStateHandlers(Map stateHandlers) { this.stateHandlers.putAll(stateHandlers); } } diff --git a/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/StateMachineProcessRouter.java b/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/StateMachineProcessRouter.java index e3a4569fa9e..bcb7c238858 100644 --- a/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/StateMachineProcessRouter.java +++ b/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/StateMachineProcessRouter.java @@ -33,6 +33,7 @@ import org.apache.seata.saga.statelang.domain.DomainConstants; import org.apache.seata.saga.statelang.domain.State; import org.apache.seata.saga.statelang.domain.StateMachine; +import org.apache.seata.saga.statelang.domain.StateType; /** * StateMachine ProcessRouter @@ -41,7 +42,7 @@ */ public class StateMachineProcessRouter implements ProcessRouter { - private final Map stateRouters = new ConcurrentHashMap<>(); + private final Map stateRouters = new ConcurrentHashMap<>(); @Override public Instruction route(ProcessContext context) throws FrameworkException { @@ -60,7 +61,7 @@ public Instruction route(ProcessContext context) throws FrameworkException { state = stateMachine.getStates().get(stateInstruction.getStateName()); } - String stateType = state.getType(); + StateType stateType = state.getType(); StateRouter router = stateRouters.get(stateType); @@ -105,26 +106,28 @@ public Instruction route(ProcessContext context) throws FrameworkException { } public void initDefaultStateRouters() { - if (this.stateRouters.isEmpty()) { - TaskStateRouter taskStateRouter = new TaskStateRouter(); - this.stateRouters.put(DomainConstants.STATE_TYPE_SERVICE_TASK, taskStateRouter); - this.stateRouters.put(DomainConstants.STATE_TYPE_SCRIPT_TASK, taskStateRouter); - this.stateRouters.put(DomainConstants.STATE_TYPE_CHOICE, taskStateRouter); - this.stateRouters.put(DomainConstants.STATE_TYPE_COMPENSATION_TRIGGER, taskStateRouter); - this.stateRouters.put(DomainConstants.STATE_TYPE_SUB_STATE_MACHINE, taskStateRouter); - this.stateRouters.put(DomainConstants.STATE_TYPE_SUB_MACHINE_COMPENSATION, taskStateRouter); - this.stateRouters.put(DomainConstants.STATE_TYPE_LOOP_START, taskStateRouter); - - this.stateRouters.put(DomainConstants.STATE_TYPE_SUCCEED, new EndStateRouter()); - this.stateRouters.put(DomainConstants.STATE_TYPE_FAIL, new EndStateRouter()); + if (!stateRouters.isEmpty()) { + return; } + + TaskStateRouter taskStateRouter = new TaskStateRouter(); + stateRouters.put(StateType.SERVICE_TASK, taskStateRouter); + stateRouters.put(StateType.SCRIPT_TASK, taskStateRouter); + stateRouters.put(StateType.CHOICE, taskStateRouter); + stateRouters.put(StateType.COMPENSATION_TRIGGER, taskStateRouter); + stateRouters.put(StateType.SUB_STATE_MACHINE, taskStateRouter); + stateRouters.put(StateType.SUB_MACHINE_COMPENSATION, taskStateRouter); + stateRouters.put(StateType.LOOP_START, taskStateRouter); + + stateRouters.put(StateType.SUCCEED, new EndStateRouter()); + stateRouters.put(StateType.FAIL, new EndStateRouter()); } - public Map getStateRouters() { + public Map getStateRouters() { return stateRouters; } - public void setStateRouters(Map stateRouters) { + public void setStateRouters(Map stateRouters) { this.stateRouters.putAll(stateRouters); } } diff --git a/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/utils/CompensationHolder.java b/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/utils/CompensationHolder.java index d7d19d42a4f..b4d748646ae 100644 --- a/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/utils/CompensationHolder.java +++ b/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/utils/CompensationHolder.java @@ -26,6 +26,7 @@ import org.apache.seata.common.util.CollectionUtils; import org.apache.seata.common.util.StringUtils; import org.apache.seata.saga.engine.exception.EngineExecutionException; +import org.apache.seata.saga.statelang.domain.StateType; import org.apache.seata.saga.engine.utils.ExceptionUtils; import org.apache.seata.saga.proctrl.ProcessContext; import org.apache.seata.saga.statelang.domain.DomainConstants; @@ -125,13 +126,13 @@ private static boolean stateNeedToCompensate(StateInstance stateInstance) { if (stateInstance.isIgnoreStatus()) { return false; } - if (DomainConstants.STATE_TYPE_SUB_STATE_MACHINE.equals(stateInstance.getType())) { + if (StateType.SUB_STATE_MACHINE.equals(stateInstance.getType())) { return (!ExecutionStatus.FA.equals(stateInstance.getStatus())) && (!ExecutionStatus.SU.equals( stateInstance.getCompensationStatus())); } else { - return DomainConstants.STATE_TYPE_SERVICE_TASK.equals(stateInstance.getType()) && !stateInstance + return StateType.SERVICE_TASK.equals(stateInstance.getType()) && !stateInstance .isForCompensation() && (!ExecutionStatus.FA.equals(stateInstance.getStatus())) && (!ExecutionStatus.SU .equals(stateInstance.getCompensationStatus())); } diff --git a/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/utils/LoopTaskUtils.java b/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/utils/LoopTaskUtils.java index e71818f1f1a..f9500eb3337 100644 --- a/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/utils/LoopTaskUtils.java +++ b/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/utils/LoopTaskUtils.java @@ -32,6 +32,7 @@ import org.apache.seata.saga.engine.exception.ForwardInvalidException; import org.apache.seata.saga.engine.expression.ExpressionResolver; import org.apache.seata.saga.engine.pcext.StateInstruction; +import org.apache.seata.saga.statelang.domain.StateType; import org.apache.seata.saga.proctrl.ProcessContext; import org.apache.seata.saga.proctrl.impl.ProcessContextImpl; import org.apache.seata.saga.statelang.domain.DomainConstants; @@ -97,9 +98,9 @@ public static Loop getLoopConfig(ProcessContext context, State currentState) { * @return the boolean */ public static boolean matchLoop(State state) { - return state != null && (DomainConstants.STATE_TYPE_SERVICE_TASK.equals(state.getType()) - || DomainConstants.STATE_TYPE_SCRIPT_TASK.equals(state.getType()) - || DomainConstants.STATE_TYPE_SUB_STATE_MACHINE.equals(state.getType())); + return state != null && (StateType.SERVICE_TASK.equals(state.getType()) + || StateType.SCRIPT_TASK.equals(state.getType()) + || StateType.SUB_STATE_MACHINE.equals(state.getType())); } /** @@ -334,7 +335,7 @@ public static boolean isForSubStateMachineForward(ProcessContext context) { StateInstance lastRetriedStateInstance = LoopTaskUtils.findOutLastRetriedStateInstance( stateMachineInstance, LoopTaskUtils.generateLoopStateName(context, instruction.getStateName())); - if (null != lastRetriedStateInstance && DomainConstants.STATE_TYPE_SUB_STATE_MACHINE.equals( + if (null != lastRetriedStateInstance && StateType.SUB_STATE_MACHINE.getValue().equals( lastRetriedStateInstance.getType()) && !ExecutionStatus.SU.equals( lastRetriedStateInstance.getCompensationStatus())) { @@ -381,7 +382,7 @@ public static String decideCurrentExceptionRoute(List subContext // compensate must be execute State state = stateMachine.getState(next); - if (DomainConstants.STATE_TYPE_COMPENSATION_TRIGGER.equals(state.getType())) { + if (StateType.COMPENSATION_TRIGGER.equals(state.getType())) { route = next; break; } else if (null == route) { diff --git a/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/strategy/impl/DefaultStatusDecisionStrategy.java b/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/strategy/impl/DefaultStatusDecisionStrategy.java index 54b75a3815c..5e8ddd23283 100644 --- a/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/strategy/impl/DefaultStatusDecisionStrategy.java +++ b/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/strategy/impl/DefaultStatusDecisionStrategy.java @@ -21,6 +21,7 @@ import org.apache.seata.common.exception.FrameworkErrorCode; import org.apache.seata.common.util.CollectionUtils; import org.apache.seata.saga.engine.exception.EngineExecutionException; +import org.apache.seata.saga.statelang.domain.StateType; import org.apache.seata.saga.engine.pcext.utils.CompensationHolder; import org.apache.seata.saga.engine.strategy.StatusDecisionStrategy; import org.apache.seata.saga.engine.utils.ExceptionUtils; @@ -110,7 +111,7 @@ public static void setMachineStatusBasedOnStateListAndException(StateMachineInst stateMachineInstance.setStatus(ExecutionStatus.UN); hasSetStatus = true; } else if (ExecutionStatus.SU.equals(stateInstance.getStatus())) { - if (DomainConstants.STATE_TYPE_SERVICE_TASK.equals(stateInstance.getType())) { + if (StateType.SERVICE_TASK.equals(stateInstance.getType())) { if (stateInstance.isForUpdate() && !stateInstance.isForCompensation()) { hasSuccessUpdateService = true; } diff --git a/saga/seata-saga-spring/src/main/java/org/apache/seata/saga/engine/store/db/DbAndReportTcStateLogStore.java b/saga/seata-saga-spring/src/main/java/org/apache/seata/saga/engine/store/db/DbAndReportTcStateLogStore.java index d70515ca288..f3ee1512976 100644 --- a/saga/seata-saga-spring/src/main/java/org/apache/seata/saga/engine/store/db/DbAndReportTcStateLogStore.java +++ b/saga/seata-saga-spring/src/main/java/org/apache/seata/saga/engine/store/db/DbAndReportTcStateLogStore.java @@ -48,11 +48,7 @@ import org.apache.seata.saga.engine.store.StateLogStore; import org.apache.seata.saga.engine.tm.SagaTransactionalTemplate; import org.apache.seata.saga.proctrl.ProcessContext; -import org.apache.seata.saga.statelang.domain.DomainConstants; -import org.apache.seata.saga.statelang.domain.ExecutionStatus; -import org.apache.seata.saga.statelang.domain.StateInstance; -import org.apache.seata.saga.statelang.domain.StateMachine; -import org.apache.seata.saga.statelang.domain.StateMachineInstance; +import org.apache.seata.saga.statelang.domain.*; import org.apache.seata.saga.statelang.domain.impl.ServiceTaskStateImpl; import org.apache.seata.saga.statelang.domain.impl.StateInstanceImpl; import org.apache.seata.saga.statelang.domain.impl.StateMachineInstanceImpl; @@ -843,7 +839,7 @@ public void toStatement(StateInstance stateInstance, PreparedStatement statement statement.setString(1, stateInstance.getId()); statement.setString(2, stateInstance.getMachineInstanceId()); statement.setString(3, stateInstance.getName()); - statement.setString(4, stateInstance.getType()); + statement.setString(4, stateInstance.getType().getValue()); statement.setTimestamp(5, new Timestamp(stateInstance.getGmtStarted().getTime())); statement.setString(6, stateInstance.getServiceName()); statement.setString(7, stateInstance.getServiceMethod()); @@ -908,7 +904,7 @@ public StateInstance toObject(ResultSet resultSet) throws SQLException { stateInstance.setId(resultSet.getString("id")); stateInstance.setMachineInstanceId(resultSet.getString("machine_inst_id")); stateInstance.setName(resultSet.getString("name")); - stateInstance.setType(resultSet.getString("type")); + stateInstance.setType(StateType.getStateType(resultSet.getString("type"))); stateInstance.setBusinessKey(resultSet.getString("business_key")); stateInstance.setStatus(ExecutionStatus.valueOf(resultSet.getString("status"))); stateInstance.setGmtStarted(resultSet.getTimestamp("gmt_started")); diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/DomainConstants.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/DomainConstants.java index 09ce9cbb017..50267e00e45 100644 --- a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/DomainConstants.java +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/DomainConstants.java @@ -21,19 +21,6 @@ * */ public interface DomainConstants { - - //region State Types - String STATE_TYPE_SERVICE_TASK = "ServiceTask"; - String STATE_TYPE_CHOICE = "Choice"; - String STATE_TYPE_FAIL = "Fail"; - String STATE_TYPE_SUCCEED = "Succeed"; - String STATE_TYPE_COMPENSATION_TRIGGER = "CompensationTrigger"; - String STATE_TYPE_SUB_STATE_MACHINE = "SubStateMachine"; - String STATE_TYPE_SUB_MACHINE_COMPENSATION = "CompensateSubMachine"; - String STATE_TYPE_SCRIPT_TASK = "ScriptTask"; - String STATE_TYPE_LOOP_START = "LoopStart"; - //endregion - String COMPENSATE_SUB_MACHINE_STATE_NAME_PREFIX = "_compensate_sub_machine_state_"; //region Service Types diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/State.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/State.java index 1eb02dbe958..985bfb3bfbc 100644 --- a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/State.java +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/State.java @@ -43,7 +43,7 @@ public interface State { * * @return the state type */ - String getType(); + StateType getType(); /** * next state name diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/StateInstance.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/StateInstance.java index a7b4dc66fd5..fcbcebc9c73 100644 --- a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/StateInstance.java +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/StateInstance.java @@ -71,14 +71,14 @@ public interface StateInstance { * * @return state instance type */ - String getType(); + StateType getType(); /** * set type * * @param type state instance type */ - void setType(String type); + void setType(StateType type); /** * get service name diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/StateType.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/StateType.java new file mode 100644 index 00000000000..38cd69e9881 --- /dev/null +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/StateType.java @@ -0,0 +1,91 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.seata.saga.statelang.domain; + +/** + * StateType + * + */ +public enum StateType { + + /** + * ServiceTask State + */ + SERVICE_TASK("ServiceTask"), + + /** + * Choice State + */ + CHOICE("Choice"), + + /** + * Fail State + */ + FAIL("Fail"), + + /** + * Succeed State + */ + SUCCEED("Succeed"), + + /** + * CompensationTrigger State + */ + COMPENSATION_TRIGGER("CompensationTrigger"), + + /** + * SubStateMachine State + */ + SUB_STATE_MACHINE("SubStateMachine"), + + /** + * CompensateSubMachine State + */ + SUB_MACHINE_COMPENSATION("CompensateSubMachine"), + + /** + * ScriptTask State + */ + SCRIPT_TASK("ScriptTask"), + + /** + * LoopStart State + */ + LOOP_START("LoopStart"); + + + private String value; + + StateType(String value) { + this.value = value; + } + + public String getValue() { + return value; + } + + public static StateType getStateType(String value) { + for (StateType stateType : values()) { + if (stateType.getValue().equalsIgnoreCase(value)) { + return stateType; + } + } + + throw new IllegalArgumentException("Unknown StateType[" + value + "]"); + } + +} diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/BaseState.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/BaseState.java index 50ae952f9b9..001083de024 100644 --- a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/BaseState.java +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/BaseState.java @@ -20,6 +20,7 @@ import org.apache.seata.saga.statelang.domain.State; import org.apache.seata.saga.statelang.domain.StateMachine; +import org.apache.seata.saga.statelang.domain.StateType; /** * BaseState @@ -28,7 +29,7 @@ public abstract class BaseState implements State { private transient String name; - private String type; + private StateType type; private String comment; private String next; private Map extensions; @@ -80,11 +81,11 @@ public void setStateMachine(StateMachine stateMachine) { } @Override - public String getType() { + public StateType getType() { return type; } - protected void setType(String type) { + protected void setType(StateType type) { this.type = type; } } diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/ChoiceStateImpl.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/ChoiceStateImpl.java index c20b6be2935..da087f545e0 100644 --- a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/ChoiceStateImpl.java +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/ChoiceStateImpl.java @@ -20,7 +20,7 @@ import java.util.Map; import org.apache.seata.saga.statelang.domain.ChoiceState; -import org.apache.seata.saga.statelang.domain.DomainConstants; +import org.apache.seata.saga.statelang.domain.StateType; /** * Single selection status @@ -36,7 +36,7 @@ public class ChoiceStateImpl extends BaseState implements ChoiceState { private Map choiceEvaluators; public ChoiceStateImpl() { - setType(DomainConstants.STATE_TYPE_CHOICE); + setType(StateType.CHOICE); } @Override diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/CompensateSubStateMachineStateImpl.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/CompensateSubStateMachineStateImpl.java index c68fe4e47b2..61cddbb6029 100644 --- a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/CompensateSubStateMachineStateImpl.java +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/CompensateSubStateMachineStateImpl.java @@ -17,7 +17,7 @@ package org.apache.seata.saga.statelang.domain.impl; import org.apache.seata.saga.statelang.domain.CompensateSubStateMachineState; -import org.apache.seata.saga.statelang.domain.DomainConstants; +import org.apache.seata.saga.statelang.domain.StateType; /** * Used to compensate the state of the sub state machine, inherited from ServiceTaskState @@ -25,6 +25,6 @@ */ public class CompensateSubStateMachineStateImpl extends ServiceTaskStateImpl implements CompensateSubStateMachineState { public CompensateSubStateMachineStateImpl() { - setType(DomainConstants.STATE_TYPE_SUB_MACHINE_COMPENSATION); + setType(StateType.SUB_MACHINE_COMPENSATION); } } diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/CompensationTriggerStateImpl.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/CompensationTriggerStateImpl.java index 8e638e48905..7aa422cd555 100644 --- a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/CompensationTriggerStateImpl.java +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/CompensationTriggerStateImpl.java @@ -17,7 +17,7 @@ package org.apache.seata.saga.statelang.domain.impl; import org.apache.seata.saga.statelang.domain.CompensationTriggerState; -import org.apache.seata.saga.statelang.domain.DomainConstants; +import org.apache.seata.saga.statelang.domain.StateType; /** * Triggering the "compensation" process for the state machine @@ -26,6 +26,6 @@ public class CompensationTriggerStateImpl extends BaseState implements CompensationTriggerState { public CompensationTriggerStateImpl() { - setType(DomainConstants.STATE_TYPE_COMPENSATION_TRIGGER); + setType(StateType.COMPENSATION_TRIGGER); } } diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/FailEndStateImpl.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/FailEndStateImpl.java index 4893c4daadd..8c5b3d7a57c 100644 --- a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/FailEndStateImpl.java +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/FailEndStateImpl.java @@ -16,7 +16,7 @@ */ package org.apache.seata.saga.statelang.domain.impl; -import org.apache.seata.saga.statelang.domain.DomainConstants; +import org.apache.seata.saga.statelang.domain.StateType; import org.apache.seata.saga.statelang.domain.FailEndState; /** @@ -29,7 +29,7 @@ public class FailEndStateImpl extends BaseState implements FailEndState { private String message; public FailEndStateImpl() { - setType(DomainConstants.STATE_TYPE_FAIL); + setType(StateType.FAIL); } @Override diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/LoopStartStateImpl.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/LoopStartStateImpl.java index 433ce91a2c1..72a3a1f3f04 100644 --- a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/LoopStartStateImpl.java +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/LoopStartStateImpl.java @@ -16,7 +16,7 @@ */ package org.apache.seata.saga.statelang.domain.impl; -import org.apache.seata.saga.statelang.domain.DomainConstants; +import org.apache.seata.saga.statelang.domain.StateType; import org.apache.seata.saga.statelang.domain.LoopStartState; /** @@ -26,7 +26,7 @@ public class LoopStartStateImpl extends BaseState implements LoopStartState { public LoopStartStateImpl() { - setType(DomainConstants.STATE_TYPE_LOOP_START); + setType(StateType.LOOP_START); } } diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/ScriptTaskStateImpl.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/ScriptTaskStateImpl.java index ed291b06b00..0634c0fbdeb 100644 --- a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/ScriptTaskStateImpl.java +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/ScriptTaskStateImpl.java @@ -16,7 +16,7 @@ */ package org.apache.seata.saga.statelang.domain.impl; -import org.apache.seata.saga.statelang.domain.DomainConstants; +import org.apache.seata.saga.statelang.domain.StateType; import org.apache.seata.saga.statelang.domain.ScriptTaskState; /** @@ -32,7 +32,7 @@ public class ScriptTaskStateImpl extends AbstractTaskState implements ScriptTask private String scriptContent; public ScriptTaskStateImpl() { - setType(DomainConstants.STATE_TYPE_SCRIPT_TASK); + setType(StateType.SCRIPT_TASK); } @Override diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/ServiceTaskStateImpl.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/ServiceTaskStateImpl.java index fdb1a075e36..aa16d60b83b 100644 --- a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/ServiceTaskStateImpl.java +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/ServiceTaskStateImpl.java @@ -20,7 +20,7 @@ import java.util.List; import java.util.Map; -import org.apache.seata.saga.statelang.domain.DomainConstants; +import org.apache.seata.saga.statelang.domain.StateType; import org.apache.seata.saga.statelang.domain.ServiceTaskState; /** @@ -38,7 +38,7 @@ public class ServiceTaskStateImpl extends AbstractTaskState implements ServiceTa private boolean isAsync; public ServiceTaskStateImpl() { - setType(DomainConstants.STATE_TYPE_SERVICE_TASK); + setType(StateType.SERVICE_TASK); } @Override diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/StateInstanceImpl.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/StateInstanceImpl.java index dd6ec21de59..0838f6ea15e 100644 --- a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/StateInstanceImpl.java +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/StateInstanceImpl.java @@ -22,6 +22,7 @@ import org.apache.seata.saga.statelang.domain.ExecutionStatus; import org.apache.seata.saga.statelang.domain.StateInstance; import org.apache.seata.saga.statelang.domain.StateMachineInstance; +import org.apache.seata.saga.statelang.domain.StateType; /** * state execution instance @@ -32,7 +33,7 @@ public class StateInstanceImpl implements StateInstance { private String id; private String machineInstanceId; private String name; - private String type; + private StateType type; private String serviceName; private String serviceMethod; private String serviceType; @@ -85,12 +86,12 @@ public void setName(String name) { } @Override - public String getType() { + public StateType getType() { return type; } @Override - public void setType(String type) { + public void setType(StateType type) { this.type = type; } diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/SubStateMachineImpl.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/SubStateMachineImpl.java index bf6d1e78b1c..534d95644b0 100644 --- a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/SubStateMachineImpl.java +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/SubStateMachineImpl.java @@ -16,7 +16,7 @@ */ package org.apache.seata.saga.statelang.domain.impl; -import org.apache.seata.saga.statelang.domain.DomainConstants; +import org.apache.seata.saga.statelang.domain.StateType; import org.apache.seata.saga.statelang.domain.SubStateMachine; import org.apache.seata.saga.statelang.domain.TaskState; @@ -31,7 +31,7 @@ public class SubStateMachineImpl extends ServiceTaskStateImpl implements SubStat private TaskState compensateStateObject; public SubStateMachineImpl() { - setType(DomainConstants.STATE_TYPE_SUB_STATE_MACHINE); + setType(StateType.SUB_STATE_MACHINE); } @Override diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/SucceedEndStateImpl.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/SucceedEndStateImpl.java index eb5ab712d37..1e6bf1414e2 100644 --- a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/SucceedEndStateImpl.java +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/SucceedEndStateImpl.java @@ -16,7 +16,7 @@ */ package org.apache.seata.saga.statelang.domain.impl; -import org.apache.seata.saga.statelang.domain.DomainConstants; +import org.apache.seata.saga.statelang.domain.StateType; import org.apache.seata.saga.statelang.domain.SucceedEndState; /** @@ -26,6 +26,6 @@ public class SucceedEndStateImpl extends BaseState implements SucceedEndState { public SucceedEndStateImpl() { - setType(DomainConstants.STATE_TYPE_SUCCEED); + setType(StateType.SUCCEED); } } diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/parser/StateParserFactory.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/parser/StateParserFactory.java index 3ef0d1e7a86..b2e70fa3498 100644 --- a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/parser/StateParserFactory.java +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/parser/StateParserFactory.java @@ -19,7 +19,7 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -import org.apache.seata.saga.statelang.domain.DomainConstants; +import org.apache.seata.saga.statelang.domain.StateType; import org.apache.seata.saga.statelang.parser.impl.ChoiceStateParser; import org.apache.seata.saga.statelang.parser.impl.CompensateSubStateMachineStateParser; import org.apache.seata.saga.statelang.parser.impl.CompensationTriggerStateParser; @@ -35,21 +35,21 @@ */ public class StateParserFactory { - protected static Map stateParserMap = new ConcurrentHashMap<>(); + protected static Map stateParserMap = new ConcurrentHashMap<>(); static { - stateParserMap.put(DomainConstants.STATE_TYPE_SERVICE_TASK, new ServiceTaskStateParser()); - stateParserMap.put(DomainConstants.STATE_TYPE_CHOICE, new ChoiceStateParser()); - stateParserMap.put(DomainConstants.STATE_TYPE_COMPENSATION_TRIGGER, new CompensationTriggerStateParser()); - stateParserMap.put(DomainConstants.STATE_TYPE_FAIL, new FailEndStateParser()); - stateParserMap.put(DomainConstants.STATE_TYPE_SUCCEED, new SucceedEndStateParser()); - stateParserMap.put(DomainConstants.STATE_TYPE_SUB_STATE_MACHINE, new SubStateMachineParser()); - stateParserMap.put(DomainConstants.STATE_TYPE_SUB_MACHINE_COMPENSATION, + stateParserMap.put(StateType.SERVICE_TASK, new ServiceTaskStateParser()); + stateParserMap.put(StateType.CHOICE, new ChoiceStateParser()); + stateParserMap.put(StateType.COMPENSATION_TRIGGER, new CompensationTriggerStateParser()); + stateParserMap.put(StateType.FAIL, new FailEndStateParser()); + stateParserMap.put(StateType.SUCCEED, new SucceedEndStateParser()); + stateParserMap.put(StateType.SUB_STATE_MACHINE, new SubStateMachineParser()); + stateParserMap.put(StateType.SUB_MACHINE_COMPENSATION, new CompensateSubStateMachineStateParser()); - stateParserMap.put(DomainConstants.STATE_TYPE_SCRIPT_TASK, new ScriptTaskStateParser()); + stateParserMap.put(StateType.SCRIPT_TASK, new ScriptTaskStateParser()); } - public static StateParser getStateParser(String stateType) { + public static StateParser getStateParser(StateType stateType) { return stateParserMap.get(stateType); } } diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/parser/impl/StateMachineParserImpl.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/parser/impl/StateMachineParserImpl.java index 46ec2f12df4..da4b5ba1971 100644 --- a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/parser/impl/StateMachineParserImpl.java +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/parser/impl/StateMachineParserImpl.java @@ -21,6 +21,7 @@ import org.apache.seata.saga.statelang.domain.DomainConstants; import org.apache.seata.saga.statelang.domain.RecoverStrategy; import org.apache.seata.saga.statelang.domain.State; +import org.apache.seata.saga.statelang.domain.StateType; import org.apache.seata.saga.statelang.domain.StateMachine; import org.apache.seata.saga.statelang.domain.impl.AbstractTaskState; import org.apache.seata.saga.statelang.domain.impl.BaseState; @@ -97,7 +98,7 @@ public StateMachine parse(String json) { Map statesNode = (Map) node.get("States"); statesNode.forEach((stateName, value) -> { Map stateNode = (Map) value; - String stateType = (String) stateNode.get("Type"); + StateType stateType = StateType.getStateType((String) stateNode.get("Type")); StateParser stateParser = StateParserFactory.getStateParser(stateType); if (stateParser == null) { throw new IllegalArgumentException("State Type [" + stateType + "] is not support"); diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/parser/utils/StateMachineUtils.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/parser/utils/StateMachineUtils.java index eb4e89f25b2..69844e58205 100644 --- a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/parser/utils/StateMachineUtils.java +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/parser/utils/StateMachineUtils.java @@ -18,7 +18,7 @@ import org.apache.seata.common.util.StringUtils; import org.apache.seata.saga.statelang.domain.ChoiceState; -import org.apache.seata.saga.statelang.domain.DomainConstants; +import org.apache.seata.saga.statelang.domain.StateType; import org.apache.seata.saga.statelang.domain.State; import org.apache.seata.saga.statelang.domain.TaskState; @@ -36,17 +36,18 @@ public static Set getAllPossibleSubsequentStates(State state) { Set subsequentStates = new HashSet<>(); // Next state subsequentStates.add(state.getNext()); - switch (state.getType()) { - case DomainConstants.STATE_TYPE_SCRIPT_TASK: - case DomainConstants.STATE_TYPE_SERVICE_TASK: - case DomainConstants.STATE_TYPE_SUB_STATE_MACHINE: - case DomainConstants.STATE_TYPE_SUB_MACHINE_COMPENSATION: + StateType stateType = state.getType(); + switch (stateType) { + case SCRIPT_TASK: + case SERVICE_TASK: + case SUB_STATE_MACHINE: + case SUB_MACHINE_COMPENSATION: // Next state in catches Optional.ofNullable(((TaskState) state).getCatches()) .ifPresent(c -> c.forEach(e -> subsequentStates.add(e.getNext()))); break; - case DomainConstants.STATE_TYPE_CHOICE: + case CHOICE: // Choice state Optional.ofNullable(((ChoiceState) state).getChoices()) .ifPresent(c -> c.forEach(e -> subsequentStates.add(e.getNext()))); diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/validator/impl/NoRecursiveSubStateMachineRule.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/validator/impl/NoRecursiveSubStateMachineRule.java index e8266f7d453..a355cb3705c 100644 --- a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/validator/impl/NoRecursiveSubStateMachineRule.java +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/validator/impl/NoRecursiveSubStateMachineRule.java @@ -16,7 +16,7 @@ */ package org.apache.seata.saga.statelang.validator.impl; -import org.apache.seata.saga.statelang.domain.DomainConstants; +import org.apache.seata.saga.statelang.domain.StateType; import org.apache.seata.saga.statelang.domain.State; import org.apache.seata.saga.statelang.domain.StateMachine; import org.apache.seata.saga.statelang.domain.SubStateMachine; @@ -30,7 +30,7 @@ public class NoRecursiveSubStateMachineRule extends AbstractRule { @Override public boolean validate(StateMachine stateMachine) { for (State state: stateMachine.getStates().values()) { - if (!DomainConstants.STATE_TYPE_SUB_STATE_MACHINE.equals(state.getType())) { + if (!StateType.SUB_STATE_MACHINE.equals(state.getType())) { continue; } if (stateMachine.getName().equals(((SubStateMachine) state).getStateMachineName())) {