服务器之家

服务器之家 > 正文

Activiti7整合Springboot使用记录

时间:2021-11-24 11:51     来源/作者:冰之杍

0.springboot项目创建

通过https://start.spring.io/生成纯净的一个springboot工程

1.引入activiti相关依赖

?
1
2
3
4
5
<dependency>
    <groupid>org.activiti</groupid>
    <artifactid>activiti-spring-boot-starter</artifactid>
    <version>7.1.0.m6</version>
</dependency>

2.启动工程并创建activiti数据库

##activiti7中使用spring security,因此启动工程前,需要加入2个文件支持,2个文件的代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package cn.gzsendi.activitidemotest.config;
 
import java.util.arrays;
import java.util.list;
import java.util.stream.collectors;
 
import org.slf4j.logger;
import org.slf4j.loggerfactory;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
import org.springframework.security.core.authority.simplegrantedauthority;
import org.springframework.security.core.userdetails.user;
import org.springframework.security.core.userdetails.userdetailsservice;
import org.springframework.security.crypto.bcrypt.bcryptpasswordencoder;
import org.springframework.security.crypto.password.passwordencoder;
import org.springframework.security.provisioning.inmemoryuserdetailsmanager;
 
@configuration
public class activiticonfiguration {
    
    private logger logger = loggerfactory.getlogger(activiticonfiguration.class);
    
    @bean(name = "userdetailsservice")
    public userdetailsservice myuserdetailsservice() {
 
        inmemoryuserdetailsmanager inmemoryuserdetailsmanager = new inmemoryuserdetailsmanager();
 
        //用户
        string[][] usersgroupsandroles = {
                {"hefy", "123456", "role_activiti_user"},
                {"liujh", "123456", "role_activiti_admin"},
                {"liuky", "123456", "role_activiti_user"},
                {"admin", "123456", "role_activiti_admin"},
        };
 
        for (string[] user : usersgroupsandroles) {
            list<string> authoritiesstrings = arrays.aslist(arrays.copyofrange(user, 2, user.length));
            logger.info("> registering new user: " + user[0] + " with the following authorities[" + authoritiesstrings + "]");
            inmemoryuserdetailsmanager.createuser(new user(user[0], passwordencoder().encode(user[1]),
                    authoritiesstrings.stream().map(s -> new simplegrantedauthority(s)).collect(collectors.tolist())));
        }
 
        return inmemoryuserdetailsmanager;
 
    }
    
    @bean
    public passwordencoder passwordencoder() {
        return new bcryptpasswordencoder();
    }
 
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package cn.gzsendi.activitidemotest.utils;
 
import java.util.collection;
 
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.beans.factory.annotation.qualifier;
import org.springframework.security.core.authentication;
import org.springframework.security.core.grantedauthority;
import org.springframework.security.core.context.securitycontextholder;
import org.springframework.security.core.context.securitycontextimpl;
import org.springframework.security.core.userdetails.userdetails;
import org.springframework.security.core.userdetails.userdetailsservice;
import org.springframework.stereotype.component;
 
import javax.annotation.resource;
 
@component
public class securityutil {
    
    @autowired
    @qualifier("userdetailsservice")
    private userdetailsservice userdetailsservice;
    
    public void loginas(string username) {
 
        userdetails user = userdetailsservice.loaduserbyusername(username);
        
        if (user == null) {
            throw new illegalstateexception("user " + username + " doesn't exist, please provide a valid user");
        }
 
        securitycontextholder.setcontext(new securitycontextimpl(new authentication() {
            @override
            public collection<? extends grantedauthority> getauthorities() {
                return user.getauthorities();
            }
 
            @override
            public object getcredentials() {
                return user.getpassword();
            }
 
            @override
            public object getdetails() {
                return user;
            }
 
            @override
            public object getprincipal() {
                return user;
            }
 
            @override
            public boolean isauthenticated() {
                return true;
            }
 
            @override
            public void setauthenticated(boolean isauthenticated) throws illegalargumentexception {
 
            }
 
            @override
            public string getname() {
                return user.getusername();
            }
        }));
        
        org.activiti.engine.impl.identity.authentication.setauthenticateduserid(username);
    }
 
}

##加入activiti7的配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
server.port=8080
server.servlet.context-path=/activitidemotest
 
spring.datasource.driver-class-name=com.mysql.cj.jdbc.driver
spring.datasource.url=jdbc:mysql://localhost:3306/activitidemo?usessl=false&useunicode=true&characterencoding=utf8&servertimezone=gmt%2b8&allowmultiqueries=true
spring.datasource.username=root
spring.datasource.password=123456
 
spring.activiti.database-schema-update=true
spring.activiti.db-history-used=true
spring.activiti.history-level=full
spring.activiti.check-process-definitions=false
spring.activiti.deployment-mode=never-fail
spring.activiti.process-definition-location-prefix=classpath:/process/

##启动springboot工程,让系统启动时帮我们建好25张表 2.安装activiti插件(设计器) ##idea

file->settings->plugins,然后找actibpm进行安装。
Activiti7整合Springboot使用记录

##流程图中乱码问题先提前设置防止:

修改idea64.exe.vmoptions文件,在文件中加上如下,然后重启idea

-dfile.encoding=utf-8

##进行流程设计

file->new->bpmnfile
Activiti7整合Springboot使用记录
Activiti7整合Springboot使用记录

设计好后,修改process1.bpmn成process1.xml,然后右键export file导出成process1.jpg,再将process1.bpmn修改成process1.bpmn20.xml,最后将2个文件放在process文件夹
Activiti7整合Springboot使用记录

3.流程部署

使用activiti提供的api把流程定义内容存储起来,activiti执行把流程定义内容存储在数据库中。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package cn.gzsendi.activitidemotest;
 
/**
 * created by jxlhl on 2021/8/18.
 */
 
import org.activiti.engine.repositoryservice;
import org.activiti.engine.repository.deployment;
import org.activiti.engine.repository.deploymentbuilder;
import org.junit.test;
import org.junit.runner.runwith;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.boot.test.context.springboottest;
import org.springframework.test.context.junit4.springrunner;
 
@runwith(springrunner.class)
@springboottest
public class springbootjunittest {
 
    //得到repositoryservice实例
    @autowired
    private repositoryservice repositoryservice;
 
    //0.流程部署,单个文件部署方式
    @test
    public void testdeployment(){
 
        //使用repositoryservice进行部署
        deploymentbuilder builder = repositoryservice.createdeployment();
        builder.addclasspathresource("process/process1.bpmn20.xml");
        builder.addclasspathresource("process/process1.jpg");
        builder.name("first_activiti_process");
        deployment deployment = builder.deploy();
 
        //输出部署信息
        system.out.println("流程部署id:" + deployment.getid());
        system.out.println("流程部署名称:" + deployment.getname());
 
        //流程部署id:125098e1-ffd9-11eb-8847-02004c4f4f50
        //流程部署名称:first_activiti_process
 
    }
 
}

执行此操作后activiti会将上边代码中指定的bpmn20文件和图片文件保存在activiti数据库。

流程定义部署后操作activiti的3张表

Activiti7整合Springboot使用记录
Activiti7整合Springboot使用记录
Activiti7整合Springboot使用记录

4.流程实例启动

启动一个流程实例表示开始一次业务流程的运行

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//1.流程实例启动
  @test
  public void teststartprocess(){
      //根据流程定义id启动流程
      processinstance processinstance = runtimeservice.startprocessinstancebykey("myprocess_1");
 
      //输出实例信息
      system.out.println("流程定义id:" + processinstance.getprocessdefinitionid());
      system.out.println("流程实例id:" + processinstance.getid());
      system.out.println("当前活动id:" + processinstance.getactivityid());
 
      //流程定义id:myprocess_1:1:12702ed4-ffd9-11eb-8847-02004c4f4f50
      //流程实例id:a9b162aa-ffda-11eb-bad1-02004c4f4f50
      //当前活动id:null
 
  }

流程实例启动,将操作以下几个数据库表

?
1
2
3
4
5
6
7
act_hi_actinst 流程实例执行历史
act_hi_identitylink 流程的参与用户历史信息
act_hi_procinst 流程实例历史信息
act_hi_taskinst 流程任务历史信息
act_ru_execution 流程执行信息
act_ru_identitylink 流程的参与用户信息
act_ru_task 任务信息

5.任务查询

流程启动后,任务的负责人就可以查询自己当前需要处理的任务,查询出来的任务都是该用户的待办任务。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//2.任务查询
   //流程启动后,任务的负责人就可以查询自己当前需要处理的任务,查询出来的任务都是该用户的待办任务。
   @test
   public void testfindpersonaltasklist() {
       //任务负责人
       string assignee = "liuky";
 
       //根据流程key 和 任务负责人 查询任务
       list<task> list = taskservice.createtaskquery()
               .processdefinitionkey("myprocess_1")
               .taskassignee(assignee)
               .list();
 
       for (task task : list) {
 
           system.out.println("流程实例id:" + task.getprocessinstanceid());
           system.out.println("任务id:" + task.getid());
           system.out.println("任务负责人:" + task.getassignee());
           system.out.println("任务名称:" + task.getname());
 
       }
 
       //流程实例id:a9b162aa-ffda-11eb-bad1-02004c4f4f50
       //任务id:a9b5815e-ffda-11eb-bad1-02004c4f4f50
       //任务负责人:liuky
       //任务名称:提交申请
 
   }

6. 完成任务

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@test
   public void complettask(){
 
       //根据流程key和任务的负责人查询任务并选择其中的一个任务处理,这里用的
       //是singleresult返回一条,真实环境中是通过步骤5中查询出所有的任务,然后在页面上选择一个任务进行处理.
       task task = taskservice.createtaskquery()
               .processdefinitionkey("myprocess_1") //流程key
               .taskassignee("liuky"//要查询的负责人
               .singleresult();
               
       //完成任务,参数:任务id
       taskservice.complete(task.getid());
 
   }

7.流程结束,或流程流转过程中的历史信息查询

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//流程结束,或流程流转过程中的历史信息查询
 @test
 public void findhistoryinfo(){
 
     //获取 actinst表的查询对象
     historicactivityinstancequery instancequery = historyservice.createhistoricactivityinstancequery();
     //查询 actinst表,条件:根据 instanceid 查询
     instancequery.processinstanceid("fb5b7674-ffde-11eb-91f8-02004c4f4f50");
     //增加排序操作,orderbyhistoricactivityinstancestarttime 根据开始时间排序 asc 升序
     instancequery.orderbyhistoricactivityinstancestarttime().asc();
     //查询所有内容
     list<historicactivityinstance> activityinstancelist = instancequery.list();
     //输出结果
     for (historicactivityinstance hi : activityinstancelist) {
 
         system.out.println("");
         system.out.println("===================-===============");
         system.out.println(hi.getstarttime());
         system.out.println(hi.getassignee());
         system.out.println(hi.getactivityid());
         system.out.println(hi.getactivityname());
         system.out.println(hi.getprocessdefinitionid());
         system.out.println(hi.getprocessinstanceid());
         system.out.println("===================-===============");
         system.out.println("");
 
     }
 }

8.其他api测试

8.1 流程定义信息查询

查询流程相关信息,包含流程定义,流程部署,流程定义版本

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@test
    public void queryprocessdefinition(){
 
        //得到processdefinitionquery对象
        processdefinitionquery processdefinitionquery = repositoryservice.createprocessdefinitionquery();
 
        //查询出当前所有的流程定义
        list<processdefinition> definitionlist = processdefinitionquery.processdefinitionkey("myprocess_1")
                .orderbyprocessdefinitionversion()
                .desc()
                .list();
 
        //打印结果
        for (processdefinition processdefinition : definitionlist) {
            system.out.println("流程定义 id="+processdefinition.getid());
            system.out.println("流程定义 name="+processdefinition.getname());
            system.out.println("流程定义 key="+processdefinition.getkey());
            system.out.println("流程定义 version="+processdefinition.getversion());
            system.out.println("流程部署id ="+processdefinition.getdeploymentid());
        }
 
    }

8.2 删除流程

?
1
2
3
4
5
6
7
8
9
10
11
12
13
//删除流程
  @test
  public void deletedeployment(){
 
      string deploymentid = "125098e1-ffd9-11eb-8847-02004c4f4f50";
 
      //删除流程定义,如果该流程定义已有流程实例启动则删除时出错
      repositoryservice.deletedeployment(deploymentid);
 
      //设置true 级联删除流程定义,即使该流程有流程实例启动也可以删除,设置为false非级别删除方式,如果流程
      //repositoryservice.deletedeployment(deploymentid, true);
 
  }

9.demo源码下载

github: https://github.com/jxlhljh/activitidemotest.git
gitee: https://gitee.com/jxlhljh/activitidemotest.git

到此这篇关于activiti7整合springboot使用记录的文章就介绍到这了,更多相关springboot整合activiti7内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/jxlhljh/article/details/119775872

标签:

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部