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 params = new HashMap();
params.put("recipient", "test");
ksession.startProcess("com.sample.leave", params);
// let john execute Task 1
List 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 params = new HashMap();
params.put("recipient", "kylin");
ksession.startProcess("com.sample.leave", params);
// let john execute Task 1
List 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