1.processdefinition.xml
<?xml version="1.0" encoding="UTF-8"?> <process-definition xmlns="urn:jbpm.org:jpdl-3.2" name="qingjia"> <task> <controller> <variable name="flag"></variable> </controller> </task> <start-state name="申请"> <task name="getInformation"> <controller> <variable name="name"></variable> <variable name="beginTime"></variable> <variable name="duringTime"></variable> </controller> </task> <transition name="to_pingshen" to="评审"> <action name="getReason" class="com.qingjia.action.GetReasonAction"> <reason>get ill</reason> </action> </transition> </start-state> <task-node name="评审"> <task name="getResult"> <controller> <variable name="result"></variable> </controller> </task> <transition to="end" name="to_end"> <action name="printlnReasult" class="com.qingjia.action.PrintlnReasultAction"> </action> </transition> </task-node> <end-state name="end"></end-state> </process-definition>
2.processdefinition.jpg
3.GetReasonAction.java
package com.qingjia.action;
import org.jbpm.graph.def.ActionHandler;
import org.jbpm.graph.exe.ExecutionContext;
public class GetReasonAction implements ActionHandler {
private static final long serialVersionUID = 1L;
private String reason;
public String getReason() {
return reason;
}
public void setReason(String reason) {
this.reason = reason;
}
public void execute(ExecutionContext executionContext) throws Exception {
executionContext.getContextInstance().setVariable("resaon", reason);
// 将前台输入的"name"属性值传进来
String name = (String) executionContext.getContextInstance()
.getVariable("name");
// 设置全局变量
executionContext.getContextInstance().setVariable("flag", "shengqing");
System.out.println(name);
System.out.println(reason);
System.out.println((String) executionContext.getContextInstance()
.getVariable("flag"));
}
}
PrintlnReasultAction.java
package com.qingjia.action;
import org.jbpm.graph.def.ActionHandler;
import org.jbpm.graph.exe.ExecutionContext;
public class PrintlnReasultAction implements ActionHandler {
private static final long serialVersionUID = 1L;
public void execute(ExecutionContext executionContext) throws Exception {
Object result = executionContext.getContextInstance().getVariable(
"result");
if (result.equals("Yes")) {
System.out.println("This holiday is agreed!");
} else {
System.out.println("This holiday is disagreed!");
}
executionContext.getContextInstance().setVariable("flag", "pingshen");
System.out.println((String) executionContext.getContextInstance()
.getVariable("flag"));
}
}
QianTai.java
package com.qingjia.action;
import org.jbpm.JbpmConfiguration;
import org.jbpm.JbpmContext;
import org.jbpm.context.exe.ContextInstance;
import org.jbpm.graph.def.ProcessDefinition;
import org.jbpm.graph.exe.ProcessInstance;
import org.jbpm.graph.exe.Token;
public class QianTai {
private JbpmConfiguration config;
private JbpmContext context;
private ProcessDefinition processDefinition;
/**
* 初始化ProcessDefinition
*/
public QianTai() {
config = JbpmConfiguration.getInstance();
context = config.createJbpmContext();
processDefinition = context.getGraphSession()
.findLatestProcessDefinition("qingjia");
}
/**
* 创建流程,进行请假申请
*
* @return 流程ID
*/
long shenqing() {
ProcessInstance processInstance = processDefinition
.createProcessInstance();
ContextInstance contextInstance = processInstance.getContextInstance();
// 填写请假单,与jbpm引擎交互
contextInstance.setVariable("name", "xiaoming");
contextInstance.setVariable("beginTime", "080426");
contextInstance.setVariable("duringTime", "5");
long ID = processInstance.getId();
return ID;
}
/**
* 得到流程的令牌"token"
*/
Token executeProcess(long ID) {
ProcessInstance processInstance = context.loadProcessInstance(ID);
Token token = processInstance.getRootToken();
return token;
}
/**
* 填写审批意见
*/
void pingshen(long ID) {
ProcessInstance processInstance = context.loadProcessInstance(ID);
ContextInstance contextInstance = processInstance.getContextInstance();
// 输入评审意见
contextInstance.setVariable("result", "NO");
}
/**
* 流程执行总过程
*/
public static void main(String[] args){
//前台实例调用各个方法
QianTai qianTai = new QianTai();
long id=qianTai.shenqing();
Token token=qianTai.executeProcess(id);
//执行"to_pingshen" 中的action
token.signal("to_pingshen");
System.out.println((String)token.getProcessInstance().getContextInstance().getVariable("flag"));
//进行请假评审
qianTai.pingshen(id);
//执行"to_end" 中的action
token.signal("to_end");
System.out.println((String)token.getProcessInstance().getContextInstance().getVariable("flag"));
System.out.println("Process"+id+" id end!");
}
}
说明:(1)使流程一步一步执行,利用的是Single(),而调用这个方法有2种方式:
ProcessInstance.single();
由ProcessInstance先得到Token(即流程执行的“令牌”),再调用该Token.Single();
(2)得到节点的方式有(但是无法得到该节点的Variable):
Token.getNode():得到当前节点;
ProcessDefinition.getNode(“”):利用节点名得到任意节点;
(3) Variable的设计原则及Variable的获得方法:
要在节点间传递的Variable设计为全局的==》在<ProceseeDefinition>中加入<task><controller><variable>;
要在节点内与Transition间传递的Variable要放在节点中==》在相应节点中加入<task><controller><variable>;
==》获得总流程中的Variable:ProcessInstance.getContextInstance()
Token. getContextInstance()
得到Variable就要通过ContextInstance.get/setVariable()
==》获得Action中的Variable:ExecutionContext.getContextInstance()
在Action中也可以定义Variable,<名字是任意的>;
(4)在“ProjectName/src/main”下建类其实质是在“ProjectName/src/main/java”下建,