在前面兩節,咱們介紹瞭如何部署activiti三個應用以及如何使用第三方數據庫,若是你還沒閱讀前兩章也不影響本文的閱讀,若是有興趣瞭解下,能夠點擊如下連接java
從本章開始,就正式開始activiti程序開發,咱們先從一個最簡單的activiti流程開發,流程圖以下:mysql
流程很簡單,發起後,由管理員admin
審批,而後結束。git
bpmn源文件代碼以下github
<?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/processdef"> <process id="SimpleProcess" name="SimpleProcess" isExecutable="true"> <startEvent id="startEvent1"></startEvent> <userTask id="sid-2B2AA039-82D2-479C-8FE9-6F48E4478BD8" name="Admin Approver" activiti:assignee="admin"> <extensionElements> <modeler:activiti-idm-assignee xmlns:modeler="http://activiti.com/modeler"><![CDATA[true]]></modeler:activiti-idm-assignee> <modeler:assignee-info-email xmlns:modeler="http://activiti.com/modeler"><![CDATA[admin]]></modeler:assignee-info-email> <modeler:assignee-info-lastname xmlns:modeler="http://activiti.com/modeler"><![CDATA[Administrator]]></modeler:assignee-info-lastname> <modeler:initiator-can-complete xmlns:modeler="http://activiti.com/modeler"><![CDATA[false]]></modeler:initiator-can-complete> </extensionElements> </userTask> <sequenceFlow id="sid-F506EEC7-0411-4A97-930D-87500FF1414F" sourceRef="startEvent1" targetRef="sid-2B2AA039-82D2-479C-8FE9-6F48E4478BD8"></sequenceFlow> <endEvent id="sid-2C31753A-3AE2-4C91-B4F8-3B5EED2B4FC5"></endEvent> <sequenceFlow id="sid-9FC989EE-BBC3-4EB2-A7AF-DA4F2649A68F" sourceRef="sid-2B2AA039-82D2-479C-8FE9-6F48E4478BD8" targetRef="sid-2C31753A-3AE2-4C91-B4F8-3B5EED2B4FC5"></sequenceFlow> </process> <bpmndi:BPMNDiagram id="BPMNDiagram_SimpleProcess"> <bpmndi:BPMNPlane bpmnElement="SimpleProcess" id="BPMNPlane_SimpleProcess"> <bpmndi:BPMNShape bpmnElement="startEvent1" id="BPMNShape_startEvent1"> <omgdc:Bounds height="30.0" width="30.0" x="100.0" y="163.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="sid-2B2AA039-82D2-479C-8FE9-6F48E4478BD8" id="BPMNShape_sid-2B2AA039-82D2-479C-8FE9-6F48E4478BD8"> <omgdc:Bounds height="80.0" width="100.0" x="255.0" y="138.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="sid-2C31753A-3AE2-4C91-B4F8-3B5EED2B4FC5" id="BPMNShape_sid-2C31753A-3AE2-4C91-B4F8-3B5EED2B4FC5"> <omgdc:Bounds height="28.0" width="28.0" x="465.0" y="164.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNEdge bpmnElement="sid-F506EEC7-0411-4A97-930D-87500FF1414F" id="BPMNEdge_sid-F506EEC7-0411-4A97-930D-87500FF1414F"> <omgdi:waypoint x="130.0" y="178.0"></omgdi:waypoint> <omgdi:waypoint x="255.0" y="178.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="sid-9FC989EE-BBC3-4EB2-A7AF-DA4F2649A68F" id="BPMNEdge_sid-9FC989EE-BBC3-4EB2-A7AF-DA4F2649A68F"> <omgdi:waypoint x="355.0" y="178.0"></omgdi:waypoint> <omgdi:waypoint x="465.0" y="178.0"></omgdi:waypoint> </bpmndi:BPMNEdge> </bpmndi:BPMNPlane> </bpmndi:BPMNDiagram> </definitions>
你可使用任何一個支持BPMN2.0的工具進行流程的建模,若是你是用eclipse開發,activiti在eclipse下有一個很是好用的插件,而且activit也提供一個web版的建模工具,就在activiti-app這個應用下,關於如何部署該應用,你能夠查看以前的文章SpringBoot Activiti6系列教程(一)-activiti-app部署。以上模型就是經過activiti-app建立導出的。web
Spring Boot是一個應用開發框架,基於spring,相比spring開發,spring boot極大簡化了配置,而且遵照約定優於配置的原則即便0配置也能正常運行,這在spring中是不可思議的。spring boot應用程序能夠獨立運行,框架內嵌web容器,使得web應用程序能夠像本地程序同樣啓動和調試,十分的方便,這種設計方式也使得spring boot應用程序很是適合容器化進行大規模部署,並且spring boot提供了很是豐富的組件,流行的java web框架基本都有spring boot版本,生態十分龐大,是目前java web開發最好的方案。spring
若是你已經對spring boot很是熟悉,能夠跳過該章節。
本次教程代碼都是經過IntelliJ IDEA
進行開發,基於Spring Boot 2.1.2
版本,若是你還沒用過IntelliJ IDEA,建議你嘗試下。
1.
建立一個maven
工程2.
設置項目parent而且加入spring boot的依賴和activiti相關依賴,參考pom.xml以下pom.xmlsql
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.definesys.tutorial.activiti</groupId> <artifactId>activiti-tutorial</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>activiti-tutorial1</module> </modules> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.2.RELEASE</version> <relativePath/> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-spring-boot-starter-basic</artifactId> <version>6.0.0</version> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>1.4.185</version> </dependency> </dependencies> </project>
spring-boot-starter-web
是spring boot用來建立rest服務的組件,h2是一個內存數據庫,若是你沒有配置數據庫,那麼加入h2後,無需配置第三方數據庫便可運行activit應用。數據庫
3.
在resource
文件夾下建立文件夾processes
(文件夾名字不能修改),而且將第一步建立的模型文件拷貝至該文件下。4.
建立兩個接口,/acitiviti/start
能夠發起該流程,/activiti/task?uid=admin
能夠查詢admin的流程待辦,依次建立appliaction,dto,service,controller相關代碼文件,以下本文全部代碼已上傳至github,倉庫地址爲 https://github.com/wls1036/springboot-activiti6-tutorial 歡迎star
ActivititiApplication.javaexpress
@SpringBootApplication(exclude = SecurityAutoConfiguration.class) public class ActivititiApplication { public static void main(String[] args) { SpringApplication.run(ActivititiApplication.class); } }
之因此要加exclude = SecurityAutoConfiguration.class
,緣由是不加的話啓動會報Caused by: java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy
錯誤,多是spring boot的版本緣由引發的。
TaskRepresentation.javaapache
public class TaskRepresentation { private String id; private String name; public TaskRepresentation(String id, String name) { this.id = id; this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
ActivitiService.java
@Service public class ActivitiService { @Autowired private RuntimeService runtimeService; @Autowired private TaskService taskService; /** * start activiti process * * @return instance id */ public String start() { ProcessInstance instance = runtimeService.startProcessInstanceByKey("SimpleProcess"); return instance.getId(); } /** * get user task list * * @param uid * @return user task list */ public List<Task> getTask(String uid) { List<Task> tasks = taskService.createTaskQuery().taskAssignee(uid).list(); return tasks; } }
RuntimeService
包含了activiti運行時接口,能夠經過點擊進入查看源文件,acitivi api設計的至關友好,不少接口看名稱就差很少知道功能,startProcessInstanceByKey是根據流程編號發起流程,參數爲bpmn文件中 process id="SimpleProcess"
中的id字段。
TaskService
包含任務相關api,createTaskQuery能夠建立一個任務查詢,activiti的api使用方法基本都同樣,好比查詢類的,都是createXXXQuery建立一個查詢類後執行查詢條件,list()是返回一個list數組。這裏是指定審批人,在bpmn文件中,咱們指定了審批人爲admin
<userTask id="sid-2B2AA039-82D2-479C-8FE9-6F48E4478BD8" name="Admin Approver" activiti:assignee="admin">
ActivitiController
@RestController @RequestMapping(value = "activiti") public class ActivitiController { @Autowired private ActivitiService service; @RequestMapping(value = "start", method = RequestMethod.GET) public String start() { return service.start(); } @RequestMapping(value = "task", method = RequestMethod.GET) public List<TaskRepresentation> getTask(@RequestParam(value = "uid") String uid) { List<Task> tasks = service.getTask(uid); List<TaskRepresentation> dtos = new ArrayList<>(); for (Task task : tasks) { dtos.add(new TaskRepresentation(task.getId(), task.getName())); } return dtos; } }
curl http://localhost:8080/activiti/start 65
curl http://localhost:8080/activiti/task?uid=admin [{"id":"65","name":"Admin Approver"}]
如今的數據都保存到內存數據庫h2中,程序從新啓動就會丟失,這顯然不是咱們想要的結果,咱們須要將數據保存到外部數據庫,關於如何初始化外部數據庫,能夠參考以前的文章SpringBoot Activiti6系列教程(二)-基於mysql數據庫初始化,這裏不在贅述。假設你已經初始化完數據庫。
@Configuration public class ActivitiConfiguration { @Bean public DataSource database() { return DataSourceBuilder.create() .url("jdbc:mysql://localhost:3306/activiti?useUnicode=true&characterEncoding=utf8") .username("activiti") .password("definesys") .driverClassName("com.mysql.jdbc.Driver") .build(); } }
這裏爲了簡單把鏈接信息寫死在代碼裏,不是一種好的方式,你能夠將鏈接信息放到application.properties裏。
.... <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-spring-boot-starter-basic</artifactId> <version>6.0.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies>
從新調用start
接口後,流程發起後,會在數據庫表ACT_RU_TASK
中建立記錄。
本文介紹了一個最簡單的activiti流程在spring boot中的應用。
本文全部代碼已經上傳至github,倉庫地址爲 https://github.com/wls1036/springboot-activiti6-tutorial歡迎star