activiti整合开发实例总结
参考手册:http://www.mossle.com/docs/activiti/
一、applicationContext.xml中引入activiti相关配置的xml文件
<!-- begin:工作流相关配置开始 -->
<import resource="classpath:com/future/sem/resource/activiti/config/activiti-core-context.xml" />
<!-- end:工作流相关配置结束 -->
二、activiti-core-context.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"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" default-lazy-init="true"> <!-- 配置Process Engine Definition -->
<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
<!--spring的xml中已经配置 -->
<property name="dataSource" ref="dataSource" />
<property name="transactionManager" ref="txManager" /> <!--工作流引擎启动和关闭时对数据库表结构的处理策略 false(default),true,create,create-drop -->
<property name="databaseSchemaUpdate" value="true" />
<!--激活Timer和异步消息的线程的组件 true(default) ,false -->
<property name="jobExecutorActivate" value="false" />
<property name="enableDatabaseEventLogging" value="true" />
<property name="history" value="full" />
<property name="databaseType" value="oracle" />
<property name="activityFontName" value="微软雅黑" />
<property name="labelFontName" value="微软雅黑" /> <!-- 自动部署,每次重启都要重新加载 -->
<property name="deploymentResources" value="classpath*:/com/future/sem/resource/activiti/diagrams/*.bpmn" />
</bean> <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean" destroy-method="destroy">
<property name="processEngineConfiguration" ref="processEngineConfiguration" />
</bean> <!-- 创建activiti提供的各种服务 -->
<bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />
<bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" />
<bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />
<bean id="formService" factory-bean="processEngine" factory-method="getFormService" />
<bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />
<bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" />
<bean id="identityService" factory-bean="processEngine" factory-method="getIdentityService" /> </beans>
三、*.bpmn流程相关的文件
使用elipse,添加流程相关插件
插件地址:http://activiti.org/designer/update/
activiti需要第三方依赖库:graphiti
地址:http://download.eclipse.org/graphiti/updates/0.9.0
实例图:
安装图解说明:
四、主要的服务service代码
RepositoryService : 提供方法获取各种流程和部署文件的信息 .
TaskService : 提供对任务相关的各种操作
identityService : 管理用户和用户组。
FormService : 获取或者绑定数据到流程实例上
RuntimeService : 提供操作部署文件,流程文件和流程实例的方法 .
ManagementService : 提供管理员对流程引擎的监控,和流程引擎服务应用无关。
HistoryService : 提供正在执行和过去执行的各种流程实例信息
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); RuntimeService runtimeService = processEngine.getRuntimeService();
RepositoryService repositoryService = processEngine.getRepositoryService();
TaskService taskService = processEngine.getTaskService();
ManagementService managementService = processEngine.getManagementService();
IdentityService identityService = processEngine.getIdentityService();
HistoryService historyService = processEngine.getHistoryService();
FormService formService = processEngine.getFormService();
项目中具体服务类实例:
package com.future.sem.common.activiti.service; import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle; import javax.annotation.Resource; import org.activiti.engine.FormService;
import org.activiti.engine.HistoryService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.ManagementService;
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.identity.Group;
import org.activiti.engine.identity.User;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import com.future.sem.common.util.ResourceUtil; @Service("activitiCoreService")
@Transactional
public class ActivitiCoreService {
/**
* 打印日志
*/
private static Logger logger = LoggerFactory
.getLogger(ActivitiCoreService.class); @Resource
private RepositoryService repositoryService; @Resource
private RuntimeService runtimeService; @Resource
private TaskService taskService; @Resource
private FormService formService; @Resource
private HistoryService historyService; @Resource
private ManagementService managementService; @Resource
private IdentityService identityService; // public static final String processDefinitionId = "myProcess"; //流程id /**
* 获取资源文件中配置的流程id
*
* @return 流程id
* @说明 流程的命名规则
*/
public String getProcessDefID(String groupId) {
ResourceBundle rb = ResourceUtil.getBundle("sem_system_config");// 读取src根目录下的sem_system_config.properties文件,调用不用的流程
String key = "com.future.sem.activiti.process." + groupId
+ "ProcessDefinitionId";
return rb.getString(key);
} /**
* 方法:部署流程
*
* @title: deploymentActiviti
* @param
* @return void
*/
public void deploymentActiviti(String path) {
// repositoryService.createDeployment().addClasspathResource(path).deploy();
// //简单的部署流程
// repositoryService.deleteDeployment("depolyid"); //删除流程
// repositoryService.createDeploymentQuery().list();//查询流程
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmSS");
Deployment dep = repositoryService.createDeployment().category(
"人员审核流程").name(sdf.format(new Date()) + "测试流程")
.addClasspathResource(path).deploy();
logger.debug(
"\n\t文件:{}部署成功!\n\t名称:{}\n\tid:{}\n\t部署时间:{}\n\t类别:{}",
new Object[] {
path,
dep.getName(),
dep.getId(),
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")
.format(dep.getDeploymentTime()),
dep.getCategory() });
logger.debug("\n\t>>>>目前已部署流程{}个!",
new Object[] { repositoryService.createDeploymentQuery()
.count() });
} catch (Exception e) {
logger.error("流程部署失败");
}
} /**
* 方法:启动流程
*
* @title: startProcessInstanceByKey
* @param
* @return ProcessInstance
*/
public ProcessInstance startProcessInstanceByKey(String businessKey,
String userId, Map<String, Object> varsMap,
String processDefinitionId) {
ProcessInstance pi = null;
try {
identityService.setAuthenticatedUserId(userId);// 设置启动流程的人员ID
pi = runtimeService.startProcessInstanceByKey(processDefinitionId,
businessKey, varsMap);
logger.info("\n\tNumber of process instances: "
+ runtimeService.createProcessInstanceQuery().count()
+ "\n\tprocessId:" + pi.getId() + "\n\tbusinessKey:"
+ pi.getBusinessKey() + "\n\tprocessDefinitionId:"
+ pi.getProcessDefinitionId() + "\n\tprocessInstanceId:"
+ pi.getProcessInstanceId() + "\n\tsuspend:"
+ pi.isSuspended());
} catch (Exception e) {
e.printStackTrace();
} return pi;
} /**
* 方法:角色的流程任务列表
*
* @title: findTaskByProcessId
* @param
* @return List<Task>
*/
public List<Task> findTaskByGroupName(String groupname) {
List<Task> list = null;
try {
list = taskService.createTaskQuery().taskCandidateGroup(groupname)
.list();
for (Task task : list) {
String a = task.getAssignee();
logger.info("task========:" + task.getName() + "------------"
+ task.getId());
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
} /**
* 方法分配任务给指定人员
*
* @title: claimTask
* @param
* @return void
*/
public void claimTask(Task task, String username) {
try {
// claim it
if (StringUtils.isBlank(task.getAssignee())) {
taskService.claim(task.getId(), username);
}
} catch (Exception e) {
e.printStackTrace();
}
} /**
* 方法 :任务签收人
*
* @title: setAssignee
* @param
* @return void
*/
public void setAssignee(String taskid, String username) {
try {
taskService.setAssignee(taskid, username);
} catch (Exception e) {
e.printStackTrace();
}
} /**
* 方法 :获得任务
*
* @title: getCurrentTaskByUserid
* @param
* @return Task
*/
public Task getCurrentTaskByUserid(String userId) {
Task task = null;
try {
List<Task> refuseTaskList = taskService.createTaskQuery()
.taskAssignee(userId).active().orderByTaskPriority().desc()
.orderByTaskCreateTime().desc().list();
} catch (Exception e) {
e.printStackTrace();
}
return task;
} /**
* 方法 :获得当前活动的任务
*
* @title: getCurrentActiveTaskBypid
* @param
* @return Task
*/
public Task getCurrentActiveTaskBypid(String processInstanceId) {
Task task = null;
try {
task = taskService.createTaskQuery().processInstanceId(
processInstanceId).active().singleResult();
} catch (Exception e) {
e.printStackTrace();
}
return task;
} /**
* 方法 :完成当前的任务
*
* @title: completeTask
* @param
* @return void
*/
public void completeTask(String taskid, Map<String, Object> varsMap) {
try {
taskService.complete(taskid, varsMap);
} catch (Exception e) {
e.printStackTrace();
}
} /**
* 方法 :根据用户id查看代办列表
*
* @title: findUndoTask
* @param
* @return List
*/
public List findUndoTask(String userId, String processDefinitionId) {
// 签收未完成的任务
List<Task> todoList = taskService.createTaskQuery()
.processDefinitionKey(processDefinitionId).taskAssignee(userId)
.active().orderByTaskPriority().desc().orderByTaskCreateTime()
.desc().list();
logger.info("\n用户“" + userId + "”签收未完成的任务:" + todoList.size() + "个.");
// 未签收的任务
List<Task> unsignedTasks = taskService.createTaskQuery()
.processDefinitionKey(processDefinitionId).taskCandidateUser(
userId).active().orderByTaskPriority().desc()
.orderByTaskCreateTime().desc().list();
logger
.info("\n用户“" + userId + "”未签收的任务:" + unsignedTasks.size()
+ "个.");
List<Task> allList = new ArrayList<Task>();
allList.addAll(todoList);
allList.addAll(unsignedTasks);
return allList;
} /**
* 方法 :用户已经发布的流程(该用户创建的已经结束的流程)
*
* @title: findPublishedOfFinishedTaskList
* @param
* @return List
*/
public List findPublishedOfFinishedTaskList(String userId) {
List<HistoricProcessInstance> list = historyService
.createHistoricProcessInstanceQuery().startedBy(userId)
.finished().list();
logger.info("=============当前用户启动的已经结束的流程=======" + list.size());
return list;
} /**
* 方法 :用户已经发布的流程(该用户创建的已经结束的流程)
*
* @title: findRunningProcessInstaceList
* @param
* @return List
*/
public List findRunningProcessInstaceList(String processDefinitionId) {
List<ProcessInstance> list = runtimeService
.createProcessInstanceQuery().processDefinitionKey(
processDefinitionId).active().list();
logger.info("=============运行中的流程个数=======" + list.size());
return list;
} /**
* 方法 :获取已经结束的流程
*
* @title: findFinishedProcessInstaces
* @param
* @return List
*/
public List findFinishedProcessInstaces(String processDefinitionId) {
List<HistoricProcessInstance> list = historyService
.createHistoricProcessInstanceQuery().processDefinitionKey(
processDefinitionId).finished().list();
logger.info("=============已经结束的流程个数=======" + list.size());
return list;
} /**
* 方法 :根据流程id 查看历史记录
*
* @title: findHistoryByproid
* @param
* @return List
*/
public List findHistoryByproid(String procId) {
List listh2 = historyService.createHistoricDetailQuery()
.formProperties().processInstanceId(procId)
.orderByVariableName().asc().list();
return listh2;
} /**
* 方法 :查询流程定义对象
*
* @title: getProcessDefinition
* @param
* @return ProcessDefinition
*/
public ProcessDefinition getProcessDefinition(String processDefinitionId) {
ProcessDefinition processDefinition = repositoryService
.createProcessDefinitionQuery().processDefinitionId(
processDefinitionId).singleResult();
return processDefinition;
} /**
* 方法 :查询当前活动的流程实例
*
* @title: getCurrentProcess
* @param
* @return ProcessDefinition
*/
public ProcessDefinition getCurrentProcess(String processInstanceId) {
ProcessDefinition processDefinition = (ProcessDefinition) runtimeService
.createProcessInstanceQuery().processInstanceId(
processInstanceId).active().singleResult();
return processDefinition;
} /**
* 方法 :查找流程表中的用户User列表
*
* @title: findListByUserid
* @param
* @return List<User>
*/
public List<User> findUserByUserid(String userId) {
List<org.activiti.engine.identity.User> list = identityService
.createUserQuery().userId(userId).list();
return list;
} /**
* 方法 :保存用户到流程表中
*
* @title: saveActiviteUser
* @param
* @return void
*/
public void saveActiviteUser(User activitiUser) {
identityService.saveUser(activitiUser);
} /**
* 方法 :查找流程中的用户组Group列表
*
* @title: findgGroupByID
* @param
* @return List<Group>
*/
public List<Group> findgGroupByID(String groupId) {
List<Group> list = identityService.createGroupQuery().groupId(groupId)
.list();
return list;
} }
五、调用方法
// 获得流程id,启动不同的流程
String processDefId = activitiCoreService.getProcessDefID(groupId);
ProcessInstance pi = activitiCoreService.startProcessInstanceByKey(businessKey, userId,varsMap, processDefId);
/查看当前任务节点
task = activitiCoreService.getCurrentActiveTaskBypid(person.getProcessInstanceId());
//分配任务
activitiCoreService.claimTask(task, userId);
activiti整合开发实例总结的更多相关文章
- Java Web整合开发实战:基于Struts 2+Hibernate+Spring 目录
第1篇 Java Web开发基础第1章 Web的工作机制( 教学视频:31分钟) 1.1 理解Web的概念 1.1.1 Web的定义 1.1.2 Web的三个核心标准 1.2 C/S与B/S两种软件体 ...
- RDIFramework.NET -.NET快速信息化系统开发整合框架 【开发实例 EasyUI】之产品管理(WebForm版)
RDIFramework.NET—.NET快速开发整合框架 [开发实例]之产品管理(WebForm版) 接上篇:RDIFramework.NET (.NET快速信息化系统开发整合框架) [开发实例]之 ...
- RDIFramework.NET-.NET快速信息化系统开发整合框架 【开发实例 EasyUI】之产品管理(MVC版)
RDIFramework.NET—.NET快速开发整合框架 [开发实例]之产品管理(MVC版) 接上篇:RDIFramework.NET (.NET快速信息化系统开发整合框架) [开发实例]之产品管理 ...
- Spring2.5整合Ibatis入门级开发实例
分类: SPRING IBATIS2010-11-22 20:19 1170人阅读 评论(0) 收藏 举报 ibatisspringstringpropertiesclassuser 最近一直在看 ...
- Thinkphp5整合微信扫码支付开发实例
ThinkPHP框架是比较多人用的,曾经做过的一个Thinkphp5整合微信扫码支付开发实例,分享出来大家一起学习 打开首页生成订单,并显示支付二维码 public function index() ...
- React-Native-Android-Studio整合开发+环境配置+官方实例
linux下React Native开发环境搭建,使用Android-studio工具进行React Native整合开发. 参考React Native的官方文档,通过图文详细记录开发过程.可以查看 ...
- 轻量级Java EE企业应用实战(第4版):Struts 2+Spring 4+Hibernate整合开发(含CD光盘1张)
轻量级Java EE企业应用实战(第4版):Struts 2+Spring 4+Hibernate整合开发(含CD光盘1张)(国家级奖项获奖作品升级版,四版累计印刷27次发行量超10万册的轻量级Jav ...
- [Spring框架]Spring开发实例: XML+注解.
前言: 本文为自己学习Spring记录所用, 文章内容包括Spring的概述已经简单开发, 主要涉及IOC相关知识, 希望能够对新入门Spring的同学有帮助, 也希望大家一起讨论相关的知识. 一. ...
- RDIFramework.NET V2.8版本 ━ 开发实例之产品管理(WinForm)
RDIFramework.NET V2.8版本 ━ 开发实例之产品管理(WinForm) 现在,我们使用.NET快速开发整合框架(RDIFramework.NET)来开发一个应用,此应用皆在说明如何使 ...
随机推荐
- (转)关于MongoDB你需要知道的几件事
本文列举了颇让作者困惑的一些MongoDB限制,如果你也打算使用MongoDB,那么至少要提前了解这些限制,以免遇到的时候措手不及. 消耗磁盘空间 这是我的第一个困惑:MongoDB会消耗太多的磁盘空 ...
- Hexo - Template render error unexpected token
问题与分析 今天发现在使用hexo g时报错如下: FATAL Something's wrong. Maybe you can find the solution here: http://hexo ...
- git合并某次提交到某个分支
有的时候,在develop分支开发,是大家公用的开发分支,但是只想合并自己提交的到master,如何操作呢?那就要用cherry-pick了. 语法 git cherry-pick commitid ...
- SpringBoot | SpringBoot启动错误
Error starting ApplicationContext. To display the conditions report re-run your application with 'de ...
- SpringBoot | Data Access
https://docs.spring.io/spring-boot/docs/current/reference/html/howto-data-access.html 配置数据源启动器.
- 牛客寒假6-G.区间或和
链接:https://ac.nowcoder.com/acm/contest/332/G 题意: 求a|(a+1)|(a+2)|...|(b-1)|b. 思路: 求a-b的差的每一个二进制位 自己也看 ...
- UVa12298(生成函数的简单应用+FFT)
I have a set of super poker cards, consisting of an infinite number of cards. For each positive compo ...
- ctypes to load library in c/c++
cdll.LoadLibrary(...) restype (default is c_int) argtypes (what's the default? c_int?) customized da ...
- C. Functions again DP + 分类讨论
http://codeforces.com/contest/789/problem/C 首先按题目要求处理出dis数组. 那么对于任意一个区间,[L, R],是dis[L] - dis[L + 1] ...
- java数据类型是有符号的,那与有些无符号的如何区别
一.首先需要明白数据类型有符号与无符号的概念 最明显的区别就是二者表示的范围不同: 无符号数中,所有的位都用于直接表示该值的大小.有符号数中最高位用于表示正负,所以,当为正值时,该数的最大值就会变小. ...