Activiti工作流--流程定义--之三

由于概念性的东西这里就不在做更多的介绍了,下面直接对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"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd      
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
  	
  	 <bean id = "processEngineConfiguration" 
  		class = "org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
  	 	<property name="jdbcDriver" value="com.mysql.jdbc.Driver"></property>
  	 	<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/activiti?useUnicode=true&characterEncoding=utf-8"></property>
  	 	<property name="jdbcUsername" value="root"></property>
  	 	<property name="jdbcPassword" value="root"></property>
  	  	<property name="databaseSchemaUpdate" value="false"></property>	
  	 </bean>
</beans>

通过默认的方式,会自动读取配置文件

private ProcessEngine processEngine = null;

@Before
public void initProcessEngine(){
	processEngine = ProcessEngines.getDefaultProcessEngine();
}

方式二:

/**
 * 新建配置对象,在配置对象设置数据库连接四要素和建表策略
 */
@Test
public void testActivitiEngine(){
	ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration();
	//配置数据库连接四要素
	configuration.setJdbcUrl("jdbc:mysql://127.0.0.1/activiti?useUnicode=true&characterEncoding=utf-8");
	configuration.setJdbcDriver("com.mysql.jdbc.Driver");
	configuration.setJdbcUsername("root");
	configuration.setJdbcPassword("root");
	/**
	 * DB_SCHEMA_UPDATE_TRUE	如果数据库中不存在表就创建,存在就不创建
	 * DB_SCHEMA_UPDATE_FALSE	如果数据库中不存在就抛出异常
	 * DB_SCHEMA_UPDATE_CREATE_DROP	每次都是先删除表,在创建表
	 */
	configuration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
	ProcessEngine processEngine = configuration.buildProcessEngine();
	System.out.println(processEngine);
}
/**
 * 通过配置文件获取数据库的链接
 */
@Test
public void testActivitiEnginecfg(){
	ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
	ProcessEngine processEngine = configuration.buildProcessEngine();
	System.out.println(processEngine);
}

流程定义好后,需要进行部署

流程的定义需要在eclipse中定义好(这里以eclipse为例),定义好之后会生成两个文件

@Test
public void testLeaveProcessDeploy(){
	//创建部署构建器
	/*RepositoryService repositoryService = processEngine.getRepositoryService();
	DeploymentBuilder deploymentBuilder = repositoryService.createDeployment();
	//添加资源
	deploymentBuilder.addClasspathResource("diagram/leave.bpmn");
	deploymentBuilder.addClasspathResource("diagram/leave.png");
	deploymentBuilder.name("请假流程");
	//进行部署
	Deployment deployment = deploymentBuilder.deploy();*/
	
	//avtiviti支持链式编程
	Deployment deployment = processEngine.getRepositoryService()
						.createDeployment()
						.addClasspathResource("diagram/leave.bpmn")
						.addClasspathResource("diagram/leave.png")
						.name("请假流程")
						.deploy();
	
	System.out.println("流程部署ID:"+deployment.getId());
	System.out.println("流程部署名称:"+deployment.getName());
}

流程图
leave.bpmn代码

<?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="leave" name="leaveProcess" isExecutable="true">
    <startEvent id="startevent1" name="Start"></startEvent>
    <userTask id="usertask1" name="请假申请" activiti:assignee="张三"></userTask>
    <userTask id="usertask2" name="领导审批" activiti:assignee="李四"></userTask>
    <endEvent id="endevent1" name="End"></endEvent>
    <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
    <sequenceFlow id="flow2" sourceRef="usertask1" targetRef="usertask2"></sequenceFlow>
    <sequenceFlow id="flow3" sourceRef="usertask2" targetRef="endevent1"></sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_leave">
    <bpmndi:BPMNPlane bpmnElement="leave" id="BPMNPlane_leave">
      <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="375.0" y="22.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
        <omgdc:Bounds height="55.0" width="105.0" x="340.0" y="122.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2">
        <omgdc:Bounds height="55.0" width="105.0" x="340.0" y="242.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="375.0" y="352.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
        <omgdi:waypoint x="392.0" y="57.0"></omgdi:waypoint>
        <omgdi:waypoint x="392.0" y="122.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
        <omgdi:waypoint x="392.0" y="177.0"></omgdi:waypoint>
        <omgdi:waypoint x="392.0" y="242.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
        <omgdi:waypoint x="392.0" y="297.0"></omgdi:waypoint>
        <omgdi:waypoint x="392.0" y="352.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

Activiti工作流--流程定义--之三_第1张图片

流程启动

@Test
public void testStartLeaveProcess(){
	String processDefinitionKey = "leave";
	ProcessInstance processInstance = processEngine.getRuntimeService()
										.startProcessInstanceByKey(processDefinitionKey);
	System.out.println("流程部署的ID:"+processInstance.getDeploymentId());
	System.out.println("流程定义的ID:"+processInstance.getProcessDefinitionId());
	System.out.println("流程实例的ID:"+processInstance.getProcessInstanceId());
}

流程的处理

@Test
public void testQueryTask(){
	String assignee = "李四";
	String processInstanceId = "7501";
	List<Task> listTask = processEngine.getTaskService()
							.createTaskQuery()
							.taskAssignee(assignee)
							.processInstanceId(processInstanceId)
							.list();
	if (listTask != null && listTask.size() > 0) {
		for (Task task : listTask) {
			System.out.println("流程定义的ID:"+task.getProcessDefinitionId());
			System.out.println("流程实例的ID:"+task.getProcessInstanceId());
			System.out.println("执行对象ID:"+task.getExecutionId());
			System.out.println("任务ID:"+task.getId());
			System.out.println("任务名称:"+task.getName());
			System.out.println("任务创建的时间:"+task.getCreateTime());
			System.out.println("================================");
		}
	}
}

流程处理过程

@Test
public void testCompleteTask(){
	String taskId = "27502";
	processEngine.getTaskService()
					.complete(taskId);
	System.out.println("任务完成");
}

【上一篇:Activiti工作流程–工作流数据库表详细介绍–之二】
【下一篇:Activiti工作流–流程定义的相关操作–之四】

你可能感兴趣的:(Activiti工作流)