Activiti5,相信做過OA系統的對它不陌生。這次資料庫使用的是Mysql和Activiti使用的版本是5.15,具體如何安裝請參考:《Activiti5.16使用者手冊》----12. Eclipse Designer(安裝跟版本沒有什麼大的改變)http://www.mossle.com/docs/activiti/
首先我們在寫代碼前:
切換到Java的角度,建立一個Java項目(這裡叫activiti1)然後打開Java Build Path,參考以下圖進行更改:
再寫代碼前:先寫一個環境的測試demo(記得要導入需要的jar包):
jar包:
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration;
import org.junit.Test;
public class TestEnv {
//搭建環境(建立流程引擎配置對象)
StandaloneProcessEngineConfiguration A;
@Test
public void test() {
// 1.建立流程引擎配置對象
ProcessEngineConfiguration configuration = ProcessEngineConfiguration
.createStandaloneProcessEngineConfiguration();
// 2.完善資料庫相關配置
configuration.setJdbcDriver("com.mysql.jdbc.Driver");
configuration
.setJdbcUrl("jdbc:mysql:///activiti1?createDatabaseIfNotExist=true");
configuration.setJdbcUsername("root");
configuration.setJdbcPassword("");
// 設定資料庫建表政策,預設為false
configuration
.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
// 3.使用配置對象建立ProcessEngine
ProcessEngine processEngine = configuration.buildProcessEngine();
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------
現在來寫第一個HelloWorld
①上面寫的環境測試的資料庫相關配置其實可以寫到一個配置檔案(activiti.cfg.xml)中
activiti.cfg.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- <bean id="processEngineConfiguration" class="org.activiti.engine.ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration"> -->
<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:///activiti1?createDatabaseIfNotExist=true"></property>
<property name="jdbcUsername" value="root"></property>
<property name="jdbcPassword" value=""></property>
<!-- 建表政策 -->
<property name="databaseSchemaUpdate" value="true"></property>
</bean>
</beans>
②在看流程圖前首先要設定讓Eclipse儲存PNG圖檔:(如果看不到Activiti麻煩你檢查是否成功):
将“Create Process definition image when saving the diagram”勾上
③參考圖檔建立相應的檔案:
與其相對應的xml檔案:
helloWorld.xml:
<?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>
<endEvent id="endevent1" name="End"></endEvent>
<userTask id="員工請假" name="員工請假" activiti:assignee="張三"></userTask>
<sequenceFlow id="flow1" sourceRef="startevent1" targetRef="員工請假"></sequenceFlow>
<sequenceFlow id="flow3" sourceRef="員工請假" targetRef="usertask1"></sequenceFlow>
<sequenceFlow id="flow4" sourceRef="usertask1" targetRef="endevent1"></sequenceFlow>
<userTask id="usertask1" name="老闆審批" activiti:assignee="老闆"></userTask>
</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="270.0" y="130.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
<omgdc:Bounds height="35.0" width="35.0" x="270.0" y="450.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="員工請假" id="BPMNShape_員工請假">
<omgdc:Bounds height="55.0" width="105.0" x="235.0" y="200.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
<omgdc:Bounds height="55.0" width="105.0" x="235.0" y="320.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
<omgdi:waypoint x="287.0" y="165.0"></omgdi:waypoint>
<omgdi:waypoint x="287.0" y="200.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
<omgdi:waypoint x="287.0" y="255.0"></omgdi:waypoint>
<omgdi:waypoint x="287.0" y="320.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4">
<omgdi:waypoint x="287.0" y="375.0"></omgdi:waypoint>
<omgdi:waypoint x="287.0" y="450.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
紅色圈起來的比較重要,需要人工填寫的:
④HelloWorld的測試代碼:(初次使用可将3.檢視任務和4.辦理任務注釋掉)
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.task.Task;
import org.junit.Test;
public class HelloWorld {
// 1.釋出流程規則
@Test
public void deployFlow() throws Exception {
// 建立測試環境(建立核心ProcessEngine,監測環境搭建是否正确)
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RepositoryService repositoryService = processEngine
.getRepositoryService();
DeploymentBuilder builder = repositoryService.createDeployment();
builder.addClasspathResource("helloworld.bpmn").addClasspathResource(
"helloworld.png");
builder.deploy();
}
// 2.啟動流程執行個體
@Test
public void startFlow() throws Exception {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RuntimeService runtimeService = processEngine.getRuntimeService();
runtimeService.startProcessInstanceByKey("myProcess");
}
// 3.檢視任務
@Test
public void quertTask() {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
TaskService taskService = processEngine.getTaskService();
String userId = "張三";
List<Task> tasks = taskService.createTaskQuery().taskAssignee(userId)
.list();
for (Task task : tasks) {
System.out.println("Id:" + task.getId() + ",name:" + task.getName()
+ ",assignee:" + task.getAssignee() + ",createTime:"
+ task.getCreateTime());
}
}
//4.辦理任務
@Test
public void completeTask() throws Exception{
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
TaskService taskService = processEngine.getTaskService();
String taskId="204";
taskService.complete(taskId);
}
}
如果你在建立的資料庫(activiti1)中看到如下圖的表:證明你已經成功了:
相關工程的代碼下載下傳位址:
http://download.csdn.net/detail/u011487470/7748047