activiti 环境搭建

activiti是一个目前比较流行的工作流引擎。譬如请假流程、财务报销流程等,使用activiti能够大大简化程序的开发。
activiti就是一个jar包,接收一个xml格式的流程定义,简单的配置下,即可使用。

activiti 环境搭建

其中MyProcess.bpmn(其实就是个xml)定义我们的流程(使用activiti designer插件):
activiti 环境搭建

其xml source为:
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test">
  <process id="myProcess" name="My process" isExecutable="true">
    <startEvent id="startevent1" name="Start"></startEvent>
    <scriptTask id="scripttask1" name="Script Task" scriptFormat="groovy" activiti:autoStoreVariables="false">
      <script>System.out.println("hahaha")</script>
    </scriptTask>
    <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="scripttask1"></sequenceFlow>
    <userTask id="usertask1" name="请假申请" activiti:candidateGroups="employee"></userTask>
    <sequenceFlow id="flow2" sourceRef="scripttask1" targetRef="usertask1"></sequenceFlow>
    <userTask id="usertask2" name="领导审批" activiti:candidateGroups="manager"></userTask>
    <sequenceFlow id="flow3" sourceRef="usertask1" targetRef="usertask2"></sequenceFlow>
    <endEvent id="endevent1" name="End"></endEvent>
    <sequenceFlow id="flow4" sourceRef="usertask2" targetRef="endevent1"></sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_myProcess">
    <bpmndi:BPMNPlane bpmnElement="myProcess" id="BPMNPlane_myProcess">
      <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="190.0" y="150.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="scripttask1" id="BPMNShape_scripttask1">
        <omgdc:Bounds height="55.0" width="105.0" x="290.0" y="140.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
        <omgdc:Bounds height="55.0" width="105.0" x="440.0" y="140.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2">
        <omgdc:Bounds height="55.0" width="105.0" x="590.0" y="140.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="740.0" y="150.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
        <omgdi:waypoint x="225.0" y="167.0"></omgdi:waypoint>
        <omgdi:waypoint x="290.0" y="167.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
        <omgdi:waypoint x="395.0" y="167.0"></omgdi:waypoint>
        <omgdi:waypoint x="440.0" y="167.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
        <omgdi:waypoint x="545.0" y="167.0"></omgdi:waypoint>
        <omgdi:waypoint x="590.0" y="167.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4">
        <omgdi:waypoint x="695.0" y="167.0"></omgdi:waypoint>
        <omgdi:waypoint x="740.0" y="167.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>



工程中的activiti.cfg.xml为整个activiti的配置文件,主要还是配置数据库
<?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="databaseType" value="h2" />
		<!-- <property name="jdbcUrl" value="jdbc:h2:~/activiti;DB_CLOSE_DELAY=1000" 
			/> -->
		<property name="jdbcUrl" value="jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000" />
		<property name="jdbcDriver" value="org.h2.Driver" />
		<property name="jdbcUsername" value="sa" />
		<property name="jdbcPassword" value="" />
		<property name="databaseSchemaUpdate" value="true" />
		<property name="jobExecutorActivate" value="false" />
		<property name="mailServerHost" value="mail.my-corp.com" />
		<property name="mailServerPort" value="5025" />
	</bean>
</beans> 


这里面使用了默认的h2database,jdbc:h2:mem:activiti表示使用内存数据库,即程序运行完毕,流程数据将不保存;而注释掉的那行jdbc:h2:~/activiti意味着将数据库文件保存在默认的目录。



测试下activiti
import java.util.List;



import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines; 
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
public class ActivitiTest {
	public static void main(String[] args){
		ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();  
        RepositoryService repositoryService = processEngine.getRepositoryService();  
        TaskService taskService = processEngine.getTaskService();
  
        DeploymentBuilder builder = repositoryService.createDeployment();  
  
        builder.addClasspathResource("diagrams/MyProcess.bpmn");  
  
        builder.deploy();   
  
        RuntimeService runtimeService = processEngine.getRuntimeService();  
  
        String piid = runtimeService.startProcessInstanceByKey("myProcess").getProcessInstanceId();//启动流程
        Task task = taskService.createTaskQuery().processInstanceId(piid).singleResult();//拿到刚启动流程执行到的第一个任务
        taskService.delegateTask(task.getId(), "lucy");//将此任务分配给lucy
        Task task2 = taskService.createTaskQuery().processInstanceId(piid).singleResult();
        System.out.println(task2.getAssignee());
        List<Task> tasks = taskService.createTaskQuery().taskAssignee("lucy").list();//查询lucy的所有任务
        System.out.println("ok......"+tasks.size());    
	}
}






可见,activiti还是非常轻量级的,也易于集成至web系统。

你可能感兴趣的:(Activiti)