一、流程图:
二、设置与获取变量:
三、代码:
public class ProcessVariableTest {
private ProcessEngine processEngine = Configuration.getProcessEngine();
//部署(添加)
@Test
public void deploy() throws Exception {
String deployId = processEngine.getRepositoryService().createDeployment()
.addResourceFromClasspath("state/testState.jpdl.xml")
.addResourceFromClasspath("state/testState.png")
.deploy();
System.out.println("部署成功:deployId=="+deployId);
}
//启动流程实例
@Test
public void start() throws Exception {
ProcessInstance processInstance = processEngine.getExecutionService().startProcessInstanceByKey("testState");
System.out.println("启动成功:id=="+processInstance.getId()
+ ", state=="+processInstance.getState()
+ ",pid=="+processInstance.getProcessDefinitionId());
}
//设置变量
//executionId==processInstance.getId()
@Test
public void setVariable() throws Exception {
String executionId = "testState.140001";
//processEngine.getExecutionService().setVariable(executionId, "请假天数", 12);
Form form = new Form(1L, "我要请假,我是张三"); // 通过指定id来模拟一个游离状态的对象
processEngine.getExecutionService().setVariable(executionId, "form", form);
}
//获取变量
@Test
public void getVariable() throws Exception {
String executionId = "testState.140001";
//Object variable = processEngine.getExecutionService().getVariable(executionId, "请假天数");
//Integer days = (Integer) variable;
//System.out.println("请假天数==" + days);
Form form = (Form) processEngine.getExecutionService().getVariable(executionId, "form");
System.out.println(form);
}
/**
{
ExecutionService executionService = processEngine.getExecutionService();
TaskService taskService = processEngine.getTaskService();
// ============ 设置变量 ========================
executionService.setVariable(executionId, name, value); // 设置一个变量
executionService.setVariables(executionId, variablesMap); // 设置多个变量
taskService.setVariables(taskId, variables); // 设置多个变量
executionService.startProcessInstanceByKey(processDefinitionKey, variablesMap); // 启动流程实例时,先设置一些变量
taskService.completeTask(taskId, variablesMap); // 真正办理完任务前先设置一些变量
// ============ 获取变量 ========================
executionService.getVariable(executionId, variableName); // 获取一个变量
executionService.getVariableNames(executionId); // 返回Set<String>,是所有变量的名称集合
executionService.getVariables(executionId, variableNames); //获取多个变量,返回Map<String,Object>,表示指定名称的变量信息
taskService.getVariable(taskId, variableName);
taskService.getVariableNames(taskId);
taskService.getVariables(taskId, variableNames);
}
*/
}
四、实体类:
public class Form /* implements java.io.Serializable */{
private Long id;
private String title;
// ...
public Form() {
}
public Form(String title) {
this.title = title;
}
public Form(Long id, String title) {
this.id = id;
this.title = title;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public String toString() {
return "[Form: id=" + id + ", title=" + title + "]";
}
}
五、配置文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.itcast.processvariable">
<class name="Form" table="itcast_form" lazy="false">
<id name="id">
<generator class="native"/>
</id>
<property name="title"/>
</class>
</hibernate-mapping>
六、jbpm.hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///jbpm4?useUnicode=true&characterEncoding=utf8</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping resource="jbpm.repository.hbm.xml" />
<mapping resource="jbpm.execution.hbm.xml" />
<mapping resource="jbpm.history.hbm.xml" />
<mapping resource="jbpm.task.hbm.xml" />
<mapping resource="jbpm.identity.hbm.xml" />
<mapping resource="com/itcast/processvariable/Form.hbm.xml" />
</session-factory>
</hibernate-configuration>