【Activiti学习之二】Activiti API(一)
环境
JDK 1.8
MySQL 5.6
Tomcat 7
Eclipse-Luna
activiti 6.0
一、Activiti数据查询
准备数据:
- package com.wjy.act;
- import org.activiti.engine.IdentityService;
- import org.activiti.engine.ProcessEngine;
- import org.activiti.engine.ProcessEngines;
- import org.activiti.engine.identity.Group;
- public class SaveGroup {
- public static void main(String[] args) {
- ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
- IdentityService is = engine.getIdentityService();
- for(int i = 0; i < 10; i++) {
- Group group = is.newGroup(String.valueOf(i));
- group.setName("Group_" + i);
- group.setType("TYPE_" + i);
- is.saveGroup(group);//保存到表act_id_group
- }
- engine.close();
- System.exit(0);
- }
- }
list 查询多条和count 数量统计:
- package com.wjy.act;
- import java.util.List;
- import org.activiti.engine.IdentityService;
- import org.activiti.engine.ProcessEngine;
- import org.activiti.engine.ProcessEngines;
- import org.activiti.engine.identity.Group;
- public class TestList {
- public static void main(String[] args) {
- ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
- IdentityService is = engine.getIdentityService();
- //测试list查询
- List<Group> groups = is.createGroupQuery().list();
- for(Group g : groups) {
- System.out.println(g.getId() + "---" + g.getName() + "---" + g.getType());
- }
- //测试 count
- long size = is.createGroupQuery().count();
- System.out.println(size);
- }
- }
listPage 分页查询:
- package com.wjy.act;
- import java.util.List;
- import org.activiti.engine.IdentityService;
- import org.activiti.engine.ProcessEngine;
- import org.activiti.engine.ProcessEngines;
- import org.activiti.engine.identity.Group;
- public class TestListPage {
- public static void main(String[] args) {
- ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
- IdentityService is = engine.getIdentityService();
- //分页查询 listPage
- List<Group> groups = is.createGroupQuery().listPage(1, 5);
- for(Group g : groups) {
- System.out.println(g.getId() + "---" + g.getName() + "---" + g.getType());
- }
- }
- }
singleResult 查询单条
- package com.wjy.act;
- import org.activiti.engine.IdentityService;
- import org.activiti.engine.ProcessEngine;
- import org.activiti.engine.ProcessEngines;
- import org.activiti.engine.identity.Group;
- public class TestSingle {
- public static void main(String[] args) {
- ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
- IdentityService is = engine.getIdentityService();
- //测试singleResult
- Group g = is.createGroupQuery().groupName("Group_0").singleResult();
- System.out.println(g.getId());
- }
- }
asc 升序和desc 降序:
- package com.wjy.act;
- import java.util.List;
- import org.activiti.engine.IdentityService;
- import org.activiti.engine.ProcessEngine;
- import org.activiti.engine.ProcessEngines;
- import org.activiti.engine.identity.Group;
- public class TestSort {
- public static void main(String[] args) {
- ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
- IdentityService is = engine.getIdentityService();
- //测试降序 升序 需要根据字段orderby
- //多字段排序 orderby之后都要跟上asc() 或desc()
- //is.createGroupQuery().orderByGroupId().asc().orderByGroupName().asc().list();
- List<Group> groups = is.createGroupQuery().orderByGroupId().desc().orderByGroupName().asc().list();
- for(Group g : groups) {
- System.out.println(g.getId());
- }
- }
- }
按照字段条件查询:
- package com.wjy.act;
- import java.util.List;
- import org.activiti.engine.IdentityService;
- import org.activiti.engine.ProcessEngine;
- import org.activiti.engine.ProcessEngines;
- import org.activiti.engine.identity.Group;
- public class FieldQuery {
- public static void main(String[] args) {
- ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
- IdentityService is = engine.getIdentityService();
- //按照字段条件查询
- List<Group> groups = is.createGroupQuery().groupName("Group_1").groupType("TYPE_1").list();
- for(Group g : groups) {
- System.out.println(g.getId());
- }
- }
- }
支持原生sql查询:
- package com.wjy.act;
- import java.util.List;
- import org.activiti.engine.IdentityService;
- import org.activiti.engine.ProcessEngine;
- import org.activiti.engine.ProcessEngines;
- import org.activiti.engine.identity.Group;
- public class NativeQuery {
- public static void main(String[] args) {
- ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
- IdentityService is = engine.getIdentityService();
- //支持原生sql语句查询
- List<Group> groups = is.createNativeGroupQuery()
- .sql("SELECT * FROM ACT_ID_GROUP where NAME_ = #{name}")
- .parameter("name", "Group_2").list();
- for (Group g : groups) {
- System.out.println(g.getId());
- }
- }
- }
二、流程文件部署
1、DeploymentBuilder对象
方法addClasspathResource:
- package com.wjy.deploy;
- import java.io.InputStream;
- import org.activiti.engine.ProcessEngine;
- import org.activiti.engine.ProcessEngines;
- import org.activiti.engine.RepositoryService;
- import org.activiti.engine.repository.Deployment;
- import org.activiti.engine.repository.DeploymentBuilder;
- public class TextQuery {
- public static void main(String[] args) throws Exception {
- ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
- // 存储服务
- RepositoryService rs = engine.getRepositoryService();
- DeploymentBuilder builder = rs.createDeployment();
- builder.addClasspathResource("my_text.txt");
- Deployment dep = builder.deploy();
- // 数据查询
- InputStream is = rs.getResourceAsStream(dep.getId(), "my_text.txt");
- int count = is.available();
- byte[] contents = new byte[count];
- is.read(contents);
- String result = new String(contents);
- //输入结果
- System.out.println(result);
- }
- }
方法addInputStream:
- package com.wjy.deploy;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import javax.imageio.ImageIO;
- import org.activiti.engine.ProcessEngine;
- import org.activiti.engine.ProcessEngines;
- import org.activiti.engine.RepositoryService;
- import org.activiti.engine.repository.Deployment;
- import org.activiti.engine.repository.ProcessDefinition;
- public class ImageQuery {
- public static void main(String[] args) throws Exception {
- // 创建流程引擎
- ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
- // 得到流程存储服务对象
- RepositoryService repositoryService = engine.getRepositoryService();
- // 部署一份流程文件与相应的流程图文件
- Deployment dep = repositoryService.createDeployment().addClasspathResource("gen.bpmn").deploy();
- // 查询流程定义
- ProcessDefinition def = repositoryService.createProcessDefinitionQuery().deploymentId(dep.getId()).singleResult();
- // 查询资源文件
- InputStream is = repositoryService.getProcessDiagram(def.getId());
- // 将输入流转换为图片对象
- BufferedImage image = ImageIO.read(is);
- // 保存为图片文件
- File file = new File("resource/result.png");
- if (!file.exists()) file.createNewFile();
- FileOutputStream fos = new FileOutputStream(file);
- ImageIO.write(image, "png", fos);
- fos.close();
- is.close();
- }
- }
方法addZipInputStream:
- package com.wjy.deploy;
- import java.io.File;
- import java.io.FileInputStream;
- import java.util.zip.ZipInputStream;
- import org.activiti.engine.ProcessEngine;
- import org.activiti.engine.ProcessEngines;
- import org.activiti.engine.RepositoryService;
- import org.activiti.engine.repository.DeploymentBuilder;
- public class ZipTest {
- public static void main(String[] args) throws Exception {
- ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
- // 存储服务
- RepositoryService rs = engine.getRepositoryService();
- DeploymentBuilder builder = rs.createDeployment();
- FileInputStream fis = new FileInputStream(new File("resource/datas.zip"));
- ZipInputStream zis = new ZipInputStream(fis);
- builder.addZipInputStream(zis);
- builder.deploy();
- }
- }
方法addBpmnModel:
- package com.wjy.deploy;
- import org.activiti.bpmn.model.BpmnModel;
- import org.activiti.bpmn.model.EndEvent;
- import org.activiti.bpmn.model.SequenceFlow;
- import org.activiti.bpmn.model.StartEvent;
- import org.activiti.bpmn.model.UserTask;
- import org.activiti.engine.ProcessEngine;
- import org.activiti.engine.ProcessEngines;
- import org.activiti.engine.RepositoryService;
- import org.activiti.engine.repository.DeploymentBuilder;
- public class BpmnTest {
- public static void main(String[] args) {
- ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
- // 存储服务
- RepositoryService rs = engine.getRepositoryService();
- DeploymentBuilder builder = rs.createDeployment();
- builder.addBpmnModel("My Process", createProcessModel());
- builder.deploy();
- }
- private static BpmnModel createProcessModel() {
- // 创建BPMN模型对象
- BpmnModel model = new BpmnModel();
- // 创建一个流程定义
- org.activiti.bpmn.model.Process process = new org.activiti.bpmn.model.Process();
- model.addProcess(process);
- process.setId("myProcess");
- process.setName("My Process");
- // 开始事件
- StartEvent startEvent = new StartEvent();
- startEvent.setId("startEvent");
- process.addFlowElement(startEvent);
- // 用户任务
- UserTask userTask = new UserTask();
- userTask.setName("User Task");
- userTask.setId("userTask");
- process.addFlowElement(userTask);
- // 结束事件
- EndEvent endEvent = new EndEvent();
- endEvent.setId("endEvent");
- process.addFlowElement(endEvent);
- // 添加流程顺序
- process.addFlowElement(new SequenceFlow("startEvent", "userTask"));
- process.addFlowElement(new SequenceFlow("userTask", "endEvent"));
- return model;
- }
- }
方法addString和addBytes:略
上面的方法是加载流程方法,另外方法deploy是将加载的流程实现部署,返回对象Deployment,部署记录记录到表表:act_re_deployment,流程则记录到表:act_ge_bytearray。
2、部署验证
默认会进行格式和流程定义验证
格式验证:
- package com.wjy.deploy;
- import org.activiti.engine.ProcessEngine;
- import org.activiti.engine.ProcessEngines;
- import org.activiti.engine.RepositoryService;
- import org.activiti.engine.repository.DeploymentBuilder;
- public class SchemaError {
- public static void main(String[] args) {
- ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
- // 存储服务
- RepositoryService rs = engine.getRepositoryService();
- DeploymentBuilder builder = rs.createDeployment();
- //schema_error.bpmn 格式有问题 比如说在里面加入了无效标签<abc>angus</abc>
- builder.addClasspathResource("error/schema_error.bpmn");
- //关闭格式验证 disableSchemaValidation
- //builder.disableSchemaValidation();
- builder.deploy();
- }
- }
流程验证:
- package com.wjy.deploy;
- import org.activiti.engine.ProcessEngine;
- import org.activiti.engine.ProcessEngines;
- import org.activiti.engine.RepositoryService;
- import org.activiti.engine.repository.DeploymentBuilder;
- public class BpmnError {
- public static void main(String[] args) {
- ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
- // 存储服务
- RepositoryService rs = engine.getRepositoryService();
- DeploymentBuilder builder = rs.createDeployment();
- //bpmn_error.bpmn 流程有问题 比如说有两个开始节点
- builder.addClasspathResource("error/bpmn_error.bpmn");
- //关闭流程验证 disableBpmnValidation
- //builder.disableBpmnValidation();
- builder.deploy();
- }
- }
3、查询部署资源
- package com.wjy.deploy;
- import java.io.InputStream;
- import org.activiti.engine.ProcessEngine;
- import org.activiti.engine.ProcessEngines;
- import org.activiti.engine.RepositoryService;
- import org.activiti.engine.repository.Deployment;
- import org.activiti.engine.repository.ProcessDefinition;
- public class BpmnQuery {
- public static void main(String[] args) throws Exception {
- // 创建流程引擎
- ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
- // 得到流程存储服务对象
- RepositoryService repositoryService = engine.getRepositoryService();
- // 部署一份流程文件
- Deployment dep = repositoryService.createDeployment().addClasspathResource("gen.bpmn").deploy();
- // 查询流程定义
- //查询流程定义实体
- ProcessDefinition def = repositoryService.createProcessDefinitionQuery().deploymentId(dep.getId()).singleResult();
- // 查询资源文件
- InputStream is = repositoryService.getProcessModel(def.getId());
- // 读取输入流
- int count = is.available();
- byte[] contents = new byte[count];
- is.read(contents);
- String result = new String(contents);
- //输入输出结果
- System.out.println(result);
- }
- }
查询流程图参考上面方法addInputStream。
4、删除部署资源
repositoryService.deleteDeployment(String deploymentId, boolean cascade)
(1)不管是否指定级联,都会删除部署相关的身份数据(act_id*)、流程定义数据(act_re_procdef)、流程资源(act_ge_bytearray)与部署数据(act_re_deployment)。
(2)如果设置为级联删除,则会将运行的流程实例、流程任务以及流程实例的历史数据删除。
(3)如果不级联删除,但是存在运行时数据,例如还有流程实例(act_ru*),就会删除失败。
【Activiti学习之二】Activiti API(一)的更多相关文章
- ZooKeeper学习笔记二:API基本使用
Grey ZooKeeper学习笔记二:API基本使用 准备工作 搭建一个zk集群,参考ZooKeeper学习笔记一:集群搭建. 确保项目可以访问集群的每个节点 新建一个基于jdk1.8的maven项 ...
- activiti学习笔记二
上一篇文章大概讲了下什么是流程引擎,为什么我们要用流程引擎,他的基本原理是啥,以及怎么进行基本的使用,这篇文章我们再讲下其他的一些使用. 删除流程部署 package activiti02; impo ...
- Activiti 学习(二)—— Activiti 流程定义和部署
概述 在这一节,我们将创建一个 Activit 工作流,并启动这个流程,主要包含以下几个步骤: 定义流程,按照 BPMN 的规范,使用流程定义工具,用流程符号把整个流程描述出来 部署流程,把画好的流程 ...
- 【Activiti学习之三】Activiti API(二)
环境 JDK 1.8 MySQL 5.6 Tomcat 7 Eclipse-Luna activiti 6.0 一.流程定义1.中止与激活流程定义 package com.wjy.pd; import ...
- Activiti学习(二)数据表结构
Activiti工作流引擎数据库表结构 数据库表的命名 Acitiviti数据库中表的命名都是以ACT_开头的.第二部分是一个两个字符用例表的标识.此用例大体与服务API是匹配的. l ...
- 【Activiti学习之四】Activiti API(三)
环境 JDK 1.8 MySQL 5.6 Tomcat 7 Eclipse-Luna activiti 6.0 一.启动流程 多种方式启动 package com.wjy.pro; import or ...
- Activiti学习记录(二)
1.初始化数据库 使用工作流引擎创建23张表 public class TestActiviti { /** * 使用代码创建工作流需要的23张表 */ @Test public void creat ...
- Activiti学习笔记5 — 常用API解析
常用API解析: 一.ProcessEngineConfiguration 流程引擎配置对象(配置数据库连接4个大配置和建表策略) 二.ProcessEngine 流程引擎核心对象( ...
- 【Activiti学习之一】Activiti入门
环境 JDK1.7 MySQL5.6 Tomcat7 Eclipse-Luna activiti 6.0 一.概念1.工作流(Workflow):是一系列相互衔接.自动进行的业务活动或任务.采用工作流 ...
随机推荐
- kafka原理详解之各种offset和checkpoint
每一个分区都是一个顺序的.不可变的消息队列,并且可以持续的添加.分区中的消息都被分配了一个序列号,称之为偏移量(offset),在每个分区中此偏移量都是唯一的.一个分区在文件系统里存储为一个文件夹.文 ...
- 【05】C#特有的ref、out参数
java和C#非常相似,它们大部分的语法是一样的,但尽管如此,也有一些地方是不同的. 为了更好地学习java或C#,有必要分清它们两者到底在哪里不同. 我们这次要来探讨C#特有的ref.out参数. ...
- C# 常用工具方法之DataTable(一)
1.DataTable 转 泛型T的List /// <summary> /// 数据集DataTable转换成List集合 /// </summary> /// <ty ...
- 《JS权威指南学习总结--第7章 数组概念、稀疏数组》
一.数组概念 数组是值的有序结合.每个值叫做一个元素,而每个元素在数组中都有一个位置,用数字表示,称为索引. JS数组是无类型的:数组元素可以是任意对象,并且同一个数组中的不同元素也可能有不同的类型. ...
- Android 工作流提交审批填写审批意见PopWindow工具类
公司的项目中几乎都会有走工作流这个环节,为了提高效率,现在特意把弹出的填写审批意见PopWindow改转成工具类,提高效率,免得下次又得整.先看运行效果.
- Alpha4
一.站立式会议照片 二.工作进展 (1) 昨天已完成的工作 a. 实现用户登录时获取用户信息功能 b. 实现个人目标列表,允许用户在个人目标界面浏览已设置的目标 c. 继续实现目标广场列表 (2)今天 ...
- 网商微信实名认证FAQ
一.商户实名认证简介 基于监管政策.系统风控.用户体验等方面的考虑,我们需要加强对从业机构特约商户的风险管理,升级对商户客户身份识别的认证标准.因此,新入驻的从业机构特约商户要陆续按照新标准完成商户实 ...
- windows docker 安装 Kitematic
在已经安装好docker for windows的基础上, 右键docker任务栏小图标, 选择Kitematic 然后放在docker的安装目录中C:\Program Files\Docker: 文 ...
- Django使用Mysql已存在数据表的方法
在mysql数据库中已经存在有数据的表,自己又不想删除,下面方法可以同步django中创建的表 1.最好将自己建的表名改为前缀和django自动创建表名前缀相同,不改也可以,但是后期表太多容易混乱 2 ...
- 06. redis cluster
目录 Redis Cluster redis cluster 特点 搭建redis cluster 访问redis cluster redis-cli 访问redis cluster 重新分片数据 新 ...