我们有一个处理不同类型的金融交易的java应用程序。这些事务具有不同的流程,并且对于系统灵活性(esp用户),我们引入了一个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>
概要
每个步骤都包含一个步骤脚本,步骤脚本的预期结果,错误更正脚本,使步骤脚本失败,还拦截称为钩子的脚本,这些脚本在执行步骤脚本或错误脚本之前执行一些工作。
当前设计 我有以下课程
名为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类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和this结构来处理事务。因此,我们为每个进入系统的事务创建一个新的状态树,并将其持久保存到数据库,然后对于每个步骤,错误或挂钩执行,我们创建一个新的状态节点并将其保存到状态树中。我希望这很清楚。
实际执行
要确定要执行哪个状态,以及在哪个步骤中我有一个名为resolve execution的递归方法。我希望这对读者来说很直观:
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和/或设计模式?我希望这不是一个开放式问题。
谢谢
答案 0 :(得分:1)
您可以实施包含所有属性的Step
类,并将所有步骤存储在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);
请查看这些示例以获取更多信息:
答案 1 :(得分:1)
感觉有点像味道问题。
如果我理解正确你有数据驱动流,我的意思是你有一些数据(在这种情况下是xml和脚本的形式),它决定了将采取什么行动(因此在编译时不知道,但你使用伪语言来描述这个流程。)
我通常的做法是创建命令队列并立即填充所有命令,然后执行它,而不是执行命令,加载下一个脚本,执行命令。然而,这涉及到制作您自己的伪语言编译器:)。
如果你的xmls有点琐碎,你可以轻松逃脱。
对于xml示例,命令队列将导致
1 - 2 - 3
因为你没有分支(在我看来你无论如何都要通过它们)
您必须预加载一些脚本(进行额外优化):
$err-5000
$notify
etc
现在,您可以在全局级别决定缓存哪些脚本,哪些脚本不会缓存。