一、安装环境
- Java JDK SE 1.7.4x
- Apache Tomcat7
- Activiti 5.13
- Eclipse Indigo (activiti插件)
- H2 database(建议用1.3.X,太高的在activiti连接的时候会报错)
HB database安装时,直接使用安装包根据安装向导完成。
Eclipse activiti插件安装: 【Help】->【Install New Software...】在弹出的框中点击【Add...】,加入下面的内容:
-
*Name:*Activiti BPMN 2.0 designer
-
*Location:*http://activiti.org/designer/update/如图:
后面再选择全部,往后点【next】直到安装完成。
二、验证环境
1、 activiti-explorer启动
需要jdk是1.7的,所以tomcat也需要7,不然会把NoClassDefFoundException错误,操作:
a、把
activiti-explorer.war拷到webapps目录下,启动tomcat
登录成功,说明服务正常。
2、H2独立服务
windows通过菜单找到H2 Console (Command Line),启动H2 数据库。
登录http://localhost:8082,会出现如下登录界面
修改红色区域内容,如果不能登录,则用jdbc:h2:tcp://localhost/activiti,能够登录说明数据库正常。
三、Activiti Explorer 与H2数据库集成
1、关掉数据库服务和Activiti Explorer服务,首先启动数据库服务,
2、再修改Activiti Explorer的db配置文件,文件路径:
\webapps\activitiexplorer\WEBINF\classes\db.properties
file
把jdbc.url改成jdbc.url=jdbc:h2:tcp://localhost/~/activiti,如果连接数据库的为jdbc:h2:tcp://localhost/activiti,这里就改成一样的。
3、启动Activiti服务器,再登录Activiti,登录成功说明集成成功。到数据库界面可以看到新增加的数据库表。
四、自己创建实例运行
1、在Eclipse中安装插件后,可以新建activiti project,再创建一个*.bpmn20.xml文件,这里直接拷手册中的数据到此文件,如下:
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
< definitions id = "definitions"targetNamespace = "http://activiti.org/bpmn20"xmlns:activiti = "http://activiti.org/bpmn"xmlns = "http://www.omg.org/spec/BPMN/20100524/MODEL" >
< process id = "financialReport" name = "Monthly financial report reminder process" >
< startEvent id = "theStart" />
< sequenceFlow id = 'flow1' sourceRef = 'theStart' targetRef = 'writeReportTask' />
< userTask id = "writeReportTask" name = "Write monthly financial report" >< documentation >Write monthly financial report for publication to shareholders.</ documentation >< potentialOwner >< resourceAssignmentExpression >< formalExpression > accountancy </ formalExpression ></ resourceAssignmentExpression ></ potentialOwner ></ userTask >
< sequenceFlow id = 'flow2' sourceRef = 'writeReportTask' targetRef = 'verifyReportTask' />
< userTask id = "verifyReportTask" name = "Verify monthly financial report" >< documentation >Verify monthly financial report composed by the accountancy department.This financial report is going to be sent to all the company shareholders.</ documentation >< potentialOwner >< resourceAssignmentExpression >< formalExpression > management </ formalExpression ></ resourceAssignmentExpression ></ potentialOwner ></ userTask >
< sequenceFlow id = 'flow3' sourceRef = 'verifyReportTask' targetRef = 'theEnd' />
< endEvent id = "theEnd" />
</ process >
</ definitions >
2、新建一个测试的java文件,文件内容可以如下:
public
static
void
main(String[]
args
) {
// Create Activiti process engine
ProcessEngine
processEngine
= ProcessEngineConfiguration
. createStandaloneProcessEngineConfiguration()
.buildProcessEngine();
// Get Activiti services
RepositoryService
repositoryService
=
processEngine
.getRepositoryService();
RuntimeService
runtimeService
=
processEngine
.getRuntimeService();
// Deploy the process definition
repositoryService
.createDeployment()
.addClasspathResource(
"myProcess.bpmn20.xml"
)
.deploy();
// Start a process instance
String
procId
=
runtimeService
.startProcessInstanceByKey(
"financialReport"
).getId();
// Get the first task
TaskService
taskService
=
processEngine
.getTaskService();
List<Task>
tasks
=
taskService
.createTaskQuery().taskCandidateGroup(
"accountancy"
).list();
for
(Task
task
:
tasks
) {
System.
out
.println(
"Following task is available for accountancy group: "
+
task
.getName());
// claim it
taskService
.claim(
task
.getId(),
"fozzie"
);
}
// Verify Fozzie can now retrieve the task
tasks
=
taskService
.createTaskQuery().taskAssignee(
"fozzie"
).list();
for
(Task
task
:
tasks
) {
System.
out
.println(
"Task for fozzie: "
+
task
.getName());
// Complete the task
taskService
.complete(
task
.getId());
}
System.
out
.println(
"Number of tasks for fozzie: "
+
taskService
.createTaskQuery().taskAssignee(
"fozzie"
).count());
// Retrieve and claim the second task
tasks
=
taskService
.createTaskQuery().taskCandidateGroup(
"management"
).list();
for
(Task
task
:
tasks
) {
System.
out
.println(
"Following task is available for management group: "
+
task
.getName());
taskService
.claim(
task
.getId(),
"kermit"
);
}
// Completing the second task ends the process
for
(Task
task
:
tasks
) {
taskService
.complete(
task
.getId());
}
// verify that the process is actually finished
HistoryService
historyService
=
processEngine
.getHistoryService();
HistoricProcessInstance
historicProcessInstance
=
historyService
.createHistoricProcessInstanceQuery().processInstanceId(
procId
).singleResult();
System.
out
.println(
"Process instance end time: "
+
historicProcessInstance
.getEndTime());
}
测试类后完成发布流程,启动流程和完成流程等一系列操作,这个是我们自己项目需要使用的API。