剛剛開始學習JBPM簡單的入門實例:
概念:
ProcessDefinition
定義了流程的業務邏輯,包含業務節點和流轉。是有向圖結構
ProcessInstance
ProcessDefinition 執行期間的體現.包含了流程定義被解釋之後的一些信息,諸如開始時間,結束時間和其他相關聯的信息
Task
是ProcessDefinition的組成部分,定義了活動的具體內容
TaskInstance
Task被解釋執行時的信息,有開始時間,結束時間,參與者等
有create start end 三種狀態
Actor
活動的參與者
jBPM目前只需要actor的Id,對actor的具體解釋是由業務應用層負責
任務創建時可以指定任務的參與者,也可以不指定
Token
是一個指針(運行期的概念),代表著一條執行路徑,指向當前流程中正在執行的節點
在實例的運行期間,tokens 可能是一樹形結構同一時刻,可能存在多個token,
但是只有一個root token
我用的是MySQL數據庫,首先修改 hibernate.cfg.xml
要注意引入數據庫驅動時用mysql-connector-java-3.2.0-alpha-bin版本的jar包在測試時回有問題,我用的是mysql-connector-java-5.1.5-bin版本的jar包
Java代碼
<!-- JDBC connection properties (begin) -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///jbpm</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<!-- JDBC connection properties (end) -->
流程定義文件:processdefinition.xml
Java代碼
<?xml version="1.0" encoding="UTF-8"?>
<process-definition xmlns="urn:jbpm.org:jpdl-3.2" name="test">
<start-state name="start">
<transition to="fillForm"></transition>
</start-state>
<task-node name="fillForm">
<task name="填寫報銷單">
<assignment actor-id="employee"></assignment>
</task>
<transition to="managerDecide"></transition>
</task-node>
<task-node name="managerDecide">
<task name="經理審批">
<assignment actor-id="manager"></assignment>
</task>
<transition to="finaceAudit"></transition>
</task-node>
<task-node name="finaceAudit">
<task name="財務審批">
<assignment actor-id="finace"></assignment>
</task>
<transition to="end"></transition>
</task-node>
<end-state name="end"></end-state>
</process-definition>
編程步驟
1.創建流程定義
2.部署流程定義
3.啟動流程實例
4.獲取任務列表
5.執行任務
6.結束任務
在Junit中進行測試:
Java代碼
package cn.senta.test;
import java.util.List;
import org.jbpm.JbpmConfiguration;
import org.jbpm.JbpmContext;
import org.jbpm.db.GraphSession;
import org.jbpm.graph.def.ProcessDefinition;
import org.jbpm.graph.exe.ProcessInstance;
import org.jbpm.taskmgmt.exe.TaskInstance;
import junit.framework.TestCase;
public class TestJBPM extends TestCase {
/**
* 創建表結構
*/
public void testCreateSchema() {
JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();
jbpmConfiguration.createSchema();
System.out.println("the database is created successfully!");
}
/**
* 部署流程定義
*/
public void testDeployProcessDifinition() {
JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();
JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
try {
ProcessDefinition pd = ProcessDefinition
.parseXmlResource("cn/senta/bx/processdefinition.xml");
jbpmContext.deployProcessDefinition(pd);
} finally {
if (jbpmContext != null) {
jbpmContext.close();
}
}
System.out.println("the deploy is successful!");
}
/**
* 啟動流程實例
*/
public void testStartProcessInstance() {
JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();
JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
try {
GraphSession graphSession = jbpmContext.getGraphSession();
ProcessDefinition processDefinition = graphSession
.findLatestProcessDefinition("test");
ProcessInstance processInstance = new ProcessInstance(
processDefinition);
jbpmContext.save(processInstance);
processInstance.signal();
} finally {
if (jbpmContext != null) {
jbpmContext.close();
}
}
System.out.println("the startProcess is successful !");
}
/**
* 獲取任務列表
*/
@SuppressWarnings("unchecked")
public void testGetTaskList() {
String actorId = "employee";
JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();
JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
try {
List<TaskInstance> tasks = jbpmContext.getTaskList(actorId);
for (TaskInstance ti : tasks) {
System.out.println("id=" + ti.getId() + ", name="
+ ti.getName() + ", create=" + ti.getCreate()
+ ", start=" + ti.getStart() + ", end=" + ti.getEnd());
}
} finally {
if (jbpmContext != null) {
jbpmContext.close();
}
}
System.out.println("you get the tasklist successfully!");
}
/**
* 執行任務
*/
public void testStartTask() {
long taskInstanceId = 1;
JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();
JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
try {
TaskInstance taskInstance = jbpmContext
.getTaskInstance(taskInstanceId);
taskInstance.start();
jbpmContext.save(taskInstance);
} finally {
if (jbpmContext != null) {
jbpmContext.close();
}
}
System.out.println("the task is started successfully!");
}
/**
* 結束任務
*/
public void testEndTask(){
long taskInstanceId=1;
JbpmConfiguration jbpmConfiguration=JbpmConfiguration.getInstance();
JbpmContext jbpmContext=jbpmConfiguration.createJbpmContext();
try{
TaskInstance taskInstance=jbpmContext.getTaskInstance(taskInstanceId);
taskInstance.end();
jbpmContext.save(taskInstance);
}finally{
if(jbpmContext!=null){
jbpmContext.close();
}
}
System.out.println("the task is ended successfully!");
}
}
工程目錄結構