因公司需求需要将flowable的流程设计器集成到项目中,下面将最近的研究成果记录一下。
文章目录
- 一、下载flowable-modeler源码
- 二、添加相关maven包
- 三、调用idm服务重新接口
- 四、配置类
- 五、启动类跳过登陆拦截
- 六、配置文件
一、下载flowable-modeler源码
- 把flowable-ui-modeler-app\src\main\resources\static下面的代码拷贝至我们自己的工程
二、添加相关maven包
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.7.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.gblfy</groupId><artifactId>flowable</artifactId><version>0.0.1-SNAPSHOT</version><name>flowable_modeler</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><!--去除本身的logback,使用log4j,如果想要使用logback需要添加commons-logging--><exclusions><!-- 去除旧log依赖 --><exclusion><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-logging</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><exclusions><!--去除本身的logback,使用log4j,如果想要使用logback需要添加commons-logging--><exclusion><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-logging</artifactId></exclusion></exclusions><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/commons-io/commons-io --><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.6</version></dependency><dependency><groupId>org.flowable</groupId><artifactId>flowable-groovy-script-static-engine</artifactId><version>6.4.1</version></dependency><dependency><groupId>org.codehaus.groovy</groupId><artifactId>groovy-all</artifactId><version>2.5.4</version><type>pom</type></dependency><!-- https://mvnrepository.com/artifact/commons-io/commons-io --><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.6</version></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.0.0</version></dependency><!-- https://mvnrepository.com/artifact/org.liquibase/liquibase-core --><dependency><groupId>org.liquibase</groupId><artifactId>liquibase-core</artifactId><version>3.6.2</version></dependency><!-- Logging --><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>org.springframework</groupId><artifactId>spring-test</artifactId></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-test --><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.0.9.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/org.flowable/flowable-spring-boot-starter-process --><dependency><groupId>org.flowable</groupId><artifactId>flowable-spring-boot-starter-process</artifactId><version>6.4.1</version></dependency><dependency><groupId>org.flowable</groupId><artifactId>flowable-ui-modeler-rest</artifactId><version>6.4.1</version></dependency><!-- dbpool的jar --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.12</version></dependency><!-- mysql驱动包 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.28</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
三、调用idm服务重新接口
- flowable获取用户是调用idm服务,这里前端修改获取用户信息接口自己实现接口
package com.gblfy.flowable.controller;import org.flowable.idm.api.User;
import org.flowable.idm.engine.impl.persistence.entity.UserEntityImpl;
import org.flowable.ui.common.model.UserRepresentation;
import org.flowable.ui.common.security.SecurityUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;import java.util.ArrayList;
import java.util.List;/*** @ClassName GblfyRemoteAccountResource* @Description 內置用户登录* @Author gblfy* @Date 2019/11/17 20:12*/
@RestController
@RequestMapping("/gblfy")
public class GblfyRemoteAccountResource {/*** GET /rest/account -> get the current user.*/@RequestMapping(value = "/rest/account", method = RequestMethod.GET, produces = "application/json")public UserRepresentation getAccount() {User user=new UserEntityImpl();user.setId("gblfy");SecurityUtils.assumeUser(user);UserRepresentation userRepresentation = new UserRepresentation();userRepresentation.setId("gblfy");userRepresentation.setFirstName("gblfy");List<String> privileges=new ArrayList<>();privileges.add("flowable-idm");privileges.add("flowable-modeler");privileges.add("flowable-task");userRepresentation.setPrivileges(privileges);return userRepresentation;}
}
四、配置类
package com.gblfy.flowable.config;import liquibase.Liquibase;
import liquibase.database.Database;
import liquibase.database.DatabaseConnection;
import liquibase.database.DatabaseFactory;
import liquibase.database.jvm.JdbcConnection;
import liquibase.exception.DatabaseException;
import liquibase.resource.ClassLoaderResourceAccessor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.flowable.common.engine.api.FlowableException;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.flowable.ui.common.service.exception.InternalServerErrorException;
import org.mybatis.spring.SqlSessionFactoryBean;
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.core.io.ResourceLoader;
import org.springframework.core.io.support.ResourcePatternUtils;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.Properties;/*** @ClassName ZhuangzProcessEngine* @Description TODO 构造流程引擎配置类* @Author gblfy* @Date 2019/11/17 20:12*/
@Configuration
public class GblfyProcessEngine {private static final Logger LOGGER = LoggerFactory.getLogger(GblfyProcessEngine.class);//TODO 解决创建流程时报act_re_model找不到protected static final String LIQUIBASE_CHANGELOG_PREFIX = "ACT_DE_";@Autowiredprivate DataSource dataSource;@Autowiredprotected ResourceLoader resourceLoader;//事务管理器@Beanpublic DataSourceTransactionManager dataSourceTransactionManager(DataSource dataSource){DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(dataSource);return dataSourceTransactionManager;}@Beanpublic SpringProcessEngineConfiguration springProcessEngineConfiguration(){SpringProcessEngineConfiguration springProcessEngineConfiguration =new SpringProcessEngineConfiguration();springProcessEngineConfiguration.setDataSource(dataSource);springProcessEngineConfiguration.setDatabaseSchemaUpdate("true");springProcessEngineConfiguration.setTransactionManager(dataSourceTransactionManager(dataSource));return springProcessEngineConfiguration;}@Beanpublic SqlSessionFactory sqlSessionFactory(DataSource dataSource) {SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();sqlSessionFactoryBean.setDataSource(dataSource);String databaseType = initDatabaseType(dataSource);if (databaseType == null) {throw new FlowableException("couldn't deduct database type");}try {Properties properties = new Properties();properties.put("prefix", "");properties.put("blobType", "BLOB");properties.put("boolValue", "TRUE");properties.load(this.getClass().getClassLoader().getResourceAsStream("properties/" + databaseType + ".properties"));sqlSessionFactoryBean.setConfigurationProperties(properties);sqlSessionFactoryBean.setMapperLocations(ResourcePatternUtils.getResourcePatternResolver(resourceLoader).getResources("classpath:/META-INF/modeler-mybatis-mappings/*.xml"));sqlSessionFactoryBean.afterPropertiesSet();return sqlSessionFactoryBean.getObject();} catch (Exception e) {throw new FlowableException("Could not create sqlSessionFactory", e);}}protected String initDatabaseType(DataSource dataSource) {String databaseType = null;Connection connection = null;try {connection = dataSource.getConnection();DatabaseMetaData databaseMetaData = connection.getMetaData();String databaseProductName = databaseMetaData.getDatabaseProductName();LOGGER.info("database product name: '{}'", databaseProductName);databaseType = databaseTypeMappings.getProperty(databaseProductName);if (databaseType == null) {throw new FlowableException("couldn't deduct database type from database product name '" + databaseProductName + "'");}LOGGER.info("using database type: {}", databaseType);} catch (SQLException e) {LOGGER.error("Exception while initializing Database connection", e);} finally {try {if (connection != null) {connection.close();}} catch (SQLException e) {LOGGER.error("Exception while closing the Database connection", e);}}return databaseType;}protected static Properties databaseTypeMappings = getDefaultDatabaseTypeMappings();public static final String DATABASE_TYPE_MYSQL = "mysql";public static final String DATABASE_TYPE_ORACLE = "oracle";public static Properties getDefaultDatabaseTypeMappings() {Properties databaseTypeMappings = new Properties();databaseTypeMappings.setProperty("MySQL", DATABASE_TYPE_MYSQL);databaseTypeMappings.setProperty("Oracle", DATABASE_TYPE_ORACLE);return databaseTypeMappings;}@Beanpublic Liquibase liquibase(DataSource dataSource) {LOGGER.info("Configuring Liquibase");Liquibase liquibase = null;try {DatabaseConnection connection = new JdbcConnection(dataSource.getConnection());Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(connection);//TODO 解决创建流程时报act_re_model找不到database.setDatabaseChangeLogTableName(LIQUIBASE_CHANGELOG_PREFIX + database.getDatabaseChangeLogTableName());database.setDatabaseChangeLogLockTableName(LIQUIBASE_CHANGELOG_PREFIX + database.getDatabaseChangeLogLockTableName());liquibase = new Liquibase("META-INF/liquibase/flowable-modeler-app-db-changelog.xml", new ClassLoaderResourceAccessor(), database);liquibase.update("flowable");return liquibase;} catch (Exception e) {throw new InternalServerErrorException("Error creating liquibase database", e);} finally {closeDatabase(liquibase);}}private void closeDatabase(Liquibase liquibase) {if (liquibase != null) {Database database = liquibase.getDatabase();if (database != null) {try {database.close();} catch (DatabaseException e) {LOGGER.warn("Error closing database", e);}}}}
}
五、启动类跳过登陆拦截
package com.gblfy.flowable;import org.flowable.ui.modeler.properties.FlowableModelerAppProperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;/*** SpringBoot启动类** @Description跳过登陆拦截* @Author gblfy* @Date 2019/11/17 20:12*/
@SpringBootApplication(exclude = {SecurityAutoConfiguration.class})
@ComponentScan(basePackages = {"com.gblfy.flowable","org.flowable.ui.modeler","org.flowable.ui.common"})
public class FlowableApplication {public static void main(String[] args) {SpringApplication.run(FlowableApplication.class, args);}@Beanpublic FlowableModelerAppProperties flowableModelerAppProperties(){FlowableModelerAppProperties flowableModelerAppProperties = new FlowableModelerAppProperties();return flowableModelerAppProperties;}
}
六、配置文件
server:port: 80
spring:datasource:url: jdbc:mysql://localhost:3306/flowable_boot?zeroDateTimeBehavior=convertToNull&useUnicode=true&useSSL=false&rewriteBatchedStatements=truedriver-class-name: com.mysql.jdbc.Driverusername: rootpassword: roottype: com.alibaba.druid.pool.DruidDataSourceflowable:#关闭定时任务JOBasync-executor-activate: false将databaseSchemaUpdate设置为true。当Flowable发现库与数据库表结构不一致时,会自动将数据库表结构升级至新版本。database-schema-update: truecommon:app:idm-url: http://127.0.0.1://80/flowable-idm
项目源码下载:
https://gitee.com/gb_90/flow-modeler-sduty