BAEL-379 A Guide to jBPM with Java (#6619)

* BAEL-379 A Guide to jBPM with Java

* BAEL-379

* BAEL-379 A Guide to jBPM with Java

* BAEL-379 A Guide to jBPM with Java

* BAEL-379 A Guide to jBPM with Java

* BAEL-379 A Guide to jBPM with Java
This commit is contained in:
Saurabh Kedia 2019-04-02 10:37:26 +05:30 committed by maibin
parent 6003634ad5
commit 8a9cfe80e9
8 changed files with 248 additions and 0 deletions

43
libraries2/pom.xml Normal file
View File

@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>libraries2</artifactId>
<name>libraries2</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<properties>
<jbpm.version>6.0.0.Final</jbpm.version>
</properties>
<repositories>
<repository>
<id>jboss-public-repository-group</id>
<name>JBoss Public Repository Group</name>
<url>http://repository.jboss.org/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>daily</updatePolicy>
</snapshots>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.jbpm</groupId>
<artifactId>jbpm-test</artifactId>
<version>${jbpm.version}</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,19 @@
package com.baeldung.jbpm;
import org.kie.api.runtime.manager.Context;
import org.kie.internal.runtime.manager.context.EmptyContext;
import com.baeldung.jbpm.engine.WorkflowEngine;
import com.baeldung.jbpm.engine.WorkflowEngineImpl;
public class WorkflowProcessMain {
public static void main(String[] args) {
WorkflowEngine workflowEngine = new WorkflowEngineImpl();
String processId = "com.baeldung.bpmn.helloworld";
String kbaseId = "kbase";
String persistenceUnit = "org.jbpm.persistence.jpa";
Context<String> initialContext = EmptyContext.get();
workflowEngine.runjBPMEngineForProcess(processId, initialContext, kbaseId, persistenceUnit);
}
}

View File

@ -0,0 +1,10 @@
package com.baeldung.jbpm.engine;
import org.kie.api.runtime.manager.Context;
import org.kie.api.runtime.process.ProcessInstance;
public interface WorkflowEngine {
public ProcessInstance runjBPMEngineForProcess(String processId, Context<String> initialContext, String kbaseId, String persistenceUnit);
}

View File

@ -0,0 +1,66 @@
package com.baeldung.jbpm.engine;
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.Context;
import org.kie.api.runtime.manager.RuntimeEngine;
import org.kie.api.runtime.manager.RuntimeEnvironment;
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.runtime.process.ProcessInstance;
public class WorkflowEngineImpl implements WorkflowEngine {
@Override
public ProcessInstance runjBPMEngineForProcess(String processId, Context<String> initialContext, String kbaseId, String persistenceUnit) {
RuntimeManager manager = null;
RuntimeEngine engine = null;
ProcessInstance pInstance = null;
try {
KieBase kbase = getKieBase(kbaseId);
manager = createJBPMRuntimeManager(kbase, persistenceUnit);
engine = manager.getRuntimeEngine(initialContext);
pInstance = executeProcessInstance(processId, manager, initialContext, engine);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (manager != null && engine != null)
manager.disposeRuntimeEngine(engine);
System.exit(0);
}
return pInstance;
}
private ProcessInstance executeProcessInstance(String processId, RuntimeManager manager, Context<String> initialContext, RuntimeEngine engine) {
KieSession ksession = engine.getKieSession();
ProcessInstance pInstance = ksession.startProcess(processId);
return pInstance;
}
private KieBase getKieBase(String kbaseId) {
KieServices ks = KieServices.Factory.get();
KieContainer kContainer = ks.getKieClasspathContainer();
KieBase kbase = kContainer.getKieBase(kbaseId);
return kbase;
}
private RuntimeManager createJBPMRuntimeManager(KieBase kbase, String persistenceUnit) {
JBPMHelper.startH2Server();
JBPMHelper.setupDataSource();
EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit);
RuntimeEnvironmentBuilder runtimeEnvironmentBuilder = RuntimeEnvironmentBuilder.Factory.get()
.newDefaultBuilder();
RuntimeEnvironment runtimeEnvironment = runtimeEnvironmentBuilder.entityManagerFactory(emf)
.knowledgeBase(kbase)
.get();
return RuntimeManagerFactory.Factory.get()
.newSingletonRuntimeManager(runtimeEnvironment);
}
}

View File

@ -0,0 +1,3 @@
<kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule">
<kbase name="kbase" packages="com.baeldung.process" />
</kmodule>

View File

@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<definitions id="Definition"
targetNamespace="http://www.jboss.org/drools"
typeLanguage="http://www.java.com/javaTypes"
expressionLanguage="http://www.mvel.org/2.0"
xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.omg.org/spec/BPMN/20100524/MODEL BPMN20.xsd"
xmlns:g="http://www.jboss.org/drools/flow/gpd"
xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI"
xmlns:dc="http://www.omg.org/spec/DD/20100524/DC"
xmlns:di="http://www.omg.org/spec/DD/20100524/DI"
xmlns:tns="http://www.jboss.org/drools">
<process processType="Private" isExecutable="true" id="com.baeldung.bpmn.helloworld" name="HelloWorld Process" tns:packageName="com.baeldung.bpmn.process" >
<!-- nodes -->
<scriptTask id="_jbpm-unique-1" name="HelloWorld" scriptFormat="http://www.java.com/java" >
<script>System.out.println("Hello World");</script>
</scriptTask>
<endEvent id="_jbpm-unique-2" name="End" >
<terminateEventDefinition />
</endEvent>
<startEvent id="_jbpm-unique-0" name="Start" isInterrupting="false"/>
<!-- connections -->
<sequenceFlow id="_jbpm-unique-0-_jbpm-unique-1" sourceRef="_jbpm-unique-0" targetRef="_jbpm-unique-1" />
<sequenceFlow id="_jbpm-unique-1-_jbpm-unique-2" sourceRef="_jbpm-unique-1" targetRef="_jbpm-unique-2" />
</process>
<bpmndi:BPMNDiagram>
<bpmndi:BPMNPlane bpmnElement="com.baeldung.bpmn.helloworld" >
<bpmndi:BPMNShape bpmnElement="_jbpm-unique-1" >
<dc:Bounds x="119" y="44" width="111" height="48" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="_jbpm-unique-2" >
<dc:Bounds x="272" y="43" width="48" height="48" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="_jbpm-unique-0" >
<dc:Bounds x="28" y="44" width="48" height="48" />
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="_jbpm-unique-0-_jbpm-unique-1" >
<di:waypoint x="52" y="68" />
<di:waypoint x="174" y="68" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="_jbpm-unique-1-_jbpm-unique-2" >
<di:waypoint x="174" y="68" />
<di:waypoint x="296" y="67" />
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>

View File

@ -0,0 +1,52 @@
package com.baeldung.jbpm;
import org.jbpm.test.JbpmJUnitBaseTestCase;
import org.junit.After;
import org.junit.Before;
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.runtime.process.ProcessInstance;
import org.kie.internal.runtime.manager.context.ProcessInstanceIdContext;
public class WorkflowEngineIntegrationTest extends JbpmJUnitBaseTestCase {
private String[] triggeredNodesArray = { "Start", "HelloWorld", "End" };
private RuntimeManager manager = null;
private RuntimeEngine runtimeEngine = null;
private KieSession ksession = null;
private ProcessInstance processInstance = null;
@Before
public void setup() {
manager = createRuntimeManager(Strategy.SINGLETON, "manager", "com/baeldung/process/helloworld.bpmn");
runtimeEngine = getRuntimeEngine(ProcessInstanceIdContext.get());
ksession = runtimeEngine.getKieSession();
processInstance = ksession.startProcess("com.baeldung.bpmn.helloworld");
}
@After
public void cleanup() {
manager.disposeRuntimeEngine(runtimeEngine);
}
@Test
public void givenProcessInstance_whenExecutionCompleted_thenVerifyNodesExecutionOrder() {
assertNodeTriggered(processInstance.getId(), triggeredNodesArray);
}
@Test
public void givenProcessInstance_whenExecutionCompleted_thenVerifyKnowledgeSessionId() {
int ksessionID = ksession.getId();
runtimeEngine = getRuntimeEngine(ProcessInstanceIdContext.get(processInstance.getId()));
ksession = runtimeEngine.getKieSession();
assertEquals(ksessionID, ksession.getId());
}
@Test
public void givenProcessInstance_whenExecutionCompleted_thenVerifyProcessInstanceStatus() {
assertProcessInstanceCompleted(processInstance.getId(), ksession);
assertTrue("ProcessInstance completed with status 2", processInstance.getState() == 2);
}
}

View File

@ -480,6 +480,7 @@
<!-- <module>lagom</module> --> <!-- Not a maven project -->
<module>libraries</module>
<module>libraries2</module>
<module>libraries-data</module>
<module>libraries-apache-commons</module>
<module>libraries-security</module>