之前我们学习了Activiti的开发环境的搭建,下面我们开始学习如何在搭建的开发环境中进行Activiti的开发。为了进行一些简单的测试,我们采用Maven工程,然后采用spring-test和junit集成进行测试验证。
第一步:创建Maven工程,加载依赖包,具体的pom.xml文件如下:
4.0.0
com.dfbhaiou.demo
activiti
0.0.1-SNAPSHOT
2.2.3
5.18.0
oschina
oschina repository
http://maven.oschina.net/content/groups/public/
oschina-thirdparty
oschina thirdparty repository
http://maven.oschina.net/content/repositories/thirdparty/
central
Central Repository
http://repo.maven.apache.org/maven2
org.springframework.maven.milestone
Spring Framework Maven Milestone Repository
http://maven.springframework.org/milestone
Activiti
https://maven.alfresco.com/nexus/content/groups/public
org.activiti
activiti-engine
${activiti.version}
org.activiti
activiti-spring
${activiti.version}
mysql
mysql-connector-java
5.1.29
org.springframework
spring-core
3.2.2.RELEASE
org.springframework
spring-beans
3.2.2.RELEASE
org.springframework
spring-test
3.2.2.RELEASE
commons-dbcp
commons-dbcp
1.4
org.springframework
spring-context
3.2.2.RELEASE
org.springframework
spring-context-support
3.2.2.RELEASE
org.springframework
spring-tx
3.2.2.RELEASE
org.springframework
spring-orm
3.2.2.RELEASE
org.mybatis
mybatis
3.2.5
junit
junit
4.11
org.slf4j
slf4j-api
1.7.6
org.activiti
activiti-bpmn-model
${activiti.version}
org.activiti
activiti-bpmn-converter
${activiti.version}
org.activiti
activiti-process-validation
${activiti.version}
joda-time
joda-time
2.1
org.slf4j
jcl-over-slf4j
1.7.6
org.slf4j
slf4j-log4j12
1.7.6
org.apache.commons
commons-lang3
3.1
javax.annotation
javax.annotation-api
1.2
commons-io
commons-io
2.4
c3p0
c3p0
0.9.1
mysql
mysql-connector-java
5.1.6
com.fasterxml.jackson.core
jackson-core
${jackson.version}
com.fasterxml.jackson.core
jackson-annotations
${jackson.version}
com.fasterxml.jackson.core
jackson-databind
${jackson.version}
gtjTelManager
maven-compiler-plugin
1.6
utf-8
maven-assembly-plugin
jar-with-dependencies
src/main/resources
src/main/java
**/*.xml
第二步:配置数据库和activiti(数据库使用activiti-explorer工程的)
数据库的配置文件和activiti的配置文件可以使用activiti-explorer项目下的activiti-standalone-context.xml和db.properties。因为activiti-explorer项目使用的dbcp连接池,而我个人喜好使用c3p0的连接池,所以做了少许的改动,spring的配置文件如下:
classpath:init.properties
${datasource.driverClassName}
${datasource.url}
${datasource.username}
${datasource.password}
${c3p0.acquireIncrement}
${c3p0.initialPoolSize}
${c3p0.minPoolSize}
${c3p0.maxPoolSize}
${c3p0.maxIdleTime}
${c3p0.idleConnectionTestPeriod}
${c3p0.maxStatements}
${c3p0.numHelperThreads}
配置好这些后,我们就可以开始编写测试类了。
测试类如下:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring-activiti.xml"})
@TransactionConfiguration(transactionManager="transactionManager", defaultRollback=false)
@Transactional
public class ActivitiTestCase extends AbstractTransactionalJUnit4SpringContextTests{
@Test
public void test(){
System.out.println ("test");
}
}
运行上面的单元测试代码,如果在控制台输出test,那代表我们前面的配置都OK了。
下面我们就开始使用学习二中安装的插件画流程图并部署。
第一步:新建如下结构的目录
建好后,新建一个 activiti Diagram文件,如上图所示的MyProcess.bpmn,建好后,使用Activiti Diagram Editor打开MyProcess.bpmn文件,然后设计一个简单的流程:员工填写年度报告,提交后由主管进行审批,审批通过后,在由经理审批。流程有开始和结束节点,设计好的流程如下;
填写:由sales来进行操作
主管审批和经理审批:由management来进行操作
设计好流程后,如果没有生成对应的流程图片,打开window=>Preferences=>activiti
勾选Save Actions中的Create Process definition image选项:如下图:
保存后,过一会就会在同目录下生成一个图片:
流程设计好后,下面就是部署了,有2种方式,我们这里采用代码的方式进行部署并测试。代码部署方式如下:
public class BpmDefinitionTest extends ActivitiTestCase{
@Resource
private RepositoryService repositoryService;
@Resource
private RuntimeService runtimeService;
@Resource
private TaskService taskService;
@Resource
private HistoryService historyService;
@Test
public void testDeploy(){
String filePath="/diagrams/simpleProcess/MyProcess.bpmn";
try {
InputStream is = Class.class.getClass().getResource(filePath).openStream();
/*//读取bpmn文件,并发布
Deployment deployment=repositoryService.createDeployment().addInputStream("bpmn20.xml",is).deploy();
ProcessDefinition processDefinition = repositoryService.createProcessDefin.itionQuery().deploymentId(deployment.getId()).singleResult();
Long businessKey=new Double(1000000*Math.random()).longValue();
//启动流程
runtimeService.startProcessInstanceById(processDefinition.getId(),businessKey.toString());
//查询任务实例
List taskList=taskService.createTaskQuery().processDefinitionId(processDefinition.getId()).list();
for(Task task:taskList){
System.out.println("task name is " + task.getName() + " ,task key is " + task.getTaskDefinitionKey());
}*/
// 部署流程定义
repositoryService.createDeployment().addInputStream("bpmn20.xml",is).deploy();
// 启动流程实例
String procId = runtimeService.startProcessInstanceByKey("myProcess").getId();
/******************第一个任务开始******************************/
List taskList = taskService.createTaskQuery().taskCandidateGroup("sales").list();
for (Task task : taskList) {
System.out.println("Following task is available for sales group: " + task.getName());
// 认领任务这里由foozie认领,因为fozzie是sales组的成员
taskService.claim(task.getId(), "gonzo");
}
//查看fozzie是否能够获取该任务
taskList = taskService.createTaskQuery().taskAssignee("gonzo").list();
System.out.println("fozzie当前的任务数:" + taskList.size());
for(Task task : taskList){
System.out.println("the task name is :" + task.getName());
// 执行(完成)任务
taskService.complete(task.getId());
}
System.out.println("gonzo当前的任务数:" + taskService.createTaskQuery().taskAssignee("gonzo").count());
/******************第一个任务结束******************************/
/******************第二个任务开始******************************/
taskList = taskService.createTaskQuery().taskCandidateGroup("management").list();
for(Task task : taskList){
System.out.println("Following task is available for management group: " + task.getName());
taskService.claim(task.getId(), "kermit");
}
//查看kermit是否能够获取改任务
taskList = taskService.createTaskQuery().taskAssignee("kermit").list();
System.out.println("kermit当前任务数:" + taskList.size());
for(Task task : taskList){
System.out.println("the task name is :" + task.getName());
taskService.complete(task.getId());
}
System.out.println("kermit当前任务数:" + taskService.createTaskQuery().taskAssignee("kermit").count());
/******************第二个任务结束******************************/
/******************第三个任务开始******************************/
//获取第三个任务
taskList = taskService.createTaskQuery().taskCandidateGroup("management").list();
for(Task task : taskList){
System.out.println("Following task is available for management group: " + task.getName());
taskService.claim(task.getId(), "gonzo");
}
//查看gonzo是否能够获取改任务
taskList = taskService.createTaskQuery().taskAssignee("gonzo").list();
System.out.println("gonzo当前任务数:" + taskList.size());
for(Task task : taskList){
System.out.println("the task name is :" + task.getName());
taskService.complete(task.getId());
}
System.out.println("gonzo当前任务数:" + taskService.createTaskQuery().taskAssignee("gonzo").count());
/******************第三个任务结束******************************/
HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery()
.processInstanceId(procId).singleResult();
System.out.println("流程结束时间:" + historicProcessInstance.getEndTime());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这里只是简单的使用了activiti的几个api,具体的过程为:部署流程,然后启动流程实例,之后对应节点的用户组用户签收任务并处理直至流程结束。
部署bpmn并测试就到这里了,下一章我们将学习如何部署含有图片的流程实例。