demo流程目标:刚接触Activiti5,先大致了解下Activiti5的架构,熟悉下开发流程
搭建开发环境
首先给MyEclipse安装Activiti Designer插件,下载网址http://www.activiti.org/designer/archived/
当然也可以在线安装,网址http://www.activiti.org/designer/update
有可能因为版本不兼容安装不上,不过也不要紧,只是小试牛刀,并不影响demo流程的开发,后面会介绍Web版流程设计器
此外,新建一个java project,然后把activiti-explorerWEB-INFlib下面的jar包全部导入
设计示例流程
如果已经按安装好了Activiti Designer插件,直接使用就可以了,如果安装失败,先临时下载一个流程的定义文件,参考网址:http://yiyiboy2010.iteye.com/blog/1530924
编写测试代码
工程的代码结构如下:
测试的代码
import java.util.List;
import org.activiti.engine.HistoryService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.task.Task;
public class Test {
// 本流程模拟的是某公司2012年实习生招聘流程
// 张三来参加该招聘流程,公司相关负责人负责处理流程
public static void main(String[] args) {
// 加载配置文件
ProcessEngine processEngine = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml").buildProcessEngine();
RepositoryService repositoryService = processEngine.getRepositoryService();
repositoryService.createDeployment().addClasspathResource("resources/diagrams/Interview.bpmn20.xml").deploy();
RuntimeService runtimeService = processEngine.getRuntimeService();
String processId = runtimeService.startProcessInstanceByKey("Interview").getId();
TaskService taskService = processEngine.getTaskService();
HistoryService historyService = processEngine.getHistoryService();
// 得到笔试的流程
System.out.println("***************笔试流程开始***************");
List<Task> tasks = taskService.createTaskQuery().taskCandidateGroup("人力资源部").list();
for (Task task : tasks) {
System.out.println("人力资源部的任务:name:" + task.getName() + ",id:" + task.getId());
taskService.claim(task.getId(), "张三");
}
System.out.println("张三的任务数量:" + taskService.createTaskQuery().taskAssignee("张三").count());
tasks = taskService.createTaskQuery().taskAssignee("张三").list();
for (Task task : tasks) {
System.out.println("张三的任务:name:" + task.getName() + ",id:" + task.getId());
taskService.complete(task.getId());
}
System.out.println("张三的任务数量:" + taskService.createTaskQuery().taskAssignee("张三").count());
System.out.println("***************笔试流程结束***************");
System.out.println("***************一面流程开始***************");
tasks = taskService.createTaskQuery().taskCandidateGroup("技术部").list();
for (Task task : tasks) {
System.out.println("技术部的任务:name:" + task.getName() + ",id:" + task.getId());
taskService.claim(task.getId(), "李四");
}
System.out.println("李四的任务数量:" + taskService.createTaskQuery().taskAssignee("李四").count());
for (Task task : tasks) {
System.out.println("李四的任务:name:" + task.getName() + ",id:" + task.getId());
taskService.complete(task.getId());
}
System.out.println("李四的任务数量:" + taskService.createTaskQuery().taskAssignee("李四").count());
System.out.println("***************一面流程结束***************");
System.out.println("***************二面流程开始***************");
tasks = taskService.createTaskQuery().taskCandidateGroup("技术部").list();
for (Task task : tasks) {
System.out.println("技术部的任务:name:" + task.getName() + ",id:" + task.getId());
taskService.claim(task.getId(), "李四");
}
System.out.println("李四的任务数量:" + taskService.createTaskQuery().taskAssignee("李四").count());
for (Task task : tasks) {
System.out.println("李四的任务:name:" + task.getName() + ",id:" + task.getId());
taskService.complete(task.getId());
}
System.out.println("李四的任务数量:" + taskService.createTaskQuery().taskAssignee("李四").count());
System.out.println("***************二面流程结束***************");
System.out.println("***************HR面流程开始***************");
tasks = taskService.createTaskQuery().taskCandidateGroup("人力资源部").list();
for (Task task : tasks) {
System.out.println("技术部的任务:name:" + task.getName() + ",id:" + task.getId());
taskService.claim(task.getId(), "李四");
}
System.out.println("李四的任务数量:" + taskService.createTaskQuery().taskAssignee("李四").count());
for (Task task : tasks) {
System.out.println("李四的任务:name:" + task.getName() + ",id:" + task.getId());
taskService.complete(task.getId());
}
System.out.println("李四的任务数量:" + taskService.createTaskQuery().taskAssignee("李四").count());
System.out.println("***************HR面流程结束***************");
System.out.println("***************录用流程开始***************");
tasks = taskService.createTaskQuery().taskCandidateGroup("人力资源部").list();
for (Task task : tasks) {
System.out.println("技术部的任务:name:" + task.getName() + ",id:" + task.getId());
taskService.claim(task.getId(), "李四");
}
System.out.println("李四的任务数量:" + taskService.createTaskQuery().taskAssignee("李四").count());
for (Task task : tasks) {
System.out.println("李四的任务:name:" + task.getName() + ",id:" + task.getId());
taskService.complete(task.getId());
}
System.out.println("李四的任务数量:" + taskService.createTaskQuery().taskAssignee("李四").count());
System.out.println("***************录用流程结束***************");
HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery().processInstanceId(processId).singleResult();
System.out.println("流程结束时间:" + historicProcessInstance.getEndTime());
System.exit(0);
}
}
参考网址:http://yiyiboy2010.iteye.com/blog/1530924
这里需要特别说明的是,测试的代码编写好之后,如果要运行,必须有一个Activiti的数据库环境,修改activiti.cfg.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="processEngineConfiguration"
class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
<property name="databaseSchemaUpdate" value="true" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/activiti5" />
<property name="jdbcDriver" value="com.mysql.jdbc.Driver" />
<property name="jdbcUsername" value="root" />
<property name="jdbcPassword" value="admin" />
<property name="jobExecutorActivate" value="true" />
</bean>
</beans>
到这里,demo流程就可以跑起来了,不出意外的话,测试代码都可以正常运行
版权声明:本文为博主原创文章,未经博主允许不得转载。