node节点类型适用于当你想要在节点中编写自己的代码的情形。通过设置action来帮定我们自己的商业逻辑。
<start-state name="start">
<transition name="tr1" to="node1"></transition>
</start-state>
<node name="node1">
<action class="com.jeffentest.JeffenActionHandler">
<msg>HELLO</msg>
</action>
<transition name="tr2" to="end1"></transition>
</node>
<end-state name="end1"></end-state>
------------------------------------
package com.jeffentest;
import org.jbpm.graph.def.ActionHandler;
import org.jbpm.graph.exe.ExecutionContext;
public class JeffenActionHandler implements ActionHandler {
private static final long serialVersionUID = 1L;
private String msg;
public void execute(ExecutionContext executionContext) {
System.out.println(msg+"jeffen");
//可以指定流程的运行
//executionContext.leaveNode(transitionName);
}
}
----------------------------------------
private static void run(){
JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
try {
long processInstanceId =1;
processInstance = jbpmContext.loadProcessInstance(processInstanceId);
Token token = processInstance.getRootToken();
System.out.println(token.getNode());
token.signal();
System.out.println(token.getNode());
token.signal();
System.out.println(token.getNode());
jbpmContext.save(processInstance);
}finally {
jbpmContext.close();
}
}
---------------------------------------
StartState(start)
HELLOjeffen
Node(node1)
EndState(end1)
自动节点实现:
在上面的node的介绍中,我们可以看到action和node的合作,这里做一点扩展,看下面的代码就很容易明白。
public class AmountUpdate implements ActionHandler {
public void execute(ExecutionContext ctx) throws Exception {
//从数据库中得到相应的信息,进行一定的商务计算,根据不同的值选择不同的变迁,流程自动运行下去
if (result > 5000) {
ctx.leaveNode(ctx, "big amounts");
} else {
ctx.leaveNode(ctx, "small amounts");
}
}
}