activiti06

Activiti7

简化对工作流的操作,特别在原有 API 的基础上再次进行封闭

ProcessRuntime 接口
TaskRuntime 接口

guide:

https://github.com/Activiti/activiti-7-developers-guide/blob/51a1681c0e4bb5e2f96a6dea73516c9fd53d8521/getting-started/getting-started-activiti-core.md

ProcessRuntime api

 1 public interface ProcessRuntime {
 2   ProcessRuntimeConfiguration configuration();
 3   ProcessDefinition processDefinition(String processDefinitionId);
 4   Page processDefinitions(Pageable pageable);
 5   Page processDefinitions(Pageable pageable,
 6               GetProcessDefinitionsPayload payload);
 7   ProcessInstance start(StartProcessPayload payload);
 8   Page processInstances(Pageable pageable);
 9   Page processInstances(Pageable pageable,
10               GetProcessInstancesPayload payload);
11   ProcessInstance processInstance(String processInstanceId);
12   ProcessInstance suspend(SuspendProcessPayload payload);
13   ProcessInstance resume(ResumeProcessPayload payload);
14   ProcessInstance delete(DeleteProcessPayload payload);
15   void signal(SignalPayload payload);
16   ...
17 }

 

1 Similarly to the TaskRuntime APIs, in order to interact with the ProcessRuntime API the currently logged user is required to have the role “ACTIVITI_USER”.
2 First things first, let’s autowire our ProcessRuntime:
3 https://github.com/Activiti/activiti-examples/blob/master/activiti-api-basic-process-example/src/main/java/org/activiti/examples/DemoApplication.java#L32
4 @Autowired
5 private ProcessRuntime processRuntime;
6 
7 @Autowired
8 private SecurityUtil securityUtil;

 

@Scheduled(initialDelay = 1000, fixedDelay = 1000)
public void processText() {
  securityUtil.logInAs("system");
  String content = pickRandomString();
  SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yy HH:mm:ss");
  logger.info("> Processing content: " + content
                    + " at " + formatter.format(new Date()));
  ProcessInstance processInstance = processRuntime
                  .start(ProcessPayloadBuilder
                       .start()
                       .withProcessDefinitionKey("categorizeProcess")
                       .withProcessInstanceName("Processing Content: " + content)
                       .withVariable("content", content)
                       .build());
  logger.info(">>> Created Process Instance: " + processInstance);
}

 

Activiti7 开发时,只要注入 ProcessRuntime 的实现对象,就可以实
现流程定义信息的操作。 当然这个过程中因为 Activiti7 SpringSecurity 的强耦合,引导我们也必须
SpringSecurity 加入进来

TaskRuntime api

 1 Then you will be able to use the TaskRuntime
 2 @Autowired
 3 private TaskRuntime taskRuntime;
4 Once you have the bean injected into your app you should be able to create tasks and interact with tasks.
5 public interface TaskRuntime { 6 TaskRuntimeConfiguration configuration(); 7 Task task(String taskId); 8 Page tasks(Pageable pageable); 9 Page tasks(Pageable pageable, GetTasksPayload payload); 10 Task create(CreateTaskPayload payload); 11 Task claim(ClaimTaskPayload payload); 12 Task release(ReleaseTaskPayload payload); 13 Task complete(CompleteTaskPayload payload); 14 Task update(UpdateTaskPayload payload); 15 Task delete(DeleteTaskPayload payload); 16 ... 17 }

 

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());

 

Activiti7与springboot整合

1.添加 SpringBoot 整合 Activti7 的坐标


2.添加 SpringSecurity 安全框架的整合配置信息


3.使用 Activti7 新支持的类来实现工作流开发
ProcessRuntime 接口
TaskRuntime 接口


4.使用新的 API 实现工作流开发,主要包括
流程定义查询
启动流程实例
任务的查询
任务的完成

pom.xml

 1 <parent>
 2         <groupId>org.springframework.boot</groupId>
 3         <artifactId>spring-boot-starter-parent</artifactId>
 4         <version>2.1.0.RELEASE</version>
 5         <relativePath/> <!-- lookup parent from repository -->
 6     </parent>
 7 
 8     <properties>
 9         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
10         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
11         <java.version>1.8</java.version>
12     </properties>
13 
14     <dependencies>
15         <dependency>
16             <groupId>org.springframework.boot</groupId>
17             <artifactId>spring-boot-starter-jdbc</artifactId>
18         </dependency>
19 
20         <dependency>
21             <groupId>org.springframework.boot</groupId>
22             <artifactId>spring-boot-starter-web</artifactId>
23         </dependency>
24 
25         <dependency>
26             <groupId>org.springframework.boot</groupId>
27             <artifactId>spring-boot-starter-test</artifactId>
28             <scope>test</scope>
29         </dependency>
30 
31         <!-- https://mvnrepository.com/artifact/org.activiti/activiti-spring-boot-starter -->
32         <dependency>
33             <groupId>org.activiti</groupId>
34             <artifactId>activiti-spring-boot-starter</artifactId>
35             <version>7.0.0.Beta2</version>
36         </dependency>
37 
38         <dependency>
39             <groupId>org.mybatis</groupId>
40             <artifactId>mybatis</artifactId>
41             <version>3.4.5</version>
42         </dependency>
43         <dependency>
44             <groupId>mysql</groupId>
45             <artifactId>mysql-connector-java</artifactId>
46             <version>5.1.27</version>
47         </dependency>
48 
49 
50     </dependencies>
51 
52     <build>
53         <plugins>
54             <plugin>
55                 <groupId>org.springframework.boot</groupId>
56                 <artifactId>spring-boot-maven-plugin</artifactId>
57             </plugin>
58         </plugins>
59     </build>

 

application.yml

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/activiti7?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
    username : root
    password : root
    driver-class-name: com.mysql.jdbc.Driver

Activiti7 SpringBoot 整合后,默认情况下,集成了 SpringSecurity 安全框架

添加SecurityUtil类,DemoApplicationConfig

使用 SpringBoot 整合 Junit 方式测试新特性

 1 @RunWith(SpringRunner.class)
 2 @SpringBootTest
 3 public class ApplicationTest {
 4 
 5     @Autowired
 6     private ProcessRuntime processRuntime;
 7     @Autowired
 8     private TaskRuntime taskRuntime;
 9     @Autowired
10     private SecurityUtil securityUtil;
11 
12 
13 。。。。。。
14 }

 

查看流程定义

 1 /**
 2  * 查看流程定义
 3   */
 4 @Test
 5 public void contextLoads() {
 6     securityUtil.logInAs("salaboy");
 7     Page<ProcessDefinition> processDefinitionPage = processRuntime.processDefinitions(Pageable.of(0, 10));
 8     System.out.println("可用的流程定义数量:" + processDefinitionPage.getTotalItems());
 9     for (ProcessDefinition pd : processDefinitionPage.getContent()) {
10         System.out.println("流程定义:" + pd);
11     }
12 }

 

启动流程实例

 1 /**
 2  * 启动流程实例
 3  */
 4 @Test
 5 public void testStartProcess() {
 6     securityUtil.logInAs("salaboy");
 7     ProcessInstance pi = processRuntime.start(ProcessPayloadBuilder.start()
 8             .withProcessDefinitionKey("myProcess_1")
 9             .build());
10     System.out.println("流程实例ID:" + pi.getId());  //b6a5
11 
12 }

 

查询任务完成

 1 /**
 2      * 查询任务,并完成自己的任务
 3      */
 4     @Test
 5     public void testTask() {
 6         securityUtil.logInAs("erdemedeiros");
 7         Page<Task> taskPage=taskRuntime.tasks(Pageable.of(0,10));
 8         if (taskPage.getTotalItems()>0){
 9             for (Task task:taskPage.getContent()){
10                 taskRuntime.claim(TaskPayloadBuilder
11                         .claim()
12                         .withTaskId(task.getId())
13                         .build());
14                 System.out.println("任务1:"+task);
15                 taskRuntime.complete(TaskPayloadBuilder.complete()
16                         .withTaskId(task.getId())
17                         .build());
18             }
19         }
20         Page<Task> taskPage2=taskRuntime.tasks(Pageable.of(0,10));
21         if (taskPage2.getTotalItems()>0){
22             System.out.println("任务2:"+taskPage2.getContent());
23         }
24     }

使用 TaskRuntime 接口的 tasks()方法实现任务的查询。
使用 TaskRuntime 接口的 claim()方法实现任务拾取。
使用 TaskRuntime 接口的 complete()方法实现任务的完成

 

 

 

 

 

posted @ 2019-08-13 21:27  曲阳阳  阅读(377)  评论(0编辑  收藏  举报