使用什么数据结构或设计模式来映射定义的步骤序列
What data structure or design pattern to use to map a defined sequence of steps
我们有一个 java 应用程序可以处理不同类型的金融交易。这些交易有不同的流程,为了系统灵活性(尤其是用户),我们引入了一个 xml 来定义每个交易流程。这些步骤是异步执行的
xml 看起来像这样
<transaction code="201510" name="deposit">
<step id="1" name="momo.debit" script="step-1" result="0000" nextid="2">
<error code="0104" script="err-0104"/>
<error code="5008" script="$err-0301"/>
<error code="03**" script="err-0300"/>
<error code="50-98" script="err-5000"/>
</step>
<step id="2" name="deposit" script="step-2" result="0000" nextid="3">
<error code="0105" script="err-0105" hook="2" />
<error code="0203" script="$err-5000" hook="2"/>
<error code="0205" script="$err-5000" hook="2"/>
<error code="0206" script="$err-5000" hook="2"/>
<error code="6100" script="err-5100" hook="2"/>
<error code="5001" script="$err-5101" hook="2"/>
<error code="5002" script="$err-5101" hook="2"/>
<error code="5003" script="$err-5101" hook="2"/>
<error code="5004" script="$err-5102" hook="2"/>
<error code="5005" script="$err-5104" hook="2"/>
<error code="5006" script="$err-5105" hook="2"/>
<error code="50-59" script="$err-5107" hook="2"/>
<error code="6000" script="$err-5108" hook="2"/>
<error code="6005" script="$err-5106" hook="2"/>
<error code="60-99" script="$err-5000" hook="2"/>
</step>
<step id="3" name="notify" script="$notify" result="*"/>
</transaction>
剧情简介
每个步骤都包含一个步骤脚本、步骤脚本的预期结果、步骤脚本失败时的纠错脚本以及拦截称为挂钩的脚本,这些脚本在执行步骤脚本或错误脚本之前执行一些工作。
当前设计
我有以下 classes
一个名为 State 的枚举
enum State {
ERROR, HOOK, STEP, REPLAY;
}
包含所有属性的状态节点:
final class StateNode implements Serializable {
private final String id;
private final String name;
private final String code;
private final String hook;
private final String replay;
private final String script;
private final String result;
private final String nextid;
private final String advance;
private final States statename;
private boolean errorExecuted = false;
private boolean hookExecuted = false;
private boolean replayExecuted = false;
private boolean scriptExecuted = false;
public StateNode(States statename, Map<String, String> step) {
this(statename, step.get("id"), step.get("name"), step.get("script"), step.get("result"), step.get("nextid"), step.get("code"), step.get("hook"), step.get("replay"), step.get("advance"));
}
public StateNode(States statename) {
this(statename, "");
}
public StateNode(States statename, String id) {
this(statename, id, "");
}
public StateNode(States statename, String id, String name) {
this(statename, id, name, "");
}
public StateNode(States statename, String id, String name, String script) {
this(statename, id, name, script, "");
}
public StateNode(States statename, String id, String name, String script, String result) {
this(statename, id, name, script, result, "");
}
public StateNode(States statename, String id, String name, String script, String result, String nextid) {
this(statename, id, name, script, result, nextid, "");
}
public StateNode(States statename, String id, String name, String script, String result, String nextid, String code) {
this(statename, id, name, script, result, nextid, code, "");
}
public StateNode(States statename, String id, String name, String script, String result, String nextid, String code, String hook) {
this(statename, id, name, script, result, nextid, code, hook, "");
}
public StateNode(States statename, String id, String name, String script, String result, String nextid, String code, String hook, String replay) {
this(statename, id, name, script, result, nextid, code, hook, replay, "");
}
public StateNode(States statename, String id, String name, String script, String result, String nextid, String code, String hook, String replay, String advance) {
this.id = id;
this.code = code;
this.name = name;
this.hook = hook;
this.nextid = nextid;
this.replay = replay;
this.result = result;
this.script = script;
this.advance = advance;
this.statename = statename;
}
public States getStateName() {
return statename;
}
public String getId() {
return StringUtils.defaultIfEmpty(id, "-1");
}
public String getName() {
return StringUtils.defaultIfEmpty(name, "");
}
public String getScript() {
return StringUtils.defaultIfEmpty(script, "");
}
public String getResult() {
return StringUtils.defaultIfEmpty(result, "*");
}
public String getNextid() {
return StringUtils.defaultIfEmpty(nextid, "-1");
}
public String getCode() {
return StringUtils.defaultIfEmpty(code, "");
}
public String getHook() {
return StringUtils.defaultIfEmpty(hook, "");
}
public String getReplay() {
return StringUtils.defaultIfEmpty(replay, "");
}
public String getAdvance() {
return StringUtils.defaultIfEmpty(advance, "true");
}
public boolean isErrorExecuted() {
return errorExecuted;
}
public void setErrorExecuted(boolean errorExecuted) {
this.errorExecuted = errorExecuted;
}
public boolean isHookExecuted() {
return hookExecuted;
}
public void setHookExecuted(boolean hookExecuted) {
this.hookExecuted = hookExecuted;
}
public boolean isReplayExecuted() {
return replayExecuted;
}
public void setReplayExecuted(boolean replayExecuted) {
this.replayExecuted = replayExecuted;
}
public boolean isScriptExecuted() {
return scriptExecuted;
}
public void setScriptExecuted(boolean scriptExecuted) {
this.scriptExecuted = scriptExecuted;
}
}
一个状态树,用于存储系统内正在执行的事务的所有正在执行的步骤 (Statenode)
final class StateTree 实现 Serializable {
private int maxsteps = 0;
private final Map<Integer, StateNode> branches;
public StateTree() {
this.branches = new LinkedHashMap();
}
public int getPoint() {
return branches.size();
}
public State getParentState(int point) {
return branches.get(point - 1).getStateName();
}
public StateNode getCurrentState() {
return branches.get(branches.size());
}
public StateNode getState(int point) {
return branches.get(point);
}
public StateNode[] getStates() {
return branches.values().toArray(new StateNode[branches.size()]);
}
public int getMaximum() {
return maxsteps;
}
public int promote() {
return promote(1);
}
public int promote(int factor) {
for (int i = 0; i < factor; i++) {
branches.remove(branches.size());
}
return branches.size();
}
public StateTree setState(StateNode state) {
branches.put((branches.size() + 1), state);
return this;
}
public void setMaxSteps(int maxsteps) {
this.maxsteps = maxsteps;
}
}
目前的运作方式
当请求新事务时,有一个称为事务处理引擎的模块,它使用 xml 和此结构来处理事务。因此,我们会为进入系统的每个事务创建一个新的状态树,并将其保存到数据库中,然后对于执行的每个步骤、错误或挂钩,我们创建一个新的状态节点并将其保存到状态树中。我希望这已经足够清楚了。
实际执行
为了确定执行哪个状态以及在哪个步骤执行,我有一个称为 resolve execution 的递归方法。我希望它对 reader:
是直观的
private void resolveExecution(Context ctx, Map<String, String> header, StateTree tree) throws Exception {
StateNode state = tree.getCurrentState();
/**
* Resolves the states priority by doing the following check. if a hook
* exists and has not been executed then change the state to hook and
* then invoke the hook logic else if not check that a replay exists an
* that it has not been executed and if that is met then invoke the
* replay manager and it's logic else invoke the the current state.
*/
State statename = !state.getHook().isEmpty() && !state.isHookExecuted()
? State.HOOK : !state.isScriptExecuted() ? state.getStateName()
: state.getStateName() == State.ERROR && !state.getReplay().isEmpty() && !state.isReplayExecuted()
? State.REPLAY
: state.getStateName();
switch (statename) {
case STEP:
logger.debug(Utility.LOG.transaction(header.get("code"), header.get("type"), header.get("id"), header.get("msisdn"), "Begin resolving execution for step."));
if (state.isScriptExecuted()) {
/**
* Add new state of the next step id and update the state
* map
*/
tree.setState(new StateNode(
State.STEP,
Configurations.HANDLER.getStepConfigurationsFromId(header.get("code"),
state.getNextid())
));
this.resolveAndExecuteScript(ctx, header, tree);
} else {
state.setScriptExecuted(true);
this.resolveAndExecuteScript(ctx, header, tree);
}
logger.debug(Utility.LOG.transaction(header.get("code"), header.get("type"), header.get("id"), header.get("msisdn"), "End of resolving execution for step."));
break;
case ERROR:
logger.debug(Utility.LOG.transaction(header.get("code"), header.get("type"), header.get("id"), header.get("msisdn"), "Begin resolving execution for error."));
if (state.isScriptExecuted()) {
if ("-9".equals(state.getNextid())) {
this.exit(ctx, header);
} else {
tree.promote();
this.resolveExecution(ctx, header, tree);
}
} else {
state.setErrorExecuted(true);
this.resolveAndExecuteScript(ctx, header, tree);
}
logger.debug(Utility.LOG.transaction(header.get("code"), header.get("type"), header.get("id"), header.get("msisdn"), "End of resolving execution for error."));
break;
case HOOK:
logger.debug(Utility.LOG.transaction(header.get("code"), header.get("type"), header.get("id"), header.get("msisdn"), "Begin resolving execution for hook."));
if (state.isScriptExecuted()) {
if ("true".equals(state.getAdvance())) {
tree.promote();
this.resolveExecution(ctx, header, tree);
} else {
this.exit(ctx, header, this.resolveStatus(7));
}
} else {
state.setHookExecuted(true);
Map<String, String> configs = Configurations.HANDLER.getHookConfigurations(state.getHook());
tree.setState(new StateNode(statename,
state.getId(),
configs.get("name"),
configs.get("script"),
configs.get("result"),
state.getNextid(),
state.getCode(),
state.getHook(),
state.getReplay(),
StringUtils.defaultIfEmpty(configs.get("advance"), "true")));
this.resolveAndExecuteScript(ctx, header, tree);
}
logger.debug(Utility.LOG.transaction(header.get("code"), header.get("type"), header.get("id"), header.get("msisdn"), "End of resolving execution for hook."));
break;
case REPLAY:
logger.debug(Utility.LOG.transaction(header.get("code"), header.get("type"), header.get("id"), header.get("msisdn"), "Begin resolving execution for replay."));
state.setReplayExecuted(true);
if (!this.replay(ctx, header, state.getStateName() == State.REPLAY ? tree
: tree.setState(new StateNode(
statename,
state.getId(),
state.getName(),
"?".equals(state.getScript()) ? tree.getState(2).getScript() : state.getScript(),
state.getResult(),
state.getNextid(),
state.getCode(),
state.getHook(),
state.getReplay())))) {
tree.promote();
this.resolveExecution(ctx, header, tree);
}
logger.debug(Utility.LOG.transaction(header.get("code"), header.get("type"), header.get("id"), header.get("msisdn"), "End of resolving execution for replay."));
break;
}
}
问题
我已经有了一个可行的解决方案,但我觉得它不是最好的。现在我们正在进行性能测试,我发现处理速度很慢,我相信它会更好。您会推荐哪种数据结构来映射此 xml and/or 设计模式?我希望这不像是一个开放式问题。
谢谢
您可以实现包含所有属性的 Step
class 并将所有步骤存储在 LinkedHashSet:
public class Step{
private int id;
private String name;
private String script;
private String result;
// getters and setters and constructors
}
然后制作一个 LinkedHashSet 步骤,它们将按照您插入它们的顺序相互链接:
Step step1=new Step(1, "momo.debit", "step-1", "0000");
Step step2=new Step(2, "deposit", "step-2", "0000");
// create all your steps here
LinkedHashSet<Step> steps = new LinkedHashSet<Step>();
steps.add(step1);
steps.add(step2);
查看这些示例以获取更多信息:
感觉有点像味道问题。
如果我理解正确的话,你有数据驱动流,我的意思是你有一些数据(在这种情况下以 xml 和脚本的形式)指示将采取什么行动(所以它是在编译时不知道,但你使用伪语言来描述这个流程)。
我通常的做法是制作一个命令队列并一次性填充所有命令,然后执行它,而不是执行命令、加载下一个脚本、执行命令。然而,这涉及到制作你自己的伪语言编译器可以这么说:)。
如果您的 xml 有点微不足道,您可以轻松解决。
对于您的 xml 示例,命令队列将导致
1 - 2 - 3
因为你没有分支(在我看来你只是通过它们不管)
您将必须预加载一些脚本(以进行额外优化):
$err-5000
$notify
etc
现在您可以在全局级别决定哪些脚本将被缓存,哪些不被缓存。
我们有一个 java 应用程序可以处理不同类型的金融交易。这些交易有不同的流程,为了系统灵活性(尤其是用户),我们引入了一个 xml 来定义每个交易流程。这些步骤是异步执行的 xml 看起来像这样
<transaction code="201510" name="deposit">
<step id="1" name="momo.debit" script="step-1" result="0000" nextid="2">
<error code="0104" script="err-0104"/>
<error code="5008" script="$err-0301"/>
<error code="03**" script="err-0300"/>
<error code="50-98" script="err-5000"/>
</step>
<step id="2" name="deposit" script="step-2" result="0000" nextid="3">
<error code="0105" script="err-0105" hook="2" />
<error code="0203" script="$err-5000" hook="2"/>
<error code="0205" script="$err-5000" hook="2"/>
<error code="0206" script="$err-5000" hook="2"/>
<error code="6100" script="err-5100" hook="2"/>
<error code="5001" script="$err-5101" hook="2"/>
<error code="5002" script="$err-5101" hook="2"/>
<error code="5003" script="$err-5101" hook="2"/>
<error code="5004" script="$err-5102" hook="2"/>
<error code="5005" script="$err-5104" hook="2"/>
<error code="5006" script="$err-5105" hook="2"/>
<error code="50-59" script="$err-5107" hook="2"/>
<error code="6000" script="$err-5108" hook="2"/>
<error code="6005" script="$err-5106" hook="2"/>
<error code="60-99" script="$err-5000" hook="2"/>
</step>
<step id="3" name="notify" script="$notify" result="*"/>
</transaction>
剧情简介
每个步骤都包含一个步骤脚本、步骤脚本的预期结果、步骤脚本失败时的纠错脚本以及拦截称为挂钩的脚本,这些脚本在执行步骤脚本或错误脚本之前执行一些工作。
当前设计 我有以下 classes
一个名为 State 的枚举
enum State {
ERROR, HOOK, STEP, REPLAY;
}
包含所有属性的状态节点:
final class StateNode implements Serializable {
private final String id;
private final String name;
private final String code;
private final String hook;
private final String replay;
private final String script;
private final String result;
private final String nextid;
private final String advance;
private final States statename;
private boolean errorExecuted = false;
private boolean hookExecuted = false;
private boolean replayExecuted = false;
private boolean scriptExecuted = false;
public StateNode(States statename, Map<String, String> step) {
this(statename, step.get("id"), step.get("name"), step.get("script"), step.get("result"), step.get("nextid"), step.get("code"), step.get("hook"), step.get("replay"), step.get("advance"));
}
public StateNode(States statename) {
this(statename, "");
}
public StateNode(States statename, String id) {
this(statename, id, "");
}
public StateNode(States statename, String id, String name) {
this(statename, id, name, "");
}
public StateNode(States statename, String id, String name, String script) {
this(statename, id, name, script, "");
}
public StateNode(States statename, String id, String name, String script, String result) {
this(statename, id, name, script, result, "");
}
public StateNode(States statename, String id, String name, String script, String result, String nextid) {
this(statename, id, name, script, result, nextid, "");
}
public StateNode(States statename, String id, String name, String script, String result, String nextid, String code) {
this(statename, id, name, script, result, nextid, code, "");
}
public StateNode(States statename, String id, String name, String script, String result, String nextid, String code, String hook) {
this(statename, id, name, script, result, nextid, code, hook, "");
}
public StateNode(States statename, String id, String name, String script, String result, String nextid, String code, String hook, String replay) {
this(statename, id, name, script, result, nextid, code, hook, replay, "");
}
public StateNode(States statename, String id, String name, String script, String result, String nextid, String code, String hook, String replay, String advance) {
this.id = id;
this.code = code;
this.name = name;
this.hook = hook;
this.nextid = nextid;
this.replay = replay;
this.result = result;
this.script = script;
this.advance = advance;
this.statename = statename;
}
public States getStateName() {
return statename;
}
public String getId() {
return StringUtils.defaultIfEmpty(id, "-1");
}
public String getName() {
return StringUtils.defaultIfEmpty(name, "");
}
public String getScript() {
return StringUtils.defaultIfEmpty(script, "");
}
public String getResult() {
return StringUtils.defaultIfEmpty(result, "*");
}
public String getNextid() {
return StringUtils.defaultIfEmpty(nextid, "-1");
}
public String getCode() {
return StringUtils.defaultIfEmpty(code, "");
}
public String getHook() {
return StringUtils.defaultIfEmpty(hook, "");
}
public String getReplay() {
return StringUtils.defaultIfEmpty(replay, "");
}
public String getAdvance() {
return StringUtils.defaultIfEmpty(advance, "true");
}
public boolean isErrorExecuted() {
return errorExecuted;
}
public void setErrorExecuted(boolean errorExecuted) {
this.errorExecuted = errorExecuted;
}
public boolean isHookExecuted() {
return hookExecuted;
}
public void setHookExecuted(boolean hookExecuted) {
this.hookExecuted = hookExecuted;
}
public boolean isReplayExecuted() {
return replayExecuted;
}
public void setReplayExecuted(boolean replayExecuted) {
this.replayExecuted = replayExecuted;
}
public boolean isScriptExecuted() {
return scriptExecuted;
}
public void setScriptExecuted(boolean scriptExecuted) {
this.scriptExecuted = scriptExecuted;
}
}
一个状态树,用于存储系统内正在执行的事务的所有正在执行的步骤 (Statenode)
final class StateTree 实现 Serializable {
private int maxsteps = 0;
private final Map<Integer, StateNode> branches;
public StateTree() {
this.branches = new LinkedHashMap();
}
public int getPoint() {
return branches.size();
}
public State getParentState(int point) {
return branches.get(point - 1).getStateName();
}
public StateNode getCurrentState() {
return branches.get(branches.size());
}
public StateNode getState(int point) {
return branches.get(point);
}
public StateNode[] getStates() {
return branches.values().toArray(new StateNode[branches.size()]);
}
public int getMaximum() {
return maxsteps;
}
public int promote() {
return promote(1);
}
public int promote(int factor) {
for (int i = 0; i < factor; i++) {
branches.remove(branches.size());
}
return branches.size();
}
public StateTree setState(StateNode state) {
branches.put((branches.size() + 1), state);
return this;
}
public void setMaxSteps(int maxsteps) {
this.maxsteps = maxsteps;
}
}
目前的运作方式
当请求新事务时,有一个称为事务处理引擎的模块,它使用 xml 和此结构来处理事务。因此,我们会为进入系统的每个事务创建一个新的状态树,并将其保存到数据库中,然后对于执行的每个步骤、错误或挂钩,我们创建一个新的状态节点并将其保存到状态树中。我希望这已经足够清楚了。
实际执行
为了确定执行哪个状态以及在哪个步骤执行,我有一个称为 resolve execution 的递归方法。我希望它对 reader:
private void resolveExecution(Context ctx, Map<String, String> header, StateTree tree) throws Exception {
StateNode state = tree.getCurrentState();
/**
* Resolves the states priority by doing the following check. if a hook
* exists and has not been executed then change the state to hook and
* then invoke the hook logic else if not check that a replay exists an
* that it has not been executed and if that is met then invoke the
* replay manager and it's logic else invoke the the current state.
*/
State statename = !state.getHook().isEmpty() && !state.isHookExecuted()
? State.HOOK : !state.isScriptExecuted() ? state.getStateName()
: state.getStateName() == State.ERROR && !state.getReplay().isEmpty() && !state.isReplayExecuted()
? State.REPLAY
: state.getStateName();
switch (statename) {
case STEP:
logger.debug(Utility.LOG.transaction(header.get("code"), header.get("type"), header.get("id"), header.get("msisdn"), "Begin resolving execution for step."));
if (state.isScriptExecuted()) {
/**
* Add new state of the next step id and update the state
* map
*/
tree.setState(new StateNode(
State.STEP,
Configurations.HANDLER.getStepConfigurationsFromId(header.get("code"),
state.getNextid())
));
this.resolveAndExecuteScript(ctx, header, tree);
} else {
state.setScriptExecuted(true);
this.resolveAndExecuteScript(ctx, header, tree);
}
logger.debug(Utility.LOG.transaction(header.get("code"), header.get("type"), header.get("id"), header.get("msisdn"), "End of resolving execution for step."));
break;
case ERROR:
logger.debug(Utility.LOG.transaction(header.get("code"), header.get("type"), header.get("id"), header.get("msisdn"), "Begin resolving execution for error."));
if (state.isScriptExecuted()) {
if ("-9".equals(state.getNextid())) {
this.exit(ctx, header);
} else {
tree.promote();
this.resolveExecution(ctx, header, tree);
}
} else {
state.setErrorExecuted(true);
this.resolveAndExecuteScript(ctx, header, tree);
}
logger.debug(Utility.LOG.transaction(header.get("code"), header.get("type"), header.get("id"), header.get("msisdn"), "End of resolving execution for error."));
break;
case HOOK:
logger.debug(Utility.LOG.transaction(header.get("code"), header.get("type"), header.get("id"), header.get("msisdn"), "Begin resolving execution for hook."));
if (state.isScriptExecuted()) {
if ("true".equals(state.getAdvance())) {
tree.promote();
this.resolveExecution(ctx, header, tree);
} else {
this.exit(ctx, header, this.resolveStatus(7));
}
} else {
state.setHookExecuted(true);
Map<String, String> configs = Configurations.HANDLER.getHookConfigurations(state.getHook());
tree.setState(new StateNode(statename,
state.getId(),
configs.get("name"),
configs.get("script"),
configs.get("result"),
state.getNextid(),
state.getCode(),
state.getHook(),
state.getReplay(),
StringUtils.defaultIfEmpty(configs.get("advance"), "true")));
this.resolveAndExecuteScript(ctx, header, tree);
}
logger.debug(Utility.LOG.transaction(header.get("code"), header.get("type"), header.get("id"), header.get("msisdn"), "End of resolving execution for hook."));
break;
case REPLAY:
logger.debug(Utility.LOG.transaction(header.get("code"), header.get("type"), header.get("id"), header.get("msisdn"), "Begin resolving execution for replay."));
state.setReplayExecuted(true);
if (!this.replay(ctx, header, state.getStateName() == State.REPLAY ? tree
: tree.setState(new StateNode(
statename,
state.getId(),
state.getName(),
"?".equals(state.getScript()) ? tree.getState(2).getScript() : state.getScript(),
state.getResult(),
state.getNextid(),
state.getCode(),
state.getHook(),
state.getReplay())))) {
tree.promote();
this.resolveExecution(ctx, header, tree);
}
logger.debug(Utility.LOG.transaction(header.get("code"), header.get("type"), header.get("id"), header.get("msisdn"), "End of resolving execution for replay."));
break;
}
}
问题
我已经有了一个可行的解决方案,但我觉得它不是最好的。现在我们正在进行性能测试,我发现处理速度很慢,我相信它会更好。您会推荐哪种数据结构来映射此 xml and/or 设计模式?我希望这不像是一个开放式问题。
谢谢
您可以实现包含所有属性的 Step
class 并将所有步骤存储在 LinkedHashSet:
public class Step{
private int id;
private String name;
private String script;
private String result;
// getters and setters and constructors
}
然后制作一个 LinkedHashSet 步骤,它们将按照您插入它们的顺序相互链接:
Step step1=new Step(1, "momo.debit", "step-1", "0000");
Step step2=new Step(2, "deposit", "step-2", "0000");
// create all your steps here
LinkedHashSet<Step> steps = new LinkedHashSet<Step>();
steps.add(step1);
steps.add(step2);
查看这些示例以获取更多信息:
感觉有点像味道问题。
如果我理解正确的话,你有数据驱动流,我的意思是你有一些数据(在这种情况下以 xml 和脚本的形式)指示将采取什么行动(所以它是在编译时不知道,但你使用伪语言来描述这个流程)。
我通常的做法是制作一个命令队列并一次性填充所有命令,然后执行它,而不是执行命令、加载下一个脚本、执行命令。然而,这涉及到制作你自己的伪语言编译器可以这么说:)。
如果您的 xml 有点微不足道,您可以轻松解决。
对于您的 xml 示例,命令队列将导致
1 - 2 - 3
因为你没有分支(在我看来你只是通过它们不管)
您将必须预加载一些脚本(以进行额外优化):
$err-5000
$notify
etc
现在您可以在全局级别决定哪些脚本将被缓存,哪些不被缓存。