gpt4 book ai didi

java - 使用什么数据结构或设计模式来映射定义的步骤序列

转载 作者:行者123 更新时间:2023-12-02 05:00:04 25 4
gpt4 key购买 nike

我们有一个处理不同类型金融交易的 Java 应用程序。这些事务具有不同的流程,为了系统灵 active (尤其是用户),我们引入了一个 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>

剧情简介
每个步骤都包含一个步骤脚本、步骤脚本的预期结果、步骤脚本失败时的纠错脚本,以及称为 Hook 的拦截脚本,这些脚本在执行步骤脚本或错误脚本之前执行一些工作。

当前设计我有以下类(class)

名为 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;
}

}

状态树,存储系统内正在进行的交易的所有正在执行的步骤(状态节点)

最终类 StateTree 实现可序列化{

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 和此结构来处理事务。因此,我们会为进入系统的每个事务创建一个新的状态树,并将其保存到数据库中,然后对于执行的每个步骤、错误或钩子(Hook),我们创建一个新的状态节点并将其保存到状态树中。我希望这足够清楚。

实际执行
为了确定执行哪个状态以及在哪个步骤执行,我有一个名为“解析执行”的递归方法。我希望它对读者来说是直观的:

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 和/或设计模式?我希望这不像是一个开放式问题。谢谢

最佳答案

这确实感觉有点像 flavor 问题。

如果我理解正确的话,你有数据驱动流,我的意思是你有一些数据(在本例中以 xml 和脚本的形式)指示将采取什么操作(所以在编译时不知道,但你使用伪语言来描述这个流程)。

我通常的方法是创建一个命令队列并一次填充所有命令,然后执行它,而不是执行命令,加载下一个脚本,执行命令。然而,这涉及到制作你自己的伪语言编译器,可以这么说:)。

如果您的 xml 有点琐碎,您可以轻松摆脱它。

对于您的 xml 示例,命令队列将导致

1 - 2 - 3

因为你没有分支(在我看来,无论如何你都会经历它们)

您必须预加载一些脚本(用于额外优化):

 $err-5000
$notify
etc

现在您可以在全局级别决定哪些脚本将被缓存,哪些不会。

关于java - 使用什么数据结构或设计模式来映射定义的步骤序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28362206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com