参考手册: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);
时间: 2024-10-09 06:29:19