Outline
(As articles using jBPM 6 BPMN2 Modeler to create flow ) as shown, we demonstrate by how jBPM 6 eclipse plug-in step by BPMN2 creation process, process step we finally created as follows:
As we will process known as jBPM Rewards flow, which is the requester filed a bonus, the process began, the request is first allowed to apply for awards PM, then was allowed to HR, the process ends, the requester rewarded. From a process to say, rewards processes including six nodes:
- Start - process start event, expressed rewards flow begins
- Start - Script Task node, run-time process Script execution node, the Start Script is java code (refer to create jBPM 6 Process Modeler using the BPMN2 ), i.e. run java code segments that perform the process when the node
- Approval by PM - Human task node, the node that is running this process must be completed before someone is involved in this role at the john PM to complete this node
- Approval by HR - Human task node, the node that is running this process must be completed before someone is involved, the department mary play the role of HR to accomplish this node
- End - Script Task node, run-time process Script execution node, End of Script java code (refer to create jBPM 6 Process Modeler using the BPMN2 ), i.e. run java code segments that perform the process when the node
- End - end event process, indicating the end of the process run rewards
The main contents of the present example can be summarized as:
- Run jBPM 6 Human tasks
- Data storage (JPA)
- Using Maven quickly build jBPM 6 Test Engineering
Run jBPM 6 Human tasks
Let 6 rewards example jBPM run through the following steps.
rewards.bpmn ( created using jBPM 6 BPMN2 Modeler process design) located in the classpath org / jbpm / demo directory.
kmodule.xml located in the classpath META-INF, shown as follows:
<kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule">
<kbase name="kbase" packages="org.jbpm.demo" />
</kmodule>
ProcessMain running processes using rewards, ProcessMain shown as follows:
package org.jbpm.demo.rewards;
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 ProcessMain {
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", "kylin");
ksession.startProcess("org.jbpm.demo.rewards", params);
// let john execute Task 1
List<TaskSummary> list = taskService.getTasksAssignedAsPotentialOwner("john", "en-UK");
TaskSummary task = list.get(0);
taskService.start(task.getId(), "john");
taskService.complete(task.getId(), "john", null);
// let mary execute Task 2
list = taskService.getTasksAssignedAsPotentialOwner("mary", "en-UK");
task = list.get(0);
taskService.start(task.getId(), "mary");
taskService.complete(task.getId(), "mary", 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");
}
}
The code is: https://github.com/kylinsoong/jbpm-6-examples/blob/master/rewards/src/main/java/org/jbpm/demo/rewards/ProcessMain.java
ProcessMain will run the following output:
process 33 started by kylin
john approved process 33
mary approved process 33
process 33 finished
Data storage (JPA)
As we used during operation to store data during operation of H2 in the database, jBPM 6 to operate the database, RuntimeEnvironmentBuilder afferent created EntityManagerFactory (org.jbpm.persistence.jpa) by JPA / hibernate, we can in jbpm-test- 6.0.0.Final.jar found in JPA persist-unit is defined, persistence.xml located in jbpm-test-6.0.0.Final.jar / META-INF, as follows:
<persistence
version="2.0"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd
http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd"
xmlns:orm="http://java.sun.com/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="org.jbpm.persistence.jpa" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/jbpm-ds</jta-data-source>
<mapping-file>META-INF/JBPMorm.xml</mapping-file>
<mapping-file>META-INF/Taskorm.xml</mapping-file>
<class>org.jbpm.persistence.processinstance.ProcessInstanceInfo</class>
<class>org.drools.persistence.info.SessionInfo</class>
<class>org.drools.persistence.info.WorkItemInfo</class>
<class>org.jbpm.process.audit.ProcessInstanceLog</class>
<class>org.jbpm.process.audit.NodeInstanceLog</class>
<class>org.jbpm.process.audit.VariableInstanceLog</class>
<class>org.jbpm.persistence.correlation.CorrelationKeyInfo</class>
<class>org.jbpm.persistence.correlation.CorrelationPropertyInfo</class>
<!-- manager -->
<class>org.jbpm.runtime.manager.impl.jpa.ContextMappingInfo</class>
<class>org.jbpm.services.task.impl.model.AttachmentImpl</class>
<class>org.jbpm.services.task.impl.model.ContentImpl</class>
<class>org.jbpm.services.task.impl.model.BooleanExpressionImpl</class>
<class>org.jbpm.services.task.impl.model.CommentImpl</class>
<class>org.jbpm.services.task.impl.model.DeadlineImpl</class>
<class>org.jbpm.services.task.impl.model.CommentImpl</class>
<class>org.jbpm.services.task.impl.model.DeadlineImpl</class>
<class>org.jbpm.services.task.impl.model.DelegationImpl</class>
<class>org.jbpm.services.task.impl.model.EscalationImpl</class>
<class>org.jbpm.services.task.impl.model.GroupImpl</class>
<class>org.jbpm.services.task.impl.model.I18NTextImpl</class>
<class>org.jbpm.services.task.impl.model.NotificationImpl</class>
<class>org.jbpm.services.task.impl.model.EmailNotificationImpl</class>
<class>org.jbpm.services.task.impl.model.EmailNotificationHeaderImpl</class>
<class>org.jbpm.services.task.impl.model.PeopleAssignmentsImpl</class>
<class>org.jbpm.services.task.impl.model.ReassignmentImpl</class>
<class>org.jbpm.services.task.impl.model.TaskImpl</class>
<class>org.jbpm.services.task.impl.model.TaskDataImpl</class>
<class>org.jbpm.services.task.impl.model.UserImpl</class>
<!--BAM for task service -->
<class>org.jbpm.services.task.impl.model.BAMTaskSummaryImpl</class>
<properties>
<property name="hibernate.max_fetch_depth" value="3"/>
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
<!-- BZ 841786: AS7/EAP 6/Hib 4 uses new (sequence) generators which seem to cause problems -->
<property name="hibernate.id.new_generator_mappings" value="false" />
<property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.BitronixJtaPlatform" />
</properties>
</persistence-unit>
</persistence>
Using Maven quickly build jBPM 6 Test Engineering
This example also demonstrates how to use Maven to rapidly build jBPM 6 test engineering, test engineering to create jBPM 6 only need to rely on a dependent package:
<dependency>
<groupId>org.jbpm</groupId>
<artifactId>jbpm-test</artifactId>
<version>${jbpm.version}</version>
</dependency>
org.jbpm.test.JbpmJUnitBaseTestCase provides createRuntimeManager constructor () method to quickly build run jBPM 6 Human tasks environment, in the previous version of jBPM (such as jBPM 5), we need to create a datasource own, create TaskService, 6 simplified jBPM these operations provide RuntimeManager, we can complete everything by RuntimeManager, as follows ProcessTest demonstrates how to use JbpmJUnitBaseTestCase quick run jBPM 6 Human tasks process. ProcessTest shown as follows:
package org.jbpm.demo.rewards;
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 ProcessTest extends JbpmJUnitBaseTestCase{
public ProcessTest() {
super(true, true);
}
@Test
public void testRewardsProcess() {
RuntimeManager manager = createRuntimeManager("org/jbpm/demo/rewards.bpmn");
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("org.jbpm.demo.rewards", params);
// let john execute Task 1
List<TaskSummary> list = taskService.getTasksAssignedAsPotentialOwner("john", "en-UK");
TaskSummary task = list.get(0);
taskService.start(task.getId(), "john");
taskService.complete(task.getId(), "john", null);
// let mary execute Task 2
list = taskService.getTasksAssignedAsPotentialOwner("mary", "en-UK");
task = list.get(0);
taskService.start(task.getId(), "mary");
taskService.complete(task.getId(), "mary", null);
manager.disposeRuntimeEngine(engine);
}
}
ProcessTest located: https://github.com/kylinsoong/jbpm-6-examples/blob/master/rewards/src/main/java/org/jbpm/demo/rewards/ProcessTest.java
Run ProcessTest same output:
process 1 started by kylin
john approved process 1
mary approved process 1
process 1 finished
Reproduced in: https: //my.oschina.net/iwuyang/blog/197201