從零開始學jBPM6(五)- BPMN2.0流程編輯
阿新 • • 發佈:2019-01-29
ps: 架構師交流群(QQ群號
304287620)
eclipse BPMN2 Modeler外掛
http://www.eclipse.org/bpmn2-modeler/
上一講在eclipse直接生成例子工程,這一講我們將通過編輯BPMN,手動建立第一個流程
1 新建一個空jBPM專案
執行ant start.eclipse開啟eclipse
2 新建jBPM Process Diagram
右擊src/main/resouces
填寫"Process name", "Process ID"等
流程圖自動生成流程起點start
3 建立script task節點
在"Script Task"寫入需執行java程式碼
long id = kcontext.getProcessInstance().getId();
String name = (String)kcontext.getVariable("recipient");
String log = "process" + id + " started by " + name;
System.out.println(log);
4 建立user task節點
在"Script Task"寫入需執行java程式碼
long id = kcontext.getProcessInstance().getId(); String name = (String)kcontext.getVariable("recipient"); String log = "process" + id + " started by " + name; System.out.println(log);
在"Actor"中新增使用者tom
在"On End Script"中寫入需執行的java程式碼
建立第二個User Script,並新增使用者green
5 建立end節點並寫入java
6 main方法執行流程圖
package com.sample; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; import org.jbpm.test.JBPMHelper; import org.kie.api.KieBase; import org.kie.api.KieServices; import org.kie.api.runtime.KieContainer; import org.kie.api.runtime.KieSession; import org.kie.api.runtime.manager.RuntimeEngine; import org.kie.api.runtime.manager.RuntimeEnvironmentBuilder; import org.kie.api.runtime.manager.RuntimeManager; import org.kie.api.runtime.manager.RuntimeManagerFactory; import org.kie.api.task.TaskService; import org.kie.api.task.model.TaskSummary; public class Leave { public static void main(String[] args) { KieServices ks = KieServices.Factory.get(); KieContainer kContainer = ks.getKieClasspathContainer(); KieBase kbase = kContainer.getKieBase("kbase"); RuntimeManager manager = createRuntimeManager(kbase); RuntimeEngine engine = manager.getRuntimeEngine(null); KieSession ksession = engine.getKieSession(); TaskService taskService = engine.getTaskService(); Map<String, Object> params = new HashMap<String, Object>(); params.put("recipient", "test"); ksession.startProcess("com.sample.leave", params); // let john execute Task 1 List<TaskSummary> list = taskService.getTasksAssignedAsPotentialOwner("tom", "en-UK"); TaskSummary task = list.get(0); taskService.start(task.getId(), "tom"); taskService.complete(task.getId(), "tom", null); // let mary execute Task 2 list = taskService.getTasksAssignedAsPotentialOwner("green", "en-UK"); task = list.get(0); taskService.start(task.getId(), "green"); taskService.complete(task.getId(), "green", null); manager.disposeRuntimeEngine(engine); System.exit(0); } private static RuntimeManager createRuntimeManager(KieBase kbase) { JBPMHelper.startH2Server(); JBPMHelper.setupDataSource(); EntityManagerFactory emf = Persistence.createEntityManagerFactory("org.jbpm.persistence.jpa"); RuntimeEnvironmentBuilder builder = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().entityManagerFactory(emf).knowledgeBase(kbase); return RuntimeManagerFactory.Factory.get().newSingletonRuntimeManager(builder.get(), "com.sample:example:1.0"); } }
最後得到如下結果
process7 started by test
tom approved process 7
green approved process 7
process 7 finished
7 junit執行流程圖
package com.sample;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jbpm.test.JbpmJUnitBaseTestCase;
import org.junit.Test;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.manager.RuntimeEngine;
import org.kie.api.runtime.manager.RuntimeManager;
import org.kie.api.task.TaskService;
import org.kie.api.task.model.TaskSummary;
public class LeaveTest extends JbpmJUnitBaseTestCase{
public LeaveTest() {
super(true, true);
}
@Test
public void testRewardsProcess() {
RuntimeManager manager = createRuntimeManager("com/sample/leave.bpmn2");
RuntimeEngine engine = manager.getRuntimeEngine(null);
KieSession ksession = engine.getKieSession();
TaskService taskService = engine.getTaskService();
Map<String, Object> params = new HashMap<String, Object>();
params.put("recipient", "kylin");
ksession.startProcess("com.sample.leave", params);
// let john execute Task 1
List<TaskSummary> list = taskService.getTasksAssignedAsPotentialOwner("tom", "en-UK");
TaskSummary task = list.get(0);
taskService.start(task.getId(), "tom");
taskService.complete(task.getId(), "tom", null);
// let mary execute Task 2
list = taskService.getTasksAssignedAsPotentialOwner("green", "en-UK");
task = list.get(0);
taskService.start(task.getId(), "green");
taskService.complete(task.getId(), "green", null);
manager.disposeRuntimeEngine(engine);
}
}
reference:
https://docs.jboss.org/jbpm/release/6.5.0.Final/jbpm-docs/html/ch06.html