activiti工作流使用的一般步骤
一、在eclipse或Myeclipse中安装activiti插件;
二、通过activiti连接数据库,有以下两种连接数据库的形式:
1.通过java代码链接数据库:
public void createTableByOracle(){ ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration(); configuration.setJdbcUrl("jdbc:oracle:thin:@192.168.1.150:1521:orcl"); configuration.setJdbcDriver("oracle.jdbc.OracleDriver"); configuration.setJdbcUsername("scott"); configuration.setJdbcPassword("oracle"); configuration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE); ProcessEngine processEngine = configuration.buildProcessEngine(); System.out.println("-------------------------------------"); System.out.println(processEngine); }
2.通过配置文件配置链接数据库
(1)配置文件:
<?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"> <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://192.168.1.150:3306/officemanage?characterEncoding=utf8" /> <property name="username" value="root" /> <property name="password" value="123456" /> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration"> <property name="dataSource" ref="dataSource" /> <property name="transactionManager" ref="transactionManager" /> <property name="databaseSchemaUpdate" value="true" /> </bean> </beans>
(2)java代码读取配置文件链接数据库:
public void createTableByXML(){ ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); }
三、创建工作流的bpmn类型的文件:
四、部署bpmn和png类型的工作流文件资源:(注意:一个工作流程部署一次即可,以后只需要启动一条流程即可)
public void deployTest(){ System.out.println("开始部署一个审批流程"); ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); Deployment deployment = processEngine.getRepositoryService().createDeployment().name("路费报销审批流程").addClasspathResource("choiceProcess.bpmn").addClasspathResource("choiceProcess.png").deploy(); System.out.println("部署流程完毕"); }
五、启动一条流程:
public void StartProcess(){ System.out.println("开始启动一个审批流程"); ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); ProcessInstance processInstance = processEngine.getRuntimeService().startProcessInstanceByKey("luFeiBaoXiaoShenPi"); System.out.println(processInstance.getId()+"--------"+processInstance.getActivityId()); System.out.println("启动流程完毕"); }
六、查看某个人要完成的任务列表:
public void queryProcess(){ ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); String assignee0 = "lsy"; List<Task> zhangSanList = processEngine.getTaskService().createTaskQuery().taskAssignee(assignee0).orderByTaskCreateTime().asc().list(); System.out.println("****************"+assignee0+"个人任务列表***********************"); for (Task task : zhangSanList) { System.out.println("id:"+task.getId()+",名称:"+task.getName()+",创建时间"+task.getCreateTime()+",人物:"+task.getAssignee()); } }
七、某个人完成自己的某个任务:
1.不带变量:
public void completeTask(){ ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); processEngine.getTaskService().complete("235009"); System.out.println("完成任务"); }
2.带变量:
public void completeTaskByVariables(){ ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); Map<String, Object> variables = new HashMap<String,Object>(); variables.put("money", 501); processEngine.getTaskService().complete("265004",variables); System.out.println("完成任务"); }
八、某个人查看自己已完成的任务列表:
public void queryHistoryTask(){ ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); List<HistoricTaskInstance> historicList = processEngine.getHistoryService().createHistoricTaskInstanceQuery().taskAssignee("lsy").list(); for (HistoricTaskInstance historicTaskInstance : historicList) { System.out.println(historicTaskInstance.getId()+":"+historicTaskInstance.getProcessDefinitionId()+":"+historicTaskInstance.getAssignee()+":"+historicTaskInstance.getExecutionId()+":"+historicTaskInstance.getStartTime()+":"+historicTaskInstance.getEndTime()+":"+historicTaskInstance.getDurationInMillis()); } }