详解springBoot集成activiti7,工作流实战案例(三)

详解springBoot集成activiti7,工作流实战案例(三)快速使用IDEA搭建SpringBoot项目,集成Activiti7(一)详解springBoot集成activiti7,使用actiBPM绘制流程图(二)过程分析:部署流程 启动一个流程实例 查询当前流程中等待执行的任务 处理任务 流程结束用到activiti的service(ps:记得注入):RepositoryService RuntimeService…

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

Jetbrains全系列IDE稳定放心使用

快速使用IDEA搭建SpringBoot项目,集成Activiti7(一) 

详解springBoot集成activiti7,使用actiBPM绘制流程图(二)

过程分析:

  1. 部署流程
  2. 启动一个流程实例
  3. 查询当前流程中等待执行的任务
  4. 处理任务
  5. 流程结束

用到activiti的service(ps:记得注入):

  1. RepositoryService
  2. RuntimeService
  3. TaskService
    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;

1.部署流程

    /**
     * 部署流程
     */
    public void prepare() {
        // 创建一个部署对象
        Deployment deployment = repositoryService.createDeployment ()
                .name ("请假流程")
                .addClasspathResource ("processes/test_bpmn20.xml")
                .addClasspathResource ("processes/test.png")
                .deploy ();
        System.out.println ("部署ID:" + deployment.getId ());
        System.out.println ("部署名称:" + deployment.getName ());
    }

2.启动一个流程实例

    /**
     * 启动流程实例
     */
    public void startProcess() {
        String processDefinitionKey = "test01";
        Map<String, Object> map = new HashMap<> ();
        
        //使用UEL 表达式设置
        
        // 学生填写申请单    Assignee:${student}
        map.put ("student", "lucy");

        // 班主任审批    Assignee:${teacher}
        map.put ("teacher", "jack");

        ProcessInstance instance = runtimeService.startProcessInstanceByKey (processDefinitionKey, map);
        System.out.println ("流程实例ID:" + instance.getId ());
        System.out.println ("流程定义ID:" + instance.getProcessDefinitionId ());
    }

3.任务查询

    /**
     * 任务查询
     */
    public List<String> searchTask() {
        //流程启动后,各各任务的负责人就可以查询自己当前需要处理的任务,查询出来的任务都是该用户的待办任务。
        List<Task> list = taskService.createTaskQuery ()
                //流程实例key
                .processDefinitionKey ("test01")
                //查询谁的任务
                //.taskAssignee("")
                .list ();
        List<String> idList = new ArrayList<String> ();

        for (Task task : list) {
            idList.add (task.getId ());
            System.out.println ("任务ID:" + task.getId ());
            System.out.println ("任务名称:" + task.getName ());
            System.out.println ("任务的创建时间:" + task.getCreateTime ());
            System.out.println ("任务的办理人:" + task.getAssignee ());
            System.out.println ("流程实例ID:" + task.getProcessInstanceId ());
            System.out.println ("执行对象ID:" + task.getExecutionId ());
            System.out.println ("流程定义ID:" + task.getProcessDefinitionId ());
        }

        return idList;

    }

4.处理任务

    /**
     * 处理任务
     */
    public void disposeTask(List<String> list) {
        for (String id : list) {
            // 任务id
            taskService.complete (id);
            System.out.println ("处理任务id:" + id);
        }
    }

5.测试请假流程

    @GetMapping("/run")
    public void run() {

        // 1.部署流程
        prepare ();

        // 2.启动一个流程实例
         startProcess ();

        // 3.任务查询
         List list = searchTask ();

        // 4.处理任务
         disposeTask (list);
    }

6.浏览器访问127.0.0.1:8080/run

 

出现Caused by: com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: 3 字节的 UTF-8 序列的字节 3 无效。

异常说明字符编码乱码,检查test.bpmn,test_bpmn20.xml文件是否出现乱码

这时候的test.bpmn可以删掉(做个备份吧!),IDEA保存之后就会编码错误,很头疼!

被拦截了,要求登陆,原因是activiti7使用了Security,需要认证

详解springBoot集成activiti7,工作流实战案例(三)

7.Security的配置,直接拿官方的实例,与启动类DemoApplication同级目录下

/*
 * Copyright 2018 Alfresco, Inc. and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.example.activiti.demo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
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;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Set up some users and groups that we can use when interacting with the process engine API.
 * We use the testuser in the process definition so we need to include this user.
 *
 * We also enable Web security so we can build a simple ReST API that uses the Process Engine Java API. We need
 * to be authenticated with a user that has the role ROLE_ACTIVITI_USER to be able to use the API.
 */
@Configuration
@EnableWebSecurity
public class DemoApplicationConfiguration extends WebSecurityConfigurerAdapter {

    private Logger logger = LoggerFactory.getLogger(DemoApplication.class);

    @Override
    @Autowired
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(myUserDetailsService());
    }

    @Bean
    public UserDetailsService myUserDetailsService() {

        InMemoryUserDetailsManager inMemoryUserDetailsManager = new InMemoryUserDetailsManager();

        String[][] usersGroupsAndRoles = {
                {"mbergljung", "1234", "ROLE_ACTIVITI_USER", "GROUP_activitiTraining"},
                {"testuser", "1234", "ROLE_ACTIVITI_USER", "GROUP_activitiTraining"},
                {"system", "1234", "ROLE_ACTIVITI_USER"},
                {"admin", "1234", "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;
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .csrf().disable()
                .authorizeRequests()
                .anyRequest()
                .authenticated()
                .and()
                .httpBasic();
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}

8.继续访问,浏览器填写密码请看 

详解springBoot集成activiti7,工作流实战案例(三)

访问成功控制台输出:

详解springBoot集成activiti7,工作流实战案例(三)

ps:学生填写完成,任务交给班主任,等待班主任审批,班主任审批完成,流程结束。再次执行任务查询即是完成班主任审批任务。 

最后,一个简单的activiti7的实例就完成了

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

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/180127.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • T24银行核心业务系统

    T24银行核心业务系统T24银行核心业务系统http://www.pianshen.com/searchhttp://www.pianshen.com/article/8248107255/

    2022年8月3日
    3
  • NPS 3.0:净推荐值的补充性财务指标 – “赢得性增长率(EGR)”「建议收藏」

    NPS 3.0:净推荐值的补充性财务指标 – “赢得性增长率(EGR)”「建议收藏」Guofu的第33篇文章分享2022年第8篇净推荐值(NPS)的创建者FredReichheld和他的合著者DarciDarnell和MaureenBurn…

    2022年6月5日
    57
  • 前端性能优化学习 02 Web 性能指标「建议收藏」

    前端性能优化学习 02 Web 性能指标「建议收藏」Web性能指标我们已经直到性能的重要性,但当我们讨论性能的时候,让一个网页变得更快,具体指哪些内容?事实上性能是相对的:对于一个用户而言,一个站点可能速度很快(在具有功能强大的设备的快速网络上),而对于另一个用户而言,一个站点可能会较慢(在具有低端设备的慢速网络上)。两个站点可能会在完全相同的时间内加载,但一个站点似乎加载速度会更快(如果它逐步加载内容,而不是等到最后显示所有内容)。一个网站可能加载很快,但在后来的用户交互会很慢。所以在讨论性能的时候,精确的、可量化的指标很重要。但是,一

    2022年9月15日
    0
  • 深度学习基础之代价函数

    深度学习基础之代价函数在机器学习和深度学习中,代价函数非常重要。所以十分有必要弄个清楚代价函数相关的概念和性质。本文介绍了什么是代价函数,然后列举了常用的三种代价函数,并对其中的二次代价函数和交叉熵代价函数进行了比较。

    2022年6月9日
    30
  • DDOS 攻击工具

    DDOS 攻击工具DDOS攻击工具使用github上的DDOS攻击工具https://github.com/Ha3MrX/DDos-Attack将python脚本拷贝到主机,使用chmod+xddos-attack.py增加执行权限然后执行pythonddos-attack.py输入要攻击的IP地址和端口号IP地址和端口号可以kalilinux中的nmap工具探测。转载…

    2022年7月27日
    15
  • 【MIME笔记】

    我最近对互联网的一些技术细节很感兴趣,打算做一系列的笔记。====…

    2022年1月18日
    41

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

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