1. Introduction
1.绪论
In this tutorial, we’ll discuss the Business Process Management (BPM) System and its implementation in Java as jBPM System.
在本教程中,我们将讨论业务流程管理(BPM)系统及其在Java中作为jBPM系统的实现。
2. Business Process Management System
2.业务流程管理系统
We can define Business Process Management as one of those fields whose scope extends beyond development to all aspects of a company.
我们可以将业务流程管理定义为其中的一个领域,其范围超出了开发的范围,延伸到公司的所有方面。
BPM provides visibility towards the functional processes of the company. This allows us to find an optimal flow, depicted by a flow chart, by using iterative improvement. The improved flow increases profits and reduces costs.
BPM提供了对公司职能流程的可见性。这使我们能够通过使用迭代改进,找到一个由流程图描述的最佳流程。改进后的流程增加了利润,降低了成本。
BPM defines its own objectives, life cycle, practices, and a common language between all its participants, i.e., business processes.
BPM定义了自己的目标、生命周期、实践,以及所有参与者之间的共同语言,即业务流程。
3. The jBPM System
3.jBPM系统
jBPM is the implementation of a BPM System in Java. It allows us to create a business process flow, execute it, and monitor its life cycle. The core of jBPM is a workflow engine, written in Java, that provides us with a tool to create and execute a process flow using the latest Business Process Modeling Notation (BPMN) 2.0 specifications.
jBPM是用Java实现的一个BPM系统。jBPM的核心是一个用Java编写的工作流引擎,它为我们提供了一个使用最新的业务流程建模符号(BPMN)2.0规范来创建和执行一个流程的工具。
jBPM focuses mainly on the executable business process. These processes have enough details so that they can be executed on the workflow engine.
jBPM主要关注的是可执行的业务流程。这些流程有足够的细节,因此它们可以在工作流引擎上执行。
Here is a graphical flowchart example of the execution order of our BPMN process model to aid in our understanding:
下面是一个关于我们BPMN流程模型执行顺序的图形流程图例子,以帮助我们理解。
- We start executing the flow using the initial context, denoted by the green start node
- First, Task 1 will execute
- On the completion of Task 1, we’ll proceed with Task 2
- The execution stops upon encountering the red end node
4. IDE Plugins for jBPM Project
4.用于jBPM项目的IDE插件
Let’s see how to install plugins to create a jBPM project and a BPMN 2.0 process in Eclipse and IntelliJ IDEA.
让我们看看如何在Eclipse和IntelliJ IDEA中安装插件来创建一个jBPM项目和一个BPMN 2.0流程。
4.1. Eclipse Plugin
4.1 Eclipse Plugin
We’ll need to install a plugin to create jBPM projects. Let’s follow the steps below:
我们需要安装一个插件来创建jBPM项目。让我们按照下面的步骤进行。
- In the Help section, click on Install New Software
- Add the Drools and jBPM update site
- Accept the terms of license agreement and complete the plugin installation
- Restart Eclipse
Once Eclipse restarts, we’ll need to go to Windows -> Preferences -> Drools -> Drools Flow Nodes:
一旦Eclipse重新启动,我们就需要进入Windows -> Preferences -> Drools -> Drools Flow Nodes。
After selecting all the options, we can click on “Apply and Close”. Now, we’re ready to create our first jBPM Project.
选择完所有的选项后,我们可以点击 “应用并关闭”。现在,我们准备创建我们的第一个jBPM项目。
4.2. IntelliJ IDEA Plugin
4.2 IntelliJ IDEA插件
IntelliJ IDEA has jBPM plugin installed by default, but that’s present only in the Ultimate and not the Community option.
IntelliJ IDEA默认安装了jBPM插件,但这只存在于终极版,而不是社区版。
We just need to enable it by clicking Configure -> Settings -> Plugins -> Installed -> JBoss jBPM:
我们只需点击Configure -> Settings -> Plugins -> Installed -> JBoss jBPM就可以启用它。
Currently, there is no BPMN 2.0 process designer for this IDE, though we can import the *.bpmn files from any other designer and run them.
目前,这个IDE没有BPMN 2.0流程设计器,尽管我们可以从任何其他设计器导入*.bpmn文件并运行它们。
5. Hello World Example
5.Hello World实例
Let’s get our hands dirty in creating a simple Hello World project.
让我们动手创建一个简单的Hello World项目。
5.1. Create a jBPM Project
5.1.创建一个jBPM项目
To create a new jBPM project in Eclipse, we’ll go to File -> New -> Other -> jBPM Project (Maven). After providing the name of our project we can hit finish. Eclipse will do all the hard work for us and will download the required Maven dependencies to create a sample jBPM project for us.
要在Eclipse中创建一个新的jBPM项目,我们将进入File -> New -> Other -> jBPM Project (Maven)/em>。在提供了我们的项目名称后,我们可以点击完成。Eclipse将为我们做所有艰苦的工作,并将下载所需的Maven依赖项,为我们创建一个jBPM示例项目。
To create the same in IntelliJ IDEA, we can go to File -> New -> Project -> JBoss Drools. The IDE will download all the required dependencies and place them in the lib folder of the project.
要在IntelliJ IDEA中创建同样的项目,我们可以进入File -> New -> Project -> JBoss Drools。IDE会下载所有需要的依赖项,并把它们放在项目的lib文件夹中。
5.2. Create the Hello World Process Model
5.2.创建 “你好世界 “进程模型
Let’s create a small BPM process model that prints “Hello World” in the console.
让我们创建一个小的BPM流程模型,在控制台打印 “Hello World”。
For this, we need to create a new BPMN file under src/main/resources:
为此,我们需要在src/main/resources下创建一个新的BPMN文件。
The file extension is .bpmn and it opens in the BPMN designer:
该文件的扩展名是.bpmn,它在BPMN设计器中打开。
The left panel of the designer lists the nodes we selected earlier while setting up the Eclipse plugin. We’re going to use these nodes to create our process model. The middle panel is the workspace, where we’ll create the process models. The right side is the properties tab, where we can set the properties of a process or node.
设计器的左边面板列出了我们先前在设置Eclipse插件时选择的节点。我们将使用这些节点来创建我们的流程模型。中间的面板是工作区,我们将在这里创建流程模型。右侧是属性标签,我们可以在这里设置流程或节点的属性。
In this HelloWorld model, we’ll be using the:
在这个HelloWorld模型中,我们将使用:
- Start Event – required to start the process instance
- Script Task – enables Java snippets
- End Event – required to end the process instance
As mentioned earlier, IntelliJ IDEA doesn’t have a BPMN designer, but we can import the .bpmn files designed in Eclipse or a web designer.
如前所述,IntelliJ IDEA没有BPMN设计器,但我们可以导入Eclipse或网页设计器中设计的.bpmn文件。
5.3. Declare and Create the Knowledge Base (kbase)
5.3.声明并创建知识库(kbase)
All the BPMN files are loaded in kbase as processes. We need to pass the respective process ids to the jBPM engine in order to execute them.
所有的BPMN文件都作为进程加载到kbase中。我们需要将各自的进程ids传递给jBPM引擎,以便执行它们。
We’ll create the kmodule.xml under the resources/META-INF with our kbase and BPMN file package declaration:
我们将在resources/META-INF下创建kmodule.xml和我们的kbase和BPMN文件包声明。
<kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule">
<kbase name="kbase" packages="com.baeldung.bpmn.process" />
</kmodule>
Once the declaration is done, we can use the KieContainer to load the kbase:
一旦声明完成,我们可以使用KieContainer来加载kbase。
KieServices kService = KieServices.Factory.get();
KieContainer kContainer = kService.getKieClasspathContainer();
KieBase kbase = kContainer.getKieBase(kbaseId);
5.4. Create the jBPM Runtime Manager
5.4.创建jBPM运行时管理器
We’ll use the JBPMHelper present in the org.jbpm.test package to build a sample runtime environment.
我们将使用JBPMHelper中的org.jbpm.test包来构建一个样本运行环境。
We require two things to create the environment: first, a data source to create the EntityManagerFactory, and second, our kbase.
我们需要两样东西来创建环境:第一,一个数据源来创建EntityManagerFactory,第二,我们的kbase。
JBPMHelper has methods to start an in-memory H2 server and set the data source. Using the same, we can create the EntityManagerFactory:
JBPMHelper具有启动内存中H2服务器和设置数据源的方法。利用这些方法,我们可以创建EntityManagerFactory。
JBPMHelper.startH2Server();
JBPMHelper.setupDataSource();
EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit);
Once we’ve got everything ready, we can create our RuntimeEnvironment:
一旦我们准备好了一切,我们就可以创建我们的RuntimeEnvironment。
RuntimeEnvironmentBuilder runtimeEnvironmentBuilder =
RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder();
RuntimeEnvironment runtimeEnvironment = runtimeEnvironmentBuilder.
entityManagerFactory(emf).knowledgeBase(kbase).get();
Using the RuntimeEnvironment, we can create our jBPM runtime manager:
使用RuntimeEnvironment,我们可以创建我们的jBPM运行时管理器。
RuntimeManager runtimeManager = RuntimeManagerFactory.Factory.get()
.newSingletonRuntimeManager(runtimeEnvironment);
5.5. Execute Process Instance
5.5.执行进程实例
Finally, we’ll use the RuntimeManager to get the RuntimeEngine:
最后,我们将使用RuntimeManager来获取RuntimeEngine。
RuntimeEngine engine = manager.getRuntimeEngine(initialContext);
Using RuntimeEngine, we’ll create a knowledge session and start the process:
使用RuntimeEngine,我们将创建一个知识会话,并开始这个过程。
KieSession ksession = engine.getKieSession();
ksession.startProcess(processId);
The process will start and print Hello World on the IDE console.
该进程将启动并在IDE控制台打印Hello World。
6. Conclusion
6.结语
In this article, we introduced the BPM System, using its Java implementation — jBPM.
在这篇文章中,我们介绍了BPM系统,使用其Java实现 – jBPM。
This was a quick guide to kickstart a jBPM project. The example demonstrated here uses the minimal process in order to give a brief understanding of executing a process and can be found on GitHub.
这是一个启动jBPM项目的快速指南。这里演示的例子使用了最小的流程,以便对执行流程有一个简单的了解,可以在GitHub上找到。
To execute the process, we simply need to run the main method in the WorkflowProcessMain class.
要执行这个过程,我们只需要运行WorkflowProcessMain类中的main方法。