對HelloWorldDbTest的分析
在Eclipse中到如jbpm-starters-kit-3.1"jbpm工程后,在src"examples"org"jbpm"db底下有一個HelloWorldDbTest.java的實例。我從這個實例分析開始,構建自己的jbpm工程在Mysql下的應用。
首先我們來先分析一下HelloWorldDbTest實例。
基本流程
HelloWorldDbTest類是一個JUnit的測試類。里面只有一個測試函數testSimplePersistence。這個函數里面調用了三個操作,分別是:
l 將流程(processDefinition)的部署到數據庫中;
l 從數據庫中加載流程,并實例化一個流程實例(processInstance),然后運行一半再將流程實例存回數據庫;
l 從數據庫中加載流程實例,然后運行完畢這個實例。
JbpmConfigration
在類HelloWorldDbTest中有靜態代碼段,內容是構造一個JbpmConfigration的實例。JbpmConfigration在Jbpm3.1中和Jbpm3中差別很大,JbpmAPI手冊描寫如下:
“configuration of one jBPM instance. During process execution, jBPM might need to use some services. A JbpmConfiguration contains the knowledge on how to create those services.
A JbpmConfiguration is a thread safe object and serves as a factory for JbpmContexts, which means one JbpmConfiguration can be used to create JbpmContexts for all threads. The single JbpmConfiguration can be maintained in a static member or in the JNDI tree if that is available. ”
JbpmConfigration主要有兩種加載方法,一種是從配置文件中加載,一般使用jbpm.cfg.xml,jbpm-starters-kit-3.1中放在jbpm-starters-kit-3.1"jbpm"src"config.files目錄下。要使用jbpm.cfg.xml進行配置的話,就必須把這個文件放在classpath中。因為jbpm-starters-kit-3.1"jbpm工程已經把此目錄放在工程的classpath中,所以如果在代碼中JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();這樣書寫的話就是使用jbpm.cfg.xml中的配置來構造JbpmConfigration。另外一種就是本例中使用的對xml解析的方法。
使用數據庫
在加載的JbpmConfiguration的加載內容中有如下部分:
“" <string name='resource.hibernate.cfg.xml' " +
" value='hibernate.cfg.xml' />" +”
這里面的hibernate.cfg.xml也在jbpm-starters-kit-3.1"jbpm"src"config.files目錄下,是對Jbpm使用的Hibernate的配置文檔。通過這個文檔,Jbpm就可以通過Hibernate的工具,在不同的數據庫中,把說使用到的表全部構建出來,而不需要再人工去操作sql語句在數據庫中操作,這也就是為什么這個實例中沒有需要我們自己在數據庫中建表建庫的原因。
通過HelloWorldDbTest類的注釋和用戶手冊中的文檔可以知道,在這個實例中使用的數據庫是一個內存數據庫,在目錄下的Hibernate.cfg.xml中有如下內容:
“ <!-- jdbc connection properties -->
<property name="hibernate.dialect">org.hibernate.dialect.HSQLDialect</property>
<property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="hibernate.connection.url">jdbc:hsqldb:mem:.;sql.enforce_strict_size=true</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password"></property>”
懂得Hibernate的朋友就能看出來,在jbpm工程中,使用的數據庫是hsqldb。
在加載HelloWorldDbTest類的時候,就會發現,在console有大量的輸出。其中包括:
“12:27:38,201 [main] INFO Configuration : Reading mappings from resource: org/jbpm/graph/def/Transition.hbm.xml
12:27:38,231 [main] INFO HbmBinder : Mapping class: org.jbpm.graph.def.Transition -> JBPM_TRANSITION“很多類似的輸出。這些輸出就是Hiernate在加載描述文件時候產生的說明信息。
輸出信息中后面還包括對數據庫的連接,設置等等說明信息。
JbpmConfiguration.createSchema()
這個方法可以實現自動在數據庫中生成表結構的。在這個實例中,在Junit的 setup()操作中調用了這個函數,在進行測試實例前對數據庫進行創建。
JbpmContext
運行HelloWorldDbTest中的測試實例。
首先是定義一個流程,然后就實例化一個JbpmContext。
“JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext(); “
JbpmContext類是Jbpm3.1版本引進的一個新類,以前是沒有的。
在jbpm的說明中有對JbpmContext類的如下說明:
JbpmContext is used to surround persistent operations to processes.
Obtain JbpmContext's via JbpmConfiguration.createJbpmContext() and put it in a try-finally block like this:
JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
try {
TaskInstance taskInstance = ...
...do your process operations...
// in case you update a process object that was not fetched
// with a ...ForUpdate method, you have to save it.
jbpmContext.save(processInstance);
finally {
jbpmContext.close();
}
A JbpmContext separates jBPM from a sprecific environment. For each service that jBPM uses, there is an interface specified in the jBPM codebase. jBPM also includes implementations that implement these services by using services in a specific environment. e.g. a hibernate session, a JMS asynchronous messaging system, ...
A JbpmContext can demarcate a transaction. When a PersistenceService is fetched from the JbpmContext, the default implementation for the persistence service will create a hibernate session and start a transaction. So that transactions can be configured in the hibernate configuration.
A JbpmContext allows the user to overwrite (or make complete) the configuration by injecting objects programmatically. like e.g. a hibernate session factory or a hibernate session or any other resource that can be fetched or created from the configuration.
Last but not least, JbpmContext provides convenient access to the most common operations such as getTaskList(String), newProcessInstance(String) loadTaskInstanceForUpdate(long) and save(ProcessInstance).
All the ...ForUpdate(...) methods will automatically save the loaded objects at jbpmContext.close();
將上面這些英文讀懂,基本上就能對JbpmContext有個大致的了解。最主要的就是實現Jbpm對數據庫操作的所有接口的封裝。
流程部署
在deployProcessDefinition中的jbpmContext. deployProcessDefinition (processDefinition);操作,向數據庫中部署一個流程。如果跟蹤deployProcessDefinition的話,會發現jbpmContext還是調用的GraphSession的流程部署方法。后面我們再講到GraphSession類。
在后面使用Mysql實現的例子中,我們就可以通過工具在mysql中看到這樣一個部署流程的操作對數據庫有如何影響。同樣也可以查看流程實例化保存在數據庫中的情況。
GraphSession
Jbpm中are the graph related database operations的類。可以通過GraphSession graphSession = jbpmContext.getGraphSession();來得到這個類的實例,并且JbpmContext很多方法都是封裝該類的方法。
該類中包括了幾乎所有的數據庫操作的,例如部署流程,加載流程,部署流程實例,加載變量,等等。
HelloWorldDbTest類本身就已經有非常完整的注釋,有關分析也就寫到這里。
Jbpm在Mysql上的demo工程
創建Jbpm Project
首先在Eclipse里面創建一個Jbpm Process Project的新項目。比如命名為myDemo。Eclipse的Jbpm插件會自動創建一些文件,比如在processes目錄下有一個simple的流程。在src/java下面有一個MessageActionHandler的類。在src/config.files目錄下四個配置有文件。在test/java下面有一個SimpleProcessTest的JUnit類實例。可以先運行這個測試,應該是正確沒有問題的。
配置Mysql數據庫
打開MySql數據庫,假設MySql數據庫的用戶名為root,密碼1234。首先創建jbpm_db數據庫。然后修改src/config.files底下的Hibernate.cfg.xml文件。將連接數據庫的部分換成Mysql的內容。如下:
<!-- jdbc connection properties -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/jbpm_db</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">1234</property>
添加代碼和流程
我們先創建一個自己的流程。在processes目錄下面創建一個叫做pro1的流程。然后添加最簡單的,start-state ,end和一個state節點。
將Jbpm3工程中的jbpm.cfg.xml中的內容添加到src/config.files目錄下的jbpm.cfg.xml中。
在test/java目錄下面添加一個新的Junit類。命名為SimpleDBTest。
代碼如下:
package com.sample;
import java.io.FileInputStream;
import java.util.List;
import junit.framework.TestCase;
import org.jbpm.JbpmConfiguration;
import org.jbpm.JbpmContext;
import org.jbpm.db.GraphSession;
import org.jbpm.graph.def.ProcessDefinition;
import org.jbpm.graph.exe.ProcessInstance;
import org.jbpm.graph.exe.Token;
public class SimpleDBTest extends TestCase {
static JbpmConfiguration cfg = JbpmConfiguration.getInstance();
public void setUp() {
cfg.createSchema();
}
public void testDeployProcessDefinition() throws Exception {
assertNotNull("JbpmConfiguration should not be null", cfg);
FileInputStream fis = new FileInputStream("processes/pro1/processdefinition.xml");
ProcessDefinition processDefinition = ProcessDefinition.parseXmlInputStream(fis);
assertNotNull("Definition should not be null", processDefinition);
JbpmContext jbpmContext = cfg.createJbpmContext() ;
try {
// Deploy the process definition in the database
jbpmContext.deployProcessDefinition(processDefinition);
} finally {
jbpmContext.close();
}
}
}
執行這個操作,會發現報錯,報不能找到Hibernate的錯誤。原來在Eclipse建立的Jbpm的工程中,雖然添加了Jbpm的包,但沒有添加Hibernate的包。因此在工程的Java Build Path的Libraries底下添加Hibernate的包(具體添加那些,請參考Jbpm的用戶手冊)。
部署流程
執行SimpleDBTest .testDeployProcessDefinition()操作。如果沒有錯誤的話,這次就可以將流程pro1部署進入Mysql數據庫。
在jbpm_db數據庫中會看到產生了33個表。查看jbpm_processdefinition表,會發現有一個名字為pro1,ID為1,version為1的條目。
同樣可以查看jbpm_node表。
再次部署
修改流程pro1的定義,在state1節點添加一個Action,在node-enter事件中。完整的xml文件見該文檔最后部分。然后再次運行SimpleDBTest .testDeployProcessDefinition()操作。查看jbpm_processdefinition,會發現有了一個pro1的version為2版本的流程。這里有了一個Jbpm流程版本的概念,可以參考用戶手冊。
還可查看jbpm_event,jbpm_action表。
實例化流程
添加代碼:
public void testLoadProcessAndInstance() throws Exception {
JbpmContext jbpmContext = cfg.createJbpmContext() ;
try {
GraphSession graphSession = jbpmContext.getGraphSession();
ProcessDefinition processDefinition =
graphSession.findLatestProcessDefinition("pro1");
ProcessInstance processInstance =
new ProcessInstance(processDefinition);
Token token = processInstance.getRootToken();
assertEquals("start", token.getNode().getName());
// Let's start the process execution
token.signal();
assertEquals("state1", token.getNode().getName());
jbpmContext.save(processInstance);
} finally {
// Tear down the pojo persistence context.
jbpmContext.close();
}
}
這段代碼把pro1流程的最新版本加載進來,然后實例化。并開始執行,到state1節點停下來(此時Action已經執行過了)。然后把這個實例也存入數據庫。
這時候查看jbpm_processInstance表,jbpm_token表。
完成實例的運行
添加代碼
public void testLoadInstanceAndDoActionAndEnd() throws Exception {
JbpmContext jbpmContext = cfg.createJbpmContext() ;
try {
GraphSession graphSession = jbpmContext.getGraphSession();
ProcessDefinition processDefinition =
graphSession.findLatestProcessDefinition("pro1");
List processInstances =
graphSession.findProcessInstances(processDefinition.getId());
ProcessInstance processInstance =
(ProcessInstance) processInstances.get(0);
this.assertEquals("message",(String)(processInstance.getContextInstance().getVariable("message")));
processInstance.signal();
assertTrue(processInstance.hasEnded());
jbpmContext.save(processInstance);
} finally {
jbpmContext.close();
}
}
這段代碼將剛才的流程實例從數據庫中加載進來,然后執行完畢。
查看表jbpm_processinstance表,會發現上次end字段還是null,現在已經是填寫了剛才執行的事件了,表示這個流程實例已經執行完畢。
流程Pro1的完整xml文檔
<?xml version="1.0" encoding="UTF-8"?>
<process-definition
xmlns="urn:jbpm.org:jpdl-3.1" name="pro1">
<start-state name="start">
<transition name="" to="state1"></transition>
</start-state>
<state name="state1">
<event type="node-enter">
<action name="action1" class="com.sample.action.MessageActionHandler">
<message>message</message>
<message2>message2 </message2>
</action>
</event>
<transition name="" to="end"></transition>
</state>
<end-state name="end"></end-state>
</process-definition>
總結
通過上面的分析和實例,我們基本上可以理解Jbpm對數據庫的操作。但這還只是一個最開始的實習。如果要理解jbpm的ER模型的話,最好能夠把數據庫中的表的ER模型做出來。在網上以前看到過相關的圖。但是沒有說明。如果有朋友有相關的文檔的話,麻煩給留個信息,謝謝。
v