spring boot activiti工作流_activiti工作流优缺点

spring boot activiti工作流_activiti工作流优缺点SpringBoot集成activiti工作流(模拟请假流程)链接:https://pan.baidu.com/s/10BT_Zertm1WBBrlrdE-QWQ提取码:zsq6学习视频地址见腾讯课堂:https://ke.qq.com/course/459167其他代码都是最原始的测试activiti的api代码,整合springboot的所有代码见下图.1.pom文件<dependency><groupId…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

SpringBoot集成activiti工作流(模拟请假流程)

链接:https://pan.baidu.com/s/10BT_Zertm1WBBrlrdE-QWQ 
提取码:zsq6 

学习视频地址见腾讯课堂 :   【Activiti】工作流从入门到精通【雷哥】-学习视频教程-腾讯课堂

其他代码都是最原始的测试activiti 的api代码,整合springboot的所有代码见下图.
spring boot activiti工作流_activiti工作流优缺点

1.pom文件

        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-spring-boot-starter-basic</artifactId>
            <version>5.22.0</version>
        </dependency>

2.配置文件

#===server 端口号配置===
server.port=8090
#数据源配置
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/activiti?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root

# 使用druid数据源
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#驱动com.mysql.cj.jdbc.Driver
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#连接池初始化数量
spring.datasource.druid.initial-size=5
#最小空闲连接数
spring.datasource.druid.min-idle=10
#最大连接数
spring.datasource.druid.max-active=2000
#获取连接等待超时时间
spring.datasource.druid.max-wait=60000
#间隔多久进行一次检测,检测需要关闭的空闲连接
spring.datasource.druid.time-between-eviction-runs-millis=60000

# Activiti配置
## 自动构建Activiti数据库表,设置value="true",启动时检测,无表创建,有表跳过;设置value="drop-create"
## 每次启动时建新表(Springboot1.x中生效,Springboot2.x中已经不生效,因此关闭)
#spring.activiti.database-schema-update=true
## Activiti自动部署验证设置,:true-开启(默认)、false-关闭
spring.activiti.check-process-definitions=false
## 设置true后将代替那些老的Job executor
spring.activiti.async-executor-enabled=false
## Activiti定时扫描任务,默认:true(开启),不使用的话建议关掉,需要和spring.activiti.async-executor-enabled=false配合使用,否则不起作用
spring.activiti.job-executor-activate=false
#spring.activiti.process-definition-location-prefix=classpath:/processes/
#spring.activiti.process-definition-location-suffixes=**.bpmn

#===mybatis 配置===
# 注意:一定要对应mapper映射xml文件的所在路径
mybatis.mapper-locations=classpath*:mapper/*.xml
# Mybatis SQL语句控制台打印
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
#开启驼峰命名  譬如数据库create_time 自动映射pojo属性createTime
mybatis.configuration.map-underscore-to-camel-case=true

3.启动类排除掉权限SecurityAutoConfiguration类,不然启动失败.报找不着这个类的问题

@SpringBootApplication(exclude = org.activiti.spring.boot.SecurityAutoConfiguration.class)
public class ActivitiDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(ActivitiDemoApplication.class, args);
    }

}

4.启动成功便已经创建好了activiti的表

5.画流程图

spring boot activiti工作流_activiti工作流优缺点

spring boot activiti工作流_activiti工作流优缺点

其他连线依次设置即可

设置办理人,使用监听器监听设置办理人,新建TaskListenerImpl 类实现 TaskListener

public class TaskListenerImpl implements TaskListener {
    @Override
    public void notify(DelegateTask delegateTask) {
        //====流程模拟====
        //1.得到当前用户
        String name="小王";
        //2.取出领导id
        String id="5";
        //3.查询领导信息
        String leader="小李";
        //4.设置办理人
        delegateTask.setAssignee(leader);
    }
}

spring boot activiti工作流_activiti工作流优缺点

画好流程图压缩成zip包待使用,Controller 接口

@RestController("/workFlow")
@Api(tags = "测试activiti",description = "LeaveBillConttroller")
public class LeaveBillConttroller {

    @Autowired
    private LeaveBillService leaveBillService;

    @ApiOperation("查询流程部署")
    @GetMapping(value = "/queryProcessDeploy")
    public List<ActDeployment> queryProcessDeploy(WorkFlowVo workFlowVo){
        return leaveBillService.queryProcessDeploy(workFlowVo);
    }

    @ApiOperation("查询流程定义")
    @GetMapping(value = "/queryAllProcessDefinition")
    public  List<ActProcessDefinition> queryAllProcessDefinition(WorkFlowVo workFlowVo){
        return leaveBillService.queryAllProcessDefinition(workFlowVo);
    }

    @ApiOperation("添加流程部署")
    @PostMapping(value = "/addWorkFlow")
    public Map<String ,Object> addWorkFlow(MultipartFile file ,WorkFlowVo workFlowVo) throws IOException {
        leaveBillService.addWorkFlow(file.getInputStream(),workFlowVo.getDeploymentName());
        return null;
    }

    @ApiOperation("删除流程部署")
    @PostMapping(value = "/deleteWorkFlow")
    public Map<String ,Object> deleteWorkFlow(WorkFlowVo workFlowVo) throws IOException {
        leaveBillService.deleteWorkFlow(workFlowVo.getDeploymentId());
        return null;
    }

    @ApiOperation("流程部署id查看流程图")
    @PostMapping(value = "/viewProcessImage")
    public void viewProcessImage(WorkFlowVo workFlowVo, HttpServletResponse response) throws IOException {
        leaveBillService.viewProcessImage(workFlowVo.getDeploymentId(),response);
    }

    @ApiOperation("提交申请,启动流程")
    @PostMapping(value = "/startProcess")
    public void startProcess(WorkFlowVo workFlowVo) throws IOException {
        leaveBillService.startProcess(workFlowVo);
    }

    @ApiOperation("查询当前登录人的待办任务")
    @GetMapping(value = "/loadCurrentUserTask")
    public List<ActTask> loadCurrentUserTask(WorkFlowVo workFlowVo) throws IOException {
        return leaveBillService.loadCurrentUserTask(workFlowVo);
    }

    @ApiOperation("根据任务id查询业务id和连线")
    @GetMapping(value = "/toDoTask")
    public Map<String ,Object> toDoTask(WorkFlowVo workFlowVo) throws IOException {
        Leavebill leavebill=leaveBillService.toDoTask(workFlowVo.getTaskId());
        List<String> outComeName=leaveBillService.queryOutComes(workFlowVo.getTaskId());

        Map map=new HashMap();
        map.put("leavebill",leavebill);
        map.put("outComeName",outComeName);
        return null;
    }

    @ApiOperation("根据任务id查询批注信息")
    @GetMapping(value = "/loadAllCommentByTaskId")
    public List<ActComment> loadAllCommentByTaskId(WorkFlowVo workFlowVo) throws IOException {
        return leaveBillService.loadAllCommentByTaskId(workFlowVo.getTaskId());
    }

    @ApiOperation("完成任务")
    @GetMapping(value = "/doTask")
    public Map<String ,Object> doTask(WorkFlowVo workFlowVo, HttpServletRequest request) throws IOException {
        return leaveBillService.completeTask(workFlowVo);

    }
}

实现类的方法

@Service
public class LeaveBillService {
@Autowired
private TaskService taskService;
@Autowired
private RuntimeService runtimeService;
@Autowired
private RepositoryService repositoryService;
//查询流程部署
public List<ActDeployment>queryProcessDeploy(WorkFlowVo workFlowVo){
if(workFlowVo.getDeploymentName()==null){
workFlowVo.setDeploymentName("");
}
String name=workFlowVo.getDeploymentName();
long count = repositoryService.
createDeploymentQuery().deploymentNameLike("%"+name+"%").count();
List<Deployment> deployments = repositoryService.createDeploymentQuery().deploymentNameLike("%"+name+"%")
.listPage(0, 10);
List<ActDeployment> actDeployments=new ArrayList<>();
for (Deployment deployment :deployments){
ActDeployment actDeployment=new ActDeployment();
BeanUtils.copyProperties(deployment,actDeployment);
actDeployments.add(actDeployment);
}
return actDeployments;
}
//查询流程定义
public  List<ActProcessDefinition> queryAllProcessDefinition(WorkFlowVo workFlowVo){
if(workFlowVo.getDeploymentName()==null){
workFlowVo.setDeploymentName("");
}
String name=workFlowVo.getDeploymentName();
List<Deployment> deployments =
repositoryService.createDeploymentQuery().deploymentNameLike("%"+name+"%").list();
Set<String> deploymentIds=new HashSet<>();
for (Deployment deployment :deployments){
deploymentIds.add(deployment.getId());
}
List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery()
.deploymentIds(deploymentIds).list();
List<ActProcessDefinition> actProcessDefinitions=new ArrayList<>();
for (ProcessDefinition definition :list){
ActProcessDefinition actProcessDefinition=new ActProcessDefinition();
BeanUtils.copyProperties(definition,actProcessDefinition);
actProcessDefinitions.add(actProcessDefinition);
}
return actProcessDefinitions;
}
//添加流程部署
@Transactional
public void addWorkFlow(InputStream inputStream, String deployMentName) throws IOException {
//流程部署 ,获取流程部署对象
ZipInputStream zipInputStream = new ZipInputStream(inputStream);
Deployment deploy = repositoryService.createDeployment().name(deployMentName)
.addZipInputStream(zipInputStream)
.deploy();
inputStream.close();
zipInputStream.close();
System.err.println("流程部署完成");
}
//删除流程部署
@Transactional
public void deleteWorkFlow(String deploymentId) {
//根据流程部署id删除,如果当前id的流程正在执行,那么会报错
repositoryService.deleteDeployment(deploymentId);
//根据流程部署id删除,删除流程定义,如果当前id流程正在执行,那么会把正在执行的流程数据删除 act_ru_*和act_hi_*表里的数据
//repositoryService.deleteDeployment(deploymentId,true);
System.err.println("删除流程部署完成");
}
//流程部署id查看流程图
public void viewProcessImage(String deploymentId, HttpServletResponse response) throws IOException {
//根据部署id查询流程定义对象
ProcessDefinition definition = repositoryService
.createProcessDefinitionQuery().deploymentId(deploymentId).singleResult();
//从流程定义里面得到图片的名称
String resourceName = definition.getDiagramResourceName();
//使用部署id和图片名称去查询图片流
InputStream inputStream = repositoryService.
getResourceAsStream(deploymentId, resourceName);
//写出图片
File file = new File("C://abc.jpeg");
FileOutputStream fos = new FileOutputStream(file);
byte[] b = new byte[1024];
int length;
while ((length = inputStream.read(b)) != -1) {
fos.write(b, 0, length);
}
inputStream.close();
}
/**
* 提交申请,启动流程
*
*/
@Transactional
public void startProcess(WorkFlowVo workFlowVo) {
//模拟入库提交请假申请,返回请假id
Integer leavebillId = workFlowVo.getId();
//找到流程的key
String processDefinitionKey = Leavebill.class.getSimpleName();
String businessKey=processDefinitionKey+":"+leavebillId;
Map<String,Object> variables=new HashMap<>();
//请假人
variables.put("username",workFlowVo.getUsername());
runtimeService.startProcessInstanceByKey
(processDefinitionKey,businessKey,variables);
System.err.println("提交申请,启动流程成功");
//todo 修改请假状态
}
/**
* 查询当前登录人的待办任务
*/
public List<ActTask> loadCurrentUserTask(WorkFlowVo workFlowVo) {
String username = workFlowVo.getUsername(); //当前登录用户
long count=taskService.createTaskQuery().taskAssignee(username).count();
List<Task> taskList =
taskService.createTaskQuery().taskAssignee(username).listPage(0, 10);
List<ActTask> actTaskList=new ArrayList<>();
for (Task task :taskList){
ActTask actTask=new ActTask();
BeanUtils.copyProperties(task,actTask);
actTaskList.add(actTask);
}
return actTaskList;
}
//根据任务id查询业务信息
public Leavebill toDoTask(String taskId) {
//根据任务id查询任务实例
Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
//任务实例中取出流程实例id
String processInstanceId = task.getProcessInstanceId();
//根据流程实例id查询流程实例
ProcessInstance processInstance = runtimeService.createProcessInstanceQuery()
.processInstanceId(processInstanceId).singleResult();
//取出business_key ,拆分获取业务id,并查询业务信息
String businessKey = processInstance.getBusinessKey();
String id = businessKey.split(":")[1];
Leavebill leavebill=new Leavebill();
leavebill.setId(Integer.valueOf(id));
return leavebill;
}
//根据任务id查询连线信息
public List<String> queryOutComes(String taskId) {
List<String> names=new ArrayList<>();
//1.根据任务id查询任务实例
Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
//2.取出流程定义id
String processDefinitionId = task.getProcessDefinitionId();
//3.取出流程实例id
String processInstanceId = task.getProcessInstanceId();
//4.根据流程实例id查询流程实例
ProcessInstance processInstance = runtimeService.createProcessInstanceQuery()
.processInstanceId(processInstanceId).singleResult();
//5.根据流程定义id查询流程定义的xml信息
ProcessDefinitionEntity processDefinition =
(ProcessDefinitionEntity)repositoryService.getProcessDefinition(processDefinitionId);
//6.从流程实例对象取出当前活动节点id
String activityId = processInstance.getActivityId();
//7.根据活动节点id取出xml和当前活动节点相关数据
ActivityImpl activityImpl = processDefinition.findActivity(activityId);
//8.从activityImpl中取出连线对象
List<PvmTransition> incomingTransitions = activityImpl.getIncomingTransitions(); //进入的线
List<PvmTransition> transitions = activityImpl.getOutgoingTransitions(); //出去的线
if(!CollectionUtils.isEmpty(transitions)){
for (PvmTransition trans : transitions) {
String name = trans.getProperty("name").toString();
names.add(name);
}
}
return names;
}
/**
* 根据任务id查询批注信息
*/
public List<ActComment> loadAllCommentByTaskId(String taskId) {
Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
String processInstanceId = task.getProcessInstanceId();
List<Comment> commentList =
taskService.getProcessInstanceComments(processInstanceId);
List<ActComment> actCommentList=new ArrayList<>();
for (Comment comment :commentList){
ActComment actComment=new ActComment();
BeanUtils.copyProperties(comment,actComment);
actCommentList.add(actComment);
}
return actCommentList;
}
/**
* 完成任务
*/
@Transactional
public Map<String, Object> completeTask(WorkFlowVo workFlowVo) {
String taskId = workFlowVo.getTaskId(); //任务id
String outCome = workFlowVo.getOutCome(); //连接名称
Integer id = workFlowVo.getId(); //请假单id
String comment = workFlowVo.getComment(); //批注信息
String username = workFlowVo.getUsername(); //用户名
//根据任务id查询任务实例
Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
//从任务中取出流程实例id
String processInstanceId = task.getProcessInstanceId();
//设置批注人名 因为批注人使用线程局部变量设置,故批注人必须这样设置
Authentication.setAuthenticatedUserId(username);
//添加批注信息
taskService.addComment(taskId,processInstanceId,comment);
//完成任务
Map<String,Object> variables=new HashMap<>();
variables.put("coutcome",outCome);
taskService.complete(taskId,variables);
//判断任务是否结束
ProcessInstance processInstance = runtimeService.
createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
if(processInstance==null){
Leavebill leavebill=new Leavebill();
leavebill.setId(id);
if(outCome.equals("放弃")){
leavebill.setState(3); //放弃
}else {
leavebill.setState(2); //审批完成
}
//todo 模拟修改数据库请假单状态
}
return null;
}
}

其中,返回的activiti5.22返回的实体是会报错的,所以要把它返回的实体参数再封装一层成自己的返回

spring boot activiti工作流_activiti工作流优缺点

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/188032.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

  • 安装软件提示:必须首先安装.NET Framework

    安装软件提示:必须首先安装.NET Framework安装软件提示:必须首先安装.NET Framework

  • 搭建自己的云计算平台

    1.Enomalism(http://www.enomaly.com/)云计算平台。Enomalism是一个开放源代码项目,它提供了一个功能类似于EC2的云计算框架。Enomalism基于Linux,同时支持Xen和KernelVirtualMachine(KVM)。Enomalism提供了一个基于TurboGearsWeb应用程序框架和Python的软件栈。

  • 基于Pycharm的Django学习1 —— Django三种响应

    基于Pycharm的Django学习1 —— Django三种响应Flask学完啦,那就来学Django吧,学习真的是会上瘾的奥!由于Flask比较简单,而且Django和Flask有很多基础语法也是有相通之处的,所以就不写Flask的博客了,一起学Django吧!基于Pycharm的Django学习Python-WebDjango的三种响应响应文本内容响应html页面响应重定向Python-Web其实在上一篇博客中,讲解Pycharm社区版创建Django项目的时候,已经讲了项目的目录结构,以及每一个文件主要是用来干什么的。但是还没有和前端联动,现在在项

  • 文件读写api函数是什么_c语言文件的读和写

    文件读写api函数是什么_c语言文件的读和写文件操作API函数详解在VC中,大多数情况对文件的操作都使用系统提供的API函数,但有的函数我们不是很熟悉,以下提供一些文件操作API函数介绍:一般文件操作APICreateFile打开文件要对文件进行读写等操作,首先必须获得文件句柄,通过该函数可以获得文件句柄,该函数是通向文件世界的大门。ReadFile从文件中读取字节信息。在打开文件获得了文件句柄之后,则

    2022年10月26日
  • 十大经典排序算法+sort排序

    十大经典排序算法+sort排序本文转自:"十大经典排序算法",其中有动图+代码详解,本文简单介绍+个人理解。排序算法经典的算法问题,也是面试过程中经常被问到的问题。排序算法简单分类如下:这些排序算法的

  • 文本处理工具简略「建议收藏」

    文本处理工具简略「建议收藏」文本处理工具简略

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号