Activiti 是一个自动化工作流框架。它能帮助企业快速搭建分布式、高扩展的工作流框架。

下面这篇文章将会带你探索Activiti 工作流核心运行时API - TaskRuntime API。(P.S. 这篇文章基本上是我对官网文章的翻译,英文好的请看官网原文

TaskRuntime API

下面有写一些demo.这些demo是Activiti官网展示的一些例子,你可以从这个地址下载这些demo。

TaskRunTime API 部分的例子可以在activiti-api-basic-task-example模块中找到。


pom.xml

在Spring Boot2 中使用Activiti添加相关依赖以及数据库驱动就行。

比如在pom.xml文件中添加以下依赖:pom.xml

  1. <dependency>
  2. <groupId>org.activiti</groupId>
  3. <artifactId>activiti-spring-boot-starter</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>com.h2database</groupId>
  7. <artifactId>h2</artifactId>
  8. </dependency>

建议使用下面的BOM

  1. <dependencyManagement>
  2. <dependencies>
  3. <dependency>
  4. <groupId>org.activiti</groupId>
  5. <artifactId>activiti-dependencies</artifactId>
  6. <version>7.1.0.M4</version>
  7. <scope>import</scope>
  8. <type>pom</type>
  9. </dependency>
  10. </dependencies>
  11. </dependencyManagement>

注册TaskRuntime实例

通过下面的方式我们可以将TaskRuntime的实例注册到APP中。

  1. @Autowired
  2. private TaskRuntime taskRuntime;

TaskRuntime接口中定义了一系列方法来帮助我们创建任务实例以及与任务进行交互,源码如下所示。

  1. public interface TaskRuntime {
  2. TaskRuntimeConfiguration configuration();
  3. Task task(String taskId);
  4. Page tasks(Pageable pageable);
  5. Page tasks(Pageable pageable, GetTasksPayload payload);
  6. Task create(CreateTaskPayload payload);
  7. Task claim(ClaimTaskPayload payload);
  8. Task release(ReleaseTaskPayload payload);
  9. Task complete(CompleteTaskPayload payload);
  10. Task update(UpdateTaskPayload payload);
  11. Task delete(DeleteTaskPayload payload);
  12. ...
  13. }

我们可以使用TaskPayloadBuilder参数化任务的信息,来平滑地构建一个TaskRuntime实例:

  1. taskRuntime.create(
  2. TaskPayloadBuilder.create()
  3. .withName("First Team Task")
  4. .withDescription("This is something really important")
  5. .withGroup("activitiTeam")
  6. .withPriority(10)
  7. .build());

上面方式创建的任务只能被 “activitiTeam” 这个分组以及任务拥有者(当前登录用户)看到。


角色与分组

在SpringBoot 工程中,为了安全考虑,角色和分组的创建Activiti依赖Spring Security 模块。在SpringBoot 工程中我们可以使用 UserDetailsService来配置可以与任务进行交互的用户以及他们各自对应的角色和分组。这个demo中是在@Configuration 配置的类中进行设置的.

需要注意的是:与TaskRuntime API 交互,必须拥有 ACTIVITI_USER 角色 (权限是: ROLE_ACTIVITI_USER)。

当与REST端点进行交互的时候,Activiti授权机构将自动设置当前登录用户。但是因为这个demo是一个教学性质的实例,它允许我们手动设置当前登录用户。在实际的工作场景中,千万不要这么做,除非你想不经过REST端点(例如HTTP请求)就改变登录用户。


任务事件监听器

最后一件需要强调的事情就是任务事件监听器的注册。

我们可以按照需要注册任意多个TaskRuntimeEventListeners。当服务触发运行时事件时,监听器能够监听到这一动作,并通知应用程序。

  1. @Bean
  2. public TaskRuntimeEventListener taskAssignedListener() {
  3. return taskAssigned
  4. -> logger.info(
  5. ">>> Task Assigned: '"
  6. + taskAssigned.getEntity().getName()
  7. +"' We can send a notification to the assignee: "
  8. + taskAssigned.getEntity().getAssignee());
  9. }



### DemoApplication 源码
下面是DemoApplication的源码,注释里已经详细地解释了代码的含义。完整代码请查看[官方源码](https://github.com/Activiti/activiti-examples/tree/master/activiti-api-basic-task-example):
```java
package org.activiti.examples;

import org.activiti.api.runtime.shared.query.Page;

import org.activiti.api.runtime.shared.query.Pageable;

import org.activiti.api.task.model.Task;

import org.activiti.api.task.model.builders.TaskPayloadBuilder;

import org.activiti.api.task.runtime.TaskRuntime;

import org.activiti.api.task.runtime.events.TaskAssignedEvent;

import org.activiti.api.task.runtime.events.TaskCompletedEvent;

import org.activiti.api.task.runtime.events.listener.TaskRuntimeEventListener;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.CommandLineRunner;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.context.annotation.Bean;

@SpringBootApplication

public class DemoApplication implements CommandLineRunner {

  1. private Logger logger = LoggerFactory.getLogger(DemoApplication.class);
  2. @Autowired
  3. private TaskRuntime taskRuntime;
  4. @Autowired
  5. private SecurityUtil securityUtil;
  6. public static void main(String[] args) {
  7. SpringApplication.run(DemoApplication.class, args);
  8. }
  9. @Override
  10. public void run(String... args) {
  11. // Using Security Util to simulate a logged in user
  12. securityUtil.logInAs("salaboy");
  13. // Let's create a Group Task (not assigned, all the members of the group can claim it)
  14. // Here 'salaboy' is the owner of the created task
  15. logger.info("> Creating a Group Task for 'activitiTeam'");
  16. taskRuntime.create(TaskPayloadBuilder.create()
  17. .withName("First Team Task")
  18. .withDescription("This is something really important")
  19. .withCandidateGroup("activitiTeam")
  20. .withPriority(10)
  21. .build());
  22. // Let's log in as 'other' user that doesn't belong to the 'activitiTeam' group
  23. securityUtil.logInAs("other");
  24. // Let's get all my tasks (as 'other' user)
  25. logger.info("> Getting all the tasks");
  26. Page<Task> tasks = taskRuntime.tasks(Pageable.of(0, 10));
  27. // No tasks are returned
  28. logger.info("> Other cannot see the task: " + tasks.getTotalItems());
  29. // Now let's switch to a user that belongs to the activitiTeam
  30. securityUtil.logInAs("erdemedeiros");
  31. // Let's get 'erdemedeiros' tasks
  32. logger.info("> Getting all the tasks");
  33. tasks = taskRuntime.tasks(Pageable.of(0, 10));
  34. // 'erdemedeiros' can see and claim the task
  35. logger.info("> erdemedeiros can see the task: " + tasks.getTotalItems());
  36. String availableTaskId = tasks.getContent().get(0).getId();
  37. // Let's claim the task, after the claim, nobody else can see the task and 'erdemedeiros' becomes the assignee
  38. logger.info("> Claiming the task");
  39. taskRuntime.claim(TaskPayloadBuilder.claim().withTaskId(availableTaskId).build());
  40. // Let's complete the task
  41. logger.info("> Completing the task");
  42. taskRuntime.complete(TaskPayloadBuilder.complete().withTaskId(availableTaskId).build());
  43. }
  44. @Bean
  45. public TaskRuntimeEventListener<TaskAssignedEvent> taskAssignedListener() {
  46. return taskAssigned -> logger.info(">>> Task Assigned: '"
  47. + taskAssigned.getEntity().getName() +
  48. "' We can send a notification to the assginee: " + taskAssigned.getEntity().getAssignee());
  49. }
  50. @Bean
  51. public TaskRuntimeEventListener<TaskCompletedEvent> taskCompletedListener() {
  52. return taskCompleted -> logger.info(">>> Task Completed: '"
  53. + taskCompleted.getEntity().getName() +
  54. "' We can send a notification to the owner: " + taskCompleted.getEntity().getOwner());
  55. }

}

  1. 下面是运行结果
  2. > ...
  3. 2019-10-05 16:58:53.900 INFO 6268 --- [ main] o.a.e.DemoApplicationConfiguration : > Registering new user: salaboy with the following Authorities[[ROLE_ACTIVITI_USER, GROUP_activitiTeam]]
  4. 2019-10-05 16:58:54.175 INFO 6268 --- [ main] o.a.e.DemoApplicationConfiguration : > Registering new user: ryandawsonuk with the following Authorities[[ROLE_ACTIVITI_USER, GROUP_activitiTeam]]
  5. 2019-10-05 16:58:54.422 INFO 6268 --- [ main] o.a.e.DemoApplicationConfiguration : > Registering new user: erdemedeiros with the following Authorities[[ROLE_ACTIVITI_USER, GROUP_activitiTeam]]
  6. 2019-10-05 16:58:54.580 INFO 6268 --- [ main] o.a.e.DemoApplicationConfiguration : > Registering new user: other with the following Authorities[[ROLE_ACTIVITI_USER, GROUP_otherTeam]]
  7. 2019-10-05 16:58:54.742 INFO 6268 --- [ main] o.a.e.DemoApplicationConfiguration : > Registering new user: admin with the following Authorities[[ROLE_ACTIVITI_ADMIN]]
  8. ...
  9. 2019-10-05 16:59:00.232 INFO 6268 --- [ main] org.activiti.examples.SecurityUtil : > Logged in as: salaboy
  10. 2019-10-05 16:59:00.233 INFO 6268 --- [ main] org.activiti.examples.DemoApplication : > Creating a Group Task for 'activitiTeam'
  11. 2019-10-05 16:59:00.261 INFO 6268 --- [ main] org.activiti.examples.SecurityUtil : > Logged in as: other
  12. 2019-10-05 16:59:00.261 INFO 6268 --- [ main] org.activiti.examples.DemoApplication : > Getting all the tasks
  13. 2019-10-05 16:59:00.380 INFO 6268 --- [ main] org.activiti.examples.DemoApplication : > Other cannot see the task: 0
  14. 2019-10-05 16:59:00.380 INFO 6268 --- [ main] org.activiti.examples.SecurityUtil : > Logged in as: erdemedeiros
  15. 2019-10-05 16:59:00.380 INFO 6268 --- [ main] org.activiti.examples.DemoApplication : > Getting all the tasks
  16. 2019-10-05 16:59:00.395 INFO 6268 --- [ main] org.activiti.examples.DemoApplication : > erdemedeiros can see the task: 1
  17. 2019-10-05 16:59:00.395 INFO 6268 --- [ main] org.activiti.examples.DemoApplication : > Claiming the task
  18. 2019-10-05 16:59:00.405 INFO 6268 --- [ main] org.activiti.examples.DemoApplication : >>> Task Assigned: 'First Team Task' We can send a notification to the assginee: erdemedeiros
  19. 2019-10-05 16:59:00.425 INFO 6268 --- [ main] org.activiti.examples.DemoApplication : > Completing the task
  20. 2019-10-05 16:59:00.457 INFO 6268 --- [ main] org.activiti.examples.DemoApplication : >>> Task Completed: 'First Team Task' We can send a notification to the owner: salaboy

Activiti(1) - TaskRuntime API 入门的更多相关文章

  1. Web API 入门指南 - 闲话安全

    Web API入门指南有些朋友回复问了些安全方面的问题,安全方面可以写的东西实在太多了,这里尽量围绕着Web API的安全性来展开,介绍一些安全的基本概念,常见安全隐患.相关的防御技巧以及Web AP ...

  2. 转载-Web API 入门

    An Introduction to ASP.NET Web API 目前感觉最好的Web API入门教程 HTTP状态码 Web API 强势入门指南 Install Mongodb Getting ...

  3. Hadoop MapReduce编程 API入门系列之压缩和计数器(三十)

    不多说,直接上代码. Hadoop MapReduce编程 API入门系列之小文件合并(二十九) 生成的结果,作为输入源. 代码 package zhouls.bigdata.myMapReduce. ...

  4. Web API入门指南(安全)转

    安全检测的工具站点:https://www.owasp.org/index.php/Category:Vulnerability_Scanning_Tools Web API入门指南有些朋友回复问了些 ...

  5. 【ASP.NET Web API教程】1 ASP.NET Web API入门

    原文 [ASP.NET Web API教程]1 ASP.NET Web API入门 Getting Started with ASP.NET Web API第1章 ASP.NET Web API入门 ...

  6. Web API 入门指南

    Web API 入门指南 - 闲话安全2013-09-21 18:56 by 微软互联网开发支持, 231 阅读, 3 评论, 收藏, 编辑 Web API入门指南有些朋友回复问了些安全方面的问题,安 ...

  7. 使用Jax-rs 开发RESTfull API 入门

    使用Jax-rs 开发RESTfull API 入门 本文使用 Jersey 2开发RESTfull API.Jersey 2 是 JAX-RS 接口的参考实现 使用到的工具 Eclipse Neon ...

  8. Web API 入门 二 媒体类型

    还是拿上面 那篇 Web API 入门 一  的那个来讲 在product类中加一个时间属性

  9. HBase编程 API入门系列之create(管理端而言)(8)

    大家,若是看过我前期的这篇博客的话,则 HBase编程 API入门系列之put(客户端而言)(1) 就知道,在这篇博文里,我是在HBase Shell里创建HBase表的. 这里,我带领大家,学习更高 ...

随机推荐

  1. JWT原理 使用(入门篇)

    1.JWT简介 JWT:Json Web Token,是基于Json的一个公开规范,这个规范允许我们使用JWT在用户和服务器之间传递安全可靠的信息,他的两大使用场景是:认证和数据交换 使用起来就是,由 ...

  2. HTTP 8中请求方式介绍

    HTTP请求方式中8种请求方法(简单介绍)   简单介绍 HTTP是超文本传输协议,其定义了客户端与服务器端之间文本传输的规范.HTTP默认使用80端口,这个端口指的是服务端的端口,而客户端使用的端口 ...

  3. Fiddle用于移动端抓包

    一.什么情况下可以用到 1.调查参考其他移动端网站的抓包,他们传输方式.如微信上京东的智能机器人的包.移动端的请求接口格式.如何实现的效果等. 2.调试本地移动端页面的测试页面效果是否有问题.如:页面 ...

  4. npm基本命令

    1.npm是什么? npm(Node Package Manager)意思是 node 的包管理器,它是随着 NodeJs 安装时一起被安装的: 无论是在前端还是在前端开发中都会使用到 npm 包管理 ...

  5. java设计模式9.备忘录模式、访问者模式、调停者模式

    备忘录模式 备忘录模式又叫快照模式,备忘录对象是一个用来存储另外一个对象内部状态快照的对象.备忘录的用意是在不破坏封装的条件下,将一个对象的状态捕捉,并外部化存储起来,从而可以在将来合适的时候把这个对 ...

  6. c#中的委托01

    delegate 是表示对具有特定参数列表和返回类型的方法的引用的类型. 在实例化委托时,你可以将其实例与任何具有兼容签名和返回类型的方法相关联. 你可以通过委托实例调用方法. 委托用于将方法作为参数 ...

  7. Linux之acl库的安装与使用(限制Linux某用户的访问权限)

    acl库 作用:限制Linux某用户的访问权限 acl库的安装 首先github中下载acl代码: git clone https://github.com/acl-dev/acl 进入acl, 执行 ...

  8. Android Activity启动耗时统计方案

    作者:林基宗 Activity的启动速度是很多开发者关心的问题,当页面跳转耗时过长时,App就会给人一种非常笨重的感觉.在遇到某个页面启动过慢的时候,开发的第一直觉一般是onCreate执行速度太慢了 ...

  9. 【Offer】[65] 【不用加减乘除做加法】

    题目描述 思路分析 测试用例 Java代码 代码链接 题目描述 写一个函数,求两个整数之和,要求在函数体内不得使用+.-.*./四则运算符号. 牛客网刷题地址 思路分析 对数字做运算,除了四则运算外, ...

  10. Go操作MySQL

    MySQL是常用的关系型数据库,本文介绍了Go语言如何操作MySQL数据库. Go操作MySQL 连接 Go语言中的database/sql包提供了保证SQL或类SQL数据库的泛用接口,并不提供具体的 ...