今天演示的JBPM的启动,说明一下JBPM的启动过程。以及简单的和Spring集成。
程序采用的是TestCase方式来演示的,当然Web应用是很重要的,但是其实在Web应用上只要自己写几个类就可以把Spring和JBPM对接起来使用了,所以后面大部分都将使用TestCase,小部分会有Web的例子,希望大家理解。
所有的例子我都会做在一个工程里,所以这个工程的打包文件,我会不断的更新,请大家下载。
需要准备的东西:
JBPM SDK,从JBPM官网上下载。
Eclipse,开发环境,插件(可选)包括:Spring、M2Eclipse(Maven)、WST(WebStandardTools)
jar依赖,如果多ant和maven熟悉的朋友推荐用maven下载,如果不熟悉的,请看附件包里面的dependencies.txt文件列表,然后去下载,谢谢。
数据库,采用HSQLDb来演示,已经包含在工程的lib下了。
范例工程:JBPMSamples.zip
JBPM启动过程简要说明:
- JBPM启动,其实就是JBPM获取需要的配置以及依赖的组件(主要是Hiberante)的配置。
- 环境中一些文件来源和修改的说明:
- src/hibernate.cfg.xml:jbpm-jpdl-3.2.GA\config\hibernate.cfg.xml,由于不使用identity部分所以,注释掉配置里“identity mappings”的部分
- src/log4j.xml:jbpm-jpdl-3.2.GA\config\log4j.cfg.xml,修改了部分配置。
- res/jbpm.jpdl.hsqldb.sql:拷贝自jbpm-jpdl-3.2.GA\db\jbpm.jpdl.hsqldb.sql,给每句后面加上逗号,然后使用build.xml中任务initDb.hsqldb对数据库执行初始化。
- 样例代码:com.cnblogs.seewood.s01.ConfigurationTests:基于JUnit的测试范例,载入配置文件。
- 样例代码:com.cnblogs.seewood.s02.ConfigurationSpringTests:结合Spring的调用。
下面贴一些代码的样例:
com.cnblogs.seewood.s01.ConfigurationTests
1
package com.cnblogs.seewood.s01;
2![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
import org.apache.commons.logging.Log;
4
import org.apache.commons.logging.LogFactory;
5
import org.hibernate.Session;
6
import org.jbpm.JbpmConfiguration;
7
import org.jbpm.JbpmContext;
8![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
import junit.framework.TestCase;
10![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11
public class ConfigurationTests extends TestCase {
12
private final Log logger = LogFactory.getLog(this.getClass());
13
14
public void testSimpleProcessInMemory() {
15
JbpmConfiguration cfg;
16
/*
17
// 载入配置的方法有很多,这里仅举几个常见例子
18
// JbpmConfiguration构造时,会先载入
19
// classpath:org/jbpm/default.jbpm.cfg.xml中的配置,
20
// 然后根据用户指定位置载入配置信息
21
// 用户自定义配置信息将会覆盖default.jbpm.cfg.xml的内容。
22
// 默认配置读取classpath:hiberante.cfg.xml
23
24
// 默认名称的载入配置,即classpath:jbpm.cfg.xml,
25
cfg = JbpmConfiguration.getInstance();
26
27
// 先载入classpath:org/jbpm/default.jbpm.cfg.xml中的配置
28
// 再从指定名称的载入配置
29
cfg = JbpmConfiguration.getInstance("jbpm-cfg.xml");
30
31
// 从XML字符串构造配置
32
cfg = JbpmConfiguration.parseXmlString(
33
"<jbpm-configuration>" +
34
" <string name=\"resource.hibernate.cfg.xml\" " +
35
" value=\"cn/horncomb/bpm/jbpm/hibernate.cfg.xml\" />" +
36
"</jbpm-configuration>");
37
*/
38
// Spring测试类,默认采用Auto-wire,即从ApplicationContext中获取名称匹配的bean,
39
// 自动注入。jbpmConfiguration就会自动注入的。
40
// applicationContext-jbpm.xml 中定义了bean: jbpmConfiguration.
41
42
cfg = JbpmConfiguration.getInstance("com/cnblogs/seewood/s01/jbpm.cfg.xml");
43
assertNotNull(cfg);
44
this.logger.info("成功创建Jbpm配置信息");
45
46
JbpmContext ctx = cfg.createJbpmContext();
47
assertNotNull(ctx);
48
this.logger.info("成功创建JbpmContext");
49
50
// 注意日志的输入,当第一次获取session的时候才初始化hibernate的配置。
51
Session s = ctx.getSession();
52
assertNotNull(s);
53
this.logger.info("成功创建Hibernate Session");
54
55
// 以后获取时,hibernate不会需要再初始化,所以这种情况用spring就更好了
56
ctx.getSession();
57
this.logger.info("再次获取Session");
58
}
59
}
60![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
42
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
43
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
48
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
50
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
52
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
54
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
55
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
56
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
57
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
58
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
59
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
60
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
1
package com.cnblogs.seewood.s02;
2![](/Images/OutliningIndicators/None.gif)
3
import org.hibernate.Session;
4
import org.hibernate.SessionFactory;
5
import org.jbpm.JbpmConfiguration;
6
import org.jbpm.JbpmContext;
7
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
8![](/Images/OutliningIndicators/None.gif)
9
/**
10
* 简单流程测试
11
* @author seewood
12
* @version $LastChangedRevision$
13
*/
14
public class ConfigurationSpringTests
15
extends AbstractTransactionalDataSourceSpringContextTests {
16
private JbpmConfiguration jbpmConfiguration;
17
private SessionFactory sessionFactory;
18![](/Images/OutliningIndicators/InBlock.gif)
19
@Override
20
protected String getConfigPath() {
21
// 返回配置文件路径(相对与classpath)
22
return "/applicationContext.xml";
23
}
24![](/Images/OutliningIndicators/InBlock.gif)
25
@Override
26
protected boolean isRollback() {
27
// 默认为true,即是测试完毕后事务全部rollback
28
return super.isRollback();
29
}
30![](/Images/OutliningIndicators/InBlock.gif)
31
public void testInitJbpmConfiguration() {
32
JbpmConfiguration cfg;
33
// Spring测试类,默认采用Auto-wire,即从ApplicationContext中获取名称匹配的bean,
34
// 自动注入。jbpmConfiguration就会自动注入的。
35
// applicationContext-jbpm.xml 中定义了bean: jbpmConfiguration.
36
// 这是自定义的一个JbpmConfiguration的Factory Bean。
37
cfg = this.getJbpmConfiguration();
38
assertNotNull(cfg);
39
this.logger.info("成功创建Jbpm配置信息");
40![](/Images/OutliningIndicators/InBlock.gif)
41
JbpmContext ctx = cfg.createJbpmContext();
42
assertNotNull(ctx);
43
this.logger.info("成功创建JbpmContext");
44![](/Images/OutliningIndicators/InBlock.gif)
45
// 注意与不使用Spring时候的区别
46
// Spring已经初始化Hibernate配置了,所以这个时候不能使用ctx直接获取session
47
// 而必须使用Spring中配置的工厂来获取session。
48
// 后面会演示在web时,在哪里处理这个session问题比较好。
49![](/Images/OutliningIndicators/InBlock.gif)
50
// 使用Spring的session工厂
51
ctx.setSessionFactory(this.sessionFactory);
52
Session s = ctx.getSession();
53
assertNotNull(s);
54
this.logger.info("成功创建Hibernate Session");
55
}
56![](/Images/OutliningIndicators/InBlock.gif)
57
public void testInitProcessDefinitionXml() {
58![](/Images/OutliningIndicators/InBlock.gif)
59
}
60![](/Images/OutliningIndicators/InBlock.gif)
61
public JbpmConfiguration getJbpmConfiguration() {
62
return jbpmConfiguration;
63
}
64![](/Images/OutliningIndicators/InBlock.gif)
65
public void setJbpmConfiguration(JbpmConfiguration jbpmConfiguration) {
66
this.jbpmConfiguration = jbpmConfiguration;
67
}
68![](/Images/OutliningIndicators/InBlock.gif)
69
public SessionFactory getSessionFactory() {
70
return sessionFactory;
71
}
72![](/Images/OutliningIndicators/InBlock.gif)
73
public void setSessionFactory(SessionFactory sessionFactory) {
74
this.sessionFactory = sessionFactory;
75
}
76
}
77![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/None.gif)
7
![](/Images/OutliningIndicators/None.gif)
8
![](/Images/OutliningIndicators/None.gif)
9
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
14
![](/Images/OutliningIndicators/None.gif)
15
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
26
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
27
![](/Images/OutliningIndicators/InBlock.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
30
![](/Images/OutliningIndicators/InBlock.gif)
31
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/InBlock.gif)
34
![](/Images/OutliningIndicators/InBlock.gif)
35
![](/Images/OutliningIndicators/InBlock.gif)
36
![](/Images/OutliningIndicators/InBlock.gif)
37
![](/Images/OutliningIndicators/InBlock.gif)
38
![](/Images/OutliningIndicators/InBlock.gif)
39
![](/Images/OutliningIndicators/InBlock.gif)
40
![](/Images/OutliningIndicators/InBlock.gif)
41
![](/Images/OutliningIndicators/InBlock.gif)
42
![](/Images/OutliningIndicators/InBlock.gif)
43
![](/Images/OutliningIndicators/InBlock.gif)
44
![](/Images/OutliningIndicators/InBlock.gif)
45
![](/Images/OutliningIndicators/InBlock.gif)
46
![](/Images/OutliningIndicators/InBlock.gif)
47
![](/Images/OutliningIndicators/InBlock.gif)
48
![](/Images/OutliningIndicators/InBlock.gif)
49
![](/Images/OutliningIndicators/InBlock.gif)
50
![](/Images/OutliningIndicators/InBlock.gif)
51
![](/Images/OutliningIndicators/InBlock.gif)
52
![](/Images/OutliningIndicators/InBlock.gif)
53
![](/Images/OutliningIndicators/InBlock.gif)
54
![](/Images/OutliningIndicators/InBlock.gif)
55
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
56
![](/Images/OutliningIndicators/InBlock.gif)
57
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
58
![](/Images/OutliningIndicators/InBlock.gif)
59
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
60
![](/Images/OutliningIndicators/InBlock.gif)
61
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
62
![](/Images/OutliningIndicators/InBlock.gif)
63
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
64
![](/Images/OutliningIndicators/InBlock.gif)
65
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
66
![](/Images/OutliningIndicators/InBlock.gif)
67
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
68
![](/Images/OutliningIndicators/InBlock.gif)
69
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
70
![](/Images/OutliningIndicators/InBlock.gif)
71
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
72
![](/Images/OutliningIndicators/InBlock.gif)
73
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
74
![](/Images/OutliningIndicators/InBlock.gif)
75
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
76
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
77
![](/Images/OutliningIndicators/None.gif)