activiti5第一弹-----基本的activiti示例
生活随笔
收集整理的這篇文章主要介紹了
activiti5第一弹-----基本的activiti示例
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
建立一個普通的javaSE工程,工程的目錄結(jié)構(gòu)如下:
需要的jar包如下:
在config文件夾下創(chuàng)建log4j.properties用來輸入日志,內(nèi)容如下:
log4j.rootLogger=INFO, CA # ConsoleAppender log4j.appender.CA=org.apache.log4j.ConsoleAppender log4j.appender.CA.layout=org.apache.log4j.PatternLayout log4j.appender.CA.layout.ConversionPattern= %d{hh:mm:ss,SSS} [%t] %-5p %c %x - %m%n
在config文件夾下創(chuàng)建activiti.cfg.xml(每個框架都有自己的配置文件,在沒有使用spring托管框架的時候,這個配置文件管理框架的全部配置),內(nèi)容如下
<?xml version="1.0"?> <beans default-lazy-init="false"xsi:schemaLocation=" http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd 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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee"xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context"xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://www.springframework.org/schema/beans"><beanclass="org.activiti.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration"id="processEngineConfiguration"> <span style="white-space:pre"> </span><!--配置數(shù)據(jù)庫信息,這里使用mysql 但是測試時推薦使用h2數(shù)據(jù)庫 見activiti自帶的demo中的配置文件<span style="font-family: Arial, Helvetica, sans-serif;">--></span> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/activi" /><property name="jdbcDriver" value="com.mysql.jdbc.Driver" /><property name="jdbcUsername" value="root" /><property name="jdbcPassword" value="root" /> <span style="white-space:pre"> </span><!--當表結(jié)構(gòu)不存在時,生成activiti的23個表--><property name="databaseSchemaUpdate" value="true" /> <span style="white-space:pre"> </span><!--工作流轉(zhuǎn)--><property name="jobExecutorActivate" value="true" /><!--郵件--> <span style="white-space:pre"> </span><property name="mailServerHost" value="mail.my-corp.com" /><property name="mailServerPort" value="5025" /><!--歷史--> <span style="white-space:pre"> </span><property name="history" value="full"></property></bean> </beans>
在progress文件夾下建立我們的流程定義,右鍵選擇new-----other---------Activiti Diagram(之前需要安裝eclipse的activiti插件,推薦在線安裝---Name: Activiti BPMN 2.0 designer ? ??Location: http://activiti.org/designer/update/)
建立如下的流程定義
在該流程右鍵選擇xml打開方式,可以得到流程的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"> <!-- 流程內(nèi)的信息--> ? <process id="myProcess" name="My process" isExecutable="true"><startEvent id="startevent1" name="Start"></startEvent><userTask id="usertask1" name="User Task"></userTask><sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow><endEvent id="endevent1" name="End"></endEvent><sequenceFlow id="flow2" sourceRef="usertask1" 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="140.0" y="180.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1"><omgdc:Bounds height="55.0" width="105.0" x="220.0" y="170.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1"><omgdc:Bounds height="35.0" width="35.0" x="370.0" y="180.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1"><omgdi:waypoint x="175.0" y="197.0"></omgdi:waypoint><omgdi:waypoint x="220.0" y="197.0"></omgdi:waypoint></bpmndi:BPMNEdge><bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2"><omgdi:waypoint x="325.0" y="197.0"></omgdi:waypoint><omgdi:waypoint x="370.0" y="197.0"></omgdi:waypoint></bpmndi:BPMNEdge></bpmndi:BPMNPlane></bpmndi:BPMNDiagram> </definitions>
可以得到流程的id為 myProcess,當然這是默認的,你可以隨意修改.
最后就可以編寫測試類來測試這個流程了
package activiti_001;import junit.framework.Assert;import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngineConfiguration; import org.activiti.engine.RepositoryService; import org.activiti.engine.RuntimeService; import org.activiti.engine.TaskService; import org.activiti.engine.runtime.ProcessInstance; import org.activiti.engine.task.Task; import org.junit.Test;public class MyProcessTest {@Testpublic void test() {// 通過默認的資源獲得流程引擎的配置信息(默認的--當然就是activiti.cfg.xml 了)ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResourceDefault();// 通過配置信息獲得流程引擎ProcessEngine processEngine = configuration.buildProcessEngine();// 流程引擎是activiti的核心,也就是activiti的門戶,通過它可以獲得各種服務。// 獲得部署服務RepositoryService repositoryService = processEngine.getRepositoryService();// 獲得運行時服務RuntimeService runtimeService = processEngine.getRuntimeService();// 獲得任務服務TaskService taskService = processEngine.getTaskService();// 部署我們定義的流程repositoryService.createDeployment().addClasspathResource("MyProcess.bpmn").deploy();// 啟動定義的流程 一定要使用BYKEYProcessInstance instance = runtimeService.startProcessInstanceByKey("myProcess");// 通過任務服務來查詢?nèi)蝿誘ask task = taskService.createTaskQuery().singleResult();// 斷言任務的名稱System.out.println(task.getName());Assert.assertTrue("User Task".equals(task.getName()));// 完成任務(因為這是一個用戶任務,所以需要模擬完成一下)taskService.complete(task.getId());task = taskService.createTaskQuery().singleResult();// 流程結(jié)束}}
總結(jié)
以上是生活随笔為你收集整理的activiti5第一弹-----基本的activiti示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: activiti5第五弹 service
- 下一篇: shell中的expect命令