zoukankan      html  css  js  c++  java
  • spring整合activiti7

    1. spring整合activiti

      1.1 依赖导入   

       <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
      </parent>
         <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <slf4j.version>1.6.6</slf4j.version>
        <log4j.version>1.2.12</log4j.version>
        <java.version>1.8</java.version>
      </properties>   
         <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
          <groupId>mysql</groupId>
    
          <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-test</artifactId>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>org.activiti</groupId>
          <artifactId>activiti-spring-boot-starter</artifactId>
          <version>7.0.0.Beta2</version>
        </dependency>
        <dependency>
          <groupId>org.activiti.cloud</groupId>
          <artifactId>activiti-cloud-services-api</artifactId>
          <version>7.0.0.Beta1</version>
        </dependency>
        <!--activiti常规依赖-->
        <dependency>
          <groupId>org.activiti</groupId>
          <artifactId>activiti-engine</artifactId>
          <version>7.0.0.Beta1</version>
        </dependency>
    
        <dependency>
          <groupId>org.activiti</groupId>
          <artifactId>activiti-bpmn-model</artifactId>
          <version>7.0.0.Beta1</version>
        </dependency>
    
        <dependency>
          <groupId>org.activiti</groupId>
          <artifactId>activiti-bpmn-converter</artifactId>
          <version>7.0.0.Beta1</version>
        </dependency>
    
        <dependency>
          <groupId>org.activiti</groupId>
          <artifactId>activiti-json-converter</artifactId>
          <version>7.0.0.Beta1</version>
        </dependency>
        <!--activiti和Spring整合依赖-->
        <dependency>
          <groupId>org.activiti</groupId>
          <artifactId>activiti-spring</artifactId>
          <version>7.0.0.Beta1</version>
        </dependency>
        <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis</artifactId>
          <version>3.4.5</version>
        </dependency>
        <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>5.1.38</version>
        </dependency>
        <!--单测依赖-->
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.12</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-test</artifactId>
          <version>5.0.7.RELEASE</version>
        </dependency>
        <dependency>
          <groupId>log4j</groupId>
          <artifactId>log4j</artifactId>
          <version>${log4j.version}</version>
        </dependency>
        <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-api</artifactId>
          <version>${slf4j.version}</version>
        </dependency>
        <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-log4j12</artifactId>
          <version>${slf4j.version}</version>
        </dependency>
        <!--数据源-->
        <dependency>
          <groupId>commons-dbcp</groupId>
          <artifactId>commons-dbcp</artifactId>
          <version>1.4</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
        <dependency>
          <groupId>commons-io</groupId>
          <artifactId>commons-io</artifactId>
          <version>2.4</version>
        </dependency>

      1.2 创建配置文件activiti-spring.xml

    <!--数据源-->
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql://localhost:3306/activiti-y2170"/>
            <property name="username" value="root"/>
            <property name="password" value="123"/>
        </bean>
    
        <!--配置ProcessEngineConfiguration-->
        <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
            <!--数据源-->
            <property name="dataSource" ref="dataSource"/>
            <!--配置事务-->
            <property name="transactionManager" ref="transactionManager"/>
            <!--数据生成策略    true   false   create   drop-create -->
            <property name="databaseSchemaUpdate" value="true"/>
        </bean>
    
        <!--配置ProcessEngine-->
        <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
            <property name="processEngineConfiguration" ref="processEngineConfiguration"/>
        </bean>
    
        <!--配置RepositoryService-->
        <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService"/>
        <!--配置RuntimeService-->
        <bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService"/>
        <!--配置TaskService-->
        <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService"/>
        <!--配置HistoryService-->
        <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService"/>
    
        <!--配置事务管理器-->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"/>
        </bean>

      1.3 如果依赖中有log4j依赖需要创建log4j配置文件

    # Set root category priority to INFO and its only appender to CONSOLE.
    #log4j.rootCategory=INFO, CONSOLE            debug   info   warn error fatal
    log4j.rootCategory=debug, CONSOLE, LOGFILE
    
    # Set the enterprise logger category to FATAL and its only appender to CONSOLE.
    log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE
    
    # CONSOLE is set to be a ConsoleAppender using a PatternLayout.
    log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
    log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
    log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m
    
    
    # LOGFILE is set to be a File appender using a PatternLayout.
    log4j.appender.LOGFILE=org.apache.log4j.FileAppender
    log4j.appender.LOGFILE.File=d:axis.log
    log4j.appender.LOGFILE.Append=true
    log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
    log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m
    

      1.4 编写测试代码  部署流程

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("classpath:activiti-spring.xml")
    public class ActivitiSpringTest {
        @Resource
        private RepositoryService repositoryService;
        @Resource
        private RuntimeService runtimeService;
    
        //测试流程部署
        @Test
        public void deployment(){
            repositoryService.createDeployment()
                    .addClasspathResource("medical/medical.bpmn")
                    .name("测试Spring流程部署")
                    .deploy();
        }
    
    }

      1.5 查看数据库

     2. springboot整合activiti

      2.1 导入依赖

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
      </parent>
      
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <java.version>1.8</java.version>
      </properties>
    
    
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
          <groupId>mysql</groupId>
    
          <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-test</artifactId>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>org.activiti</groupId>
          <artifactId>activiti-spring-boot-starter</artifactId>
          <version>7.0.0.Beta2</version>
        </dependency>
        <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis</artifactId>
          <version>3.4.5</version>
        </dependency>
        <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>5.1.38</version>
        </dependency>

      2.2  编写配置文件application.xml

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

      2.3 定义流程

      2.4  添加工具类DemoApplicationConfiguration 

    @Configuration
    @EnableWebSecurity
    public class DemoApplicationConfiguration extends WebSecurityConfigurerAdapter {
    
        private Logger logger = LoggerFactory.getLogger(DemoApplicationConfiguration.class);
    
        @Override
        @Autowired
        public void configure(AuthenticationManagerBuilder auth) throws Exception {
            auth.userDetailsService(myUserDetailsService());
        }
    
        @Bean
        public UserDetailsService myUserDetailsService() {
    
            InMemoryUserDetailsManager inMemoryUserDetailsManager = new InMemoryUserDetailsManager();
    
            String[][] usersGroupsAndRoles = {
                    {"salaboy", "password", "ROLE_ACTIVITI_USER", "GROUP_activitiTeam"},
                    {"ryandawsonuk", "password", "ROLE_ACTIVITI_USER", "GROUP_activitiTeam"},
                    {"erdemedeiros", "password", "ROLE_ACTIVITI_USER", "GROUP_activitiTeam"},
                    {"other", "password", "ROLE_ACTIVITI_USER", "GROUP_otherTeam"},
                    {"admin", "password", "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();
        }
    }

      工具类SecurityUtil 

    @Component
    public class SecurityUtil {
    
        @Autowired
        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);
        }
    }

     

      2.4  编写启动类   当启动类启动时流程自动部署

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

      2.5 编写测试代码  查看流程定义

    @RestController
    @RequestMapping("/activiti")
    public class ActivitiSpringBootTest {
        @Resource
        private ProcessRuntime processRuntime;
        @Resource
        private TaskRuntime taskRuntime;
        @Resource
        private SecurityUtil securityUtil;
        //查看流程定义
        @RequestMapping("/getProcess")
        public void getProcess(){
            //查询所有流程定义信息
            Page<ProcessDefinition> processDefinitionPage = processRuntime.processDefinitions(Pageable.of(0, 10));
            System.out.println("当前流程定义的数量:"+processDefinitionPage.getTotalItems());
            //获取流程信息
            for (ProcessDefinition processDefinition:processDefinitionPage.getContent()) {
                System.out.println("流程定义信息"+processDefinition);
            }
        }

      2.6 访问页面

     

       2.7 查看控制台

       2.8 启动流程实例

     /**
         * 启动流程示例
         */
        @RequestMapping("/startInstance")
        public void startInstance(){
            ProcessInstance instance = processRuntime.start(ProcessPayloadBuilder.start().withProcessDefinitionKey("demo").build());
            System.out.println(instance.getId());
        }

      2.9 访问页面 后查看数据库

       2.10 获取任务 拾取任务并执行

     /**
         * 获取任务,拾取任务,并且执行
         */
        @RequestMapping("/getTask")
        public void getTask(){
            securityUtil.logInAs("salaboy");        //指定组内任务人
            Page<Task> tasks = taskRuntime.tasks(Pageable.of(0, 10));
            if(tasks.getTotalItems()>0){
                for (Task task:tasks.getContent()) {
                    System.out.println("任务名称:"+task.getName());
                    //拾取任务
                    taskRuntime.claim(TaskPayloadBuilder.claim().withTaskId(task.getId()).build());
                    //执行任务
                    taskRuntime.complete(TaskPayloadBuilder.complete().withTaskId(task.getId()).build());
                }
            }
        }
  • 相关阅读:
    如何使用WPF用户界面框架编译EasyPlayPro-Win版本网页无插件视频播放器?
    【解决方案】TSINGSEE青犀视频智慧校园解决方案
    TSINGSEE青犀视频开发rtp推流如何使用ffmpeg配置rtp打包模式?
    TSINGSEE青犀视频部署流媒体服务器如何通过分布式存储突破设备接入及存储瓶颈?
    mysql--存储引擎
    数据库-基本操作
    初识数据库
    mysql---索引
    数据类型--mysql
    mysql--权限问题
  • 原文地址:https://www.cnblogs.com/szhhhh/p/12668206.html
Copyright © 2011-2022 走看看