0
回答
JBPM 5 简单步骤
注册华为云得mate10,2.9折抢先购!>>>   

自从JBPM 4发布以后,很多人谈到jBPM和它的命运。两个jBPM项目的大牛Tom Baeyens和Joram Barrez离开了jBPM项目,并开始了一个命名为“Activiti”的项目,该项目由Alfresco公司支持。当前jBPM4.4)的稳定版本是没有红帽在JBoss SOA平台商业支持尽管如此,JBPM5是BPM解决方案的一个非常好的选择。它也集成了Drools所以人们也可以轻松整合BPM规则系统此外,JBPM5支持BPMN2.0。

这篇文章,介绍了一些简单的步骤来开启JBPM 5.0,非常简单的低层次的例子来理解JBPM API。该示例包括所示代码的流程定义执行

KnowledgeBase: 

The jBPM API allows you to first create a knowledge base. The Knowledge Base is a repository of all the application’s knowledge definitions. It may contain rules, processes, functions etc. The Knowledge Base itself does not contain instance data, known as facts; instead, sessions are created from the Knowledge Base into which data can be inserted and where process instances may be started. The following code snippet shows how to create a knowledge base consisting of only one process definition.

KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("TestProcess.bpmn"), ResourceType.BPMN2);
KnowledgeBase kbase = kbuilder.newKnowledgeBase();

Knowledge Session:

Next step is to start a session to interact with created knowledge base. The following code snippet shows how easy it is to create a session based on the earlier created knowledge base, and to start a process.

StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
ProcessInstance processInstance = ksession.startProcess("first_test");

Events: 

JBPM API allows you to listens to some events during process execution to interact with JBPM runtime. A ProcessEventListener can be used to listen to process-related events, like starting or completing a process, entering and leaving a node, etc. You can set your listener class using following line of code.

ksession.addEventListener(new TestEventListener());

Input Parameters:

You can also provide some input parameters to help you executing your business logic meanwhile the process is executing. Following code sample shows how to provide input parameters to a process execution.

Map<String, Object> inputParams = new HashMap<String, Object>();
inputParams.put("name", "i am an input parameter.");
ProcessInstance processInstance = ksession.startProcess("first_test", inputParams);

You can access this parameter during process execution in your event listener implementation.

public class TestEventListener implements ProcessEventListener {

	@Override
	public void beforeNodeTriggered(ProcessNodeTriggeredEvent event) {
		System.out.println("Before Node triggered event received for node: "+ event.getNodeInstance().getNodeName());
		Object obj  = event.getNodeInstance().getVariable("name");
		System.out.println("Input Parameter of ProcessInstance: "+obj.toString());
	}
}

Now, let’s put up all together in a test class. Followings are libraries I have includes in my eclipse workspace. You can download them from here.

  • antlr-runtime.jar
  • drools-compiler.jar
  • drools-core.jar
  • jbpm-bam.jar
  • jbpm-bpmn2.jar
  • jbpm-flow-builder.jar
  • jbpm-flow.jar
  • knowledge-api.jar
  • mvel2.jar
  • org.eclipse.jdt.core_3.3.0.v_771.jar
  • xstream.jar

BPMN 2.0 Process Definition

Following is a sample process definition that I have used in my test class. It contains three nodes: start, script task and end node. ScriptTask simply prints something when it executes. The id attribute in Process tag is used in starting workflow from session.

<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="first_test" name="Hello">

		<!-- nodes -->
		<startEvent id="_1" name="Start" >
		</startEvent>

		<scriptTask id="_2" name="Hello" >
			<script>System.out.println("This is my first test process.");</script>
		</scriptTask>

		<endEvent id="_3" name="End" >
			<terminateEventDefinition/>
		</endEvent>

		<!-- connections -->
		<sequenceFlow id="_1-_2" sourceRef="_1" targetRef="_2" />
		<sequenceFlow id="_2-_3" sourceRef="_2" targetRef="_3" />
	</process>
</definitions>

TestEventListener.java

import org.drools.event.process.ProcessCompletedEvent;
import org.drools.event.process.ProcessEventListener;
import org.drools.event.process.ProcessNodeLeftEvent;
import org.drools.event.process.ProcessNodeTriggeredEvent;
import org.drools.event.process.ProcessStartedEvent;
import org.drools.event.process.ProcessVariableChangedEvent;

public class TestEventListener implements ProcessEventListener {

	@Override
	public void beforeNodeTriggered(ProcessNodeTriggeredEvent event) {
		System.out.println("Before Node triggered.   "+event.getNodeInstance().getNodeName());
		Object obj  = event.getNodeInstance().getVariable("name");
		System.out.println("Input Parameter of ProcessInstance: "+obj.toString());
	}
	@Override
	public void afterNodeLeft(ProcessNodeLeftEvent arg0) {}

	@Override
	public void afterNodeTriggered(ProcessNodeTriggeredEvent arg0) {}

	@Override
	public void afterProcessCompleted(ProcessCompletedEvent arg0) {}

	@Override
	public void afterProcessStarted(ProcessStartedEvent arg0) {}

	@Override
	public void afterVariableChanged(ProcessVariableChangedEvent arg0) {}

	@Override
	public void beforeNodeLeft(ProcessNodeLeftEvent arg0) {}

	@Override
	public void beforeProcessCompleted(ProcessCompletedEvent arg0) {}

	@Override
	public void beforeProcessStarted(ProcessStartedEvent arg0) {}

	@Override
	public void beforeVariableChanged(ProcessVariableChangedEvent arg0) {}
}

FirstTest.java

import java.util.HashMap;
import java.util.Map;

import org.drools.KnowledgeBase;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.runtime.StatefulKnowledgeSession;
import org.drools.runtime.process.ProcessInstance;

public class FirstTest {

	public static void main(String[] args) {

		KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
		kbuilder.add(ResourceFactory.newClassPathResource("conf/TestProcess.bpmn"), ResourceType.BPMN2);

		KnowledgeBase kbase = kbuilder.newKnowledgeBase();
		StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
		ksession.addEventListener(new TestEventListener());

		Map<String, Object> inputParams = new HashMap<String, Object>();
		inputParams.put("name", "i am an input parameter.");
		ProcessInstance processInstance = ksession.startProcess("first_test", inputParams);
	}
}
举报
小卒过河
发帖于7年前 0回/3K+阅
顶部