本项目是在ruoyi微服务版本的代码上进行集成,其他框架的集成可以参考,单纯为了做个笔记。
这里选择的flowable版本是6.5.0,首先新建模块sky-modules-flowable,然后通过pom添加流程引擎和页面相关的依赖包。
<properties>
<flowable.version>6.5.0</flowable.version>
</properties>
<!-- flowable 流程引擎-->
<dependency>
<groupId>org.flowable</groupId>
<artifactId>flowable-spring-boot-starter</artifactId>
<version>${flowable.version}</version>
</dependency>
<dependency>
<groupId>org.flowable</groupId>
<artifactId>flowable-spring-boot-starter-rest-api</artifactId>
<version>${flowable.version}</version>
</dependency>
<dependency>
<groupId>org.flowable</groupId>
<artifactId>flowable-json-converter</artifactId>
<version>${flowable.version}</version>
<exclusions>
<exclusion>
<groupId>org.flowable</groupId>
<artifactId>flowable-bpmn-model</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- flowable UI集成 -->
<dependency>
<groupId>org.flowable</groupId>
<artifactId>flowable-ui-modeler-conf</artifactId>
<version>${flowable.version}</version>
</dependency>
<dependency>
<groupId>org.flowable</groupId>
<artifactId>flowable-ui-task-conf</artifactId>
<version>${flowable.version}</version>
</dependency>
<dependency>
<groupId>org.flowable</groupId>
<artifactId>flowable-ui-admin-conf</artifactId>
<version>${flowable.version}</version>
</dependency>
<dependency>
<groupId>org.flowable</groupId>
<artifactId>flowable-ui-idm-conf</artifactId>
<version>${flowable.version}</version>
</dependency>
然后在nacos中的 配置文件或者项目中的yml添加flowable相关的配置:
flowable:
#关闭定时任务JOB
async-executor-activate: false
# 检测身份信息表是否存在
db-identity-used: false
database-schema-update: false
在本项目里没有配置ProcessEngine,直接通过配置文件控制flowable的相关信息,如果需要初始化ProcessEngine可以参考如下代码:
@Primary
@Bean(name = "processEngine")
public ProcessEngine initProcessEngine() {
logger.info("=============================ProcessEngineBegin=============================");
// 流程引擎配置 对应配置文件中的数据库配置和flowable配置
ProcessEngineConfiguration cfg = null;
try {
cfg = new StandaloneProcessEngineConfiguration()
.setJdbcUrl(url)
.setJdbcUsername(username)
.setJdbcPassword(password)
.setJdbcDriver(driverClassName) .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE)
.setDatabaseSchema("TEST");
} catch (Exception e) {
e.printStackTrace();
}
// 初始化流程引擎对象
ProcessEngine processEngine = cfg.buildProcessEngine();
logger.info("=============================ProcessEngineEnd=============================");
return processEngine;
}
至此flowable流程引擎就集成完毕了,可以编写流程控制方法,具体可参考下面代码:
@RestController
@RequestMapping("/process")
public class ProcessBasicController {
@Autowired
IProcessActionService processActionService;
@Autowired
RemoteFlowableCallBackService callBackService;
/**
* 清楚所有数据
* @return
*/
@DeleteMapping("clear")
public AjaxResult clearAllProcess(){
processActionService.clear();
return AjaxResult.success("删除成功");
}
/**
* 启动流程
* @param req
* @throws IOException
*/
@PostMapping("task/start")
public R<String> startProcess(@RequestBody FlowReqPara req) throws IOException {
if(StringUtils.isEmpty(req.getCandidates())){
throw new RuntimeException("请指定审核人!");
}
if(StringUtils.isEmpty(req.getDeployId())){
throw new RuntimeException("需要传入流程定义ID");
}
R<String> stringR = processActionService.startProcess(req);
return stringR;
}
/**
* 查询已经部署的模型实例
* @return
*/
@GetMapping("deploy/list")
public R<List<FlowReqPara>> getDeployedModels(){
R<List<FlowReqPara>> listR = processActionService.listDeployedModels();
return listR;
}
/**
* 查询任务列表
* @param userId 用户ID
* @return
*/
@GetMapping("task/list")
public R<List<String>> getTasks(String userId){
R<List<Task>> listR = processActionService.listTask(userId);
List<Task> tasks = listR.getData();
if(StringUtils.isEmpty(tasks)){
return null;
}
ArrayList<String> processDefinitionIds = new ArrayList<>();
tasks.forEach(e -> processDefinitionIds.add(e.getProcessDefinitionId()));
R<List<String>> listTaskId = new R<List<String>>();
listTaskId.setData(processDefinitionIds);
return listTaskId;
}
/**
* 同意
* @param flowReqPara
* @return
*/
@PostMapping("task/approve")
public R<String> taskApprove(@RequestBody FlowReqPara flowReqPara){
if(StringUtils.isEmpty(flowReqPara.getTaskId())){
throw new RuntimeException("请确认已传入流程任务ID");
}
if(StringUtils.isEmpty(flowReqPara.getUserId())){
throw new RuntimeException("用户身份信息获取失败");
}
R<String> approved = processActionService.approved(flowReqPara);
return approved;
}
/**
* 驳回
* @param flowReqPara
* @return
*/
@PostMapping("task/reject")
public R<String> taskReject(@RequestBody FlowReqPara flowReqPara){
if(StringUtils.isEmpty(flowReqPara.getTaskId())){
throw new RuntimeException("请确认已传入流程任务ID");
}
if(StringUtils.isEmpty(flowReqPara.getUserId())){
throw new RuntimeException("用户身份信息获取失败");
}
R<String> reject = processActionService.reject(flowReqPara);
return reject;
}
/**
* 撤销审批
* @param flowReqPara
* @return
*/
@DeleteMapping("task/revo")
public R<String> taskRevocation(FlowReqPara flowReqPara){
if(StringUtils.isEmpty(flowReqPara.getTaskId())){
throw new RuntimeException("需要填入任务ID");
}
return processActionService.delTask(flowReqPara);
}
如果还需要集成Flowable Modeler,那么去官网下载该版本的源码包,然后找到源码包下面modules\flowable-ui-modeler\flowable-ui-modeler-app\src\main\resources\static,在新模块的resource文件夹下建立static文件夹,将源码中static下面所有的文件拷贝到static文件夹里面。
然后为了实现在模块启动时加载Flowable Modeler以及去掉idm权限登录等功能,还需要加载以及修改源码包的部分文件。在模块中新建conf文件夹,将相关配置文件拷贝到该文件夹中,主要包括:
ApplicationConfiguration.java------modules\flowable-ui-modeler\flowable-ui-modeler-conf\src\main\java\org\flowable\ui\modeler\servlet
需要在文件中注释开头的"org.flowable.ui.modeler.conf"、"org.flowable.ui.modeler.security"、"org.flowable.ui.common.conf"、"org.flowable.ui.common.filter","org.flowable.ui.common.security"等与idm相关的配置文件,最终文件如下:
/* 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.sky.flowable.conf;
import org.flowable.ui.modeler.properties.FlowableModelerAppProperties;
import org.flowable.ui.modeler.servlet.ApiDispatcherServletConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(FlowableModelerAppProperties.class)
@ComponentScan(basePackages = {
"org.flowable.ui.modeler.repository",
"org.flowable.ui.modeler.service",
"org.flowable.ui.common.service",
"org.flowable.ui.common.repository",
"org.flowable.ui.common.tenant" })
public class ApplicationConfiguration {
@Bean
public ServletRegistrationBean modelerApiServlet(ApplicationContext applicationContext) {
AnnotationConfigWebApplicationContext dispatcherServletConfiguration = new AnnotationConfigWebApplicationContext();
dispatcherServletConfiguration.setParent(applicationContext);
dispatcherServletConfiguration.register(ApiDispatcherServletConfiguration.class);
DispatcherServlet servlet = new DispatcherServlet(dispatcherServletConfiguration);
ServletRegistrationBean registrationBean = new ServletRegistrationBean(servlet, "/api/*");
registrationBean.setName("Flowable Modeler App API Servlet");
registrationBean.setLoadOnStartup(1);
registrationBean.setAsyncSupported(true);
return registrationBean;
}
}
AppDispatcherServletConfiguration.java------modules\flowable-ui-modeler\flowable-ui-modeler-conf\src\main\java\org\flowable\ui\modeler\servlet
需要移除和idm以及用户相关的文件,最终文件如下:
/* 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.sky.flowable.conf;
import org.flowable.ui.modeler.rest.app.EditorGroupsResource;
import org.flowable.ui.modeler.rest.app.EditorUsersResource;
import org.flowable.ui.modeler.rest.app.StencilSetResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcRegistrations;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
@Configuration
@ComponentScan(value = { "org.flowable.ui.modeler.rest.app"},excludeFilters = {
// 移除 EditorUsersResource 与 EditorGroupsResource,因为不使用 IDM 部分
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = EditorUsersResource.class),
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = EditorGroupsResource.class),
// 配置文件用自己的
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = StencilSetResource.class),})
@EnableAsync
public class AppDispatcherServletConfiguration implements WebMvcRegistrations {
private static final Logger LOGGER = LoggerFactory.getLogger(AppDispatcherServletConfiguration.class);
@Bean
public SessionLocaleResolver localeResolver() {
return new SessionLocaleResolver();
}
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LOGGER.debug("Configuring localeChangeInterceptor");
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("language");
return localeChangeInterceptor;
}
@Override
public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
LOGGER.debug("Creating requestMappingHandlerMapping");
RequestMappingHandlerMapping requestMappingHandlerMapping = new RequestMappingHandlerMapping();
requestMappingHandlerMapping.setUseSuffixPatternMatch(false);
requestMappingHandlerMapping.setRemoveSemicolonContent(false);
Object[] interceptors = { localeChangeInterceptor() };
requestMappingHandlerMapping.setInterceptors(interceptors);
return requestMappingHandlerMapping;
}
}
如果还需要汉化的话那就拷贝StencilSetResource到本地并且重新命名,不然会由于bean同名冲突。
StencilSetResource.java------modules\flowable-ui-modeler\flowable-ui-modeler-rest\src\main\java\org\flowable\ui\modeler\rest\app
同时拷贝汉化文件到本地resource资源文件夹下,在StencilSetResource文件中进行路径修改修改,加载当前项目资源的汉化文件。
@GetMapping(value = "/rest/stencil-sets/editor", produces = "application/json")
public JsonNode getStencilSetForEditor() {
try {
JsonNode stencilNode = objectMapper.readTree(this.getClass().getClassLoader().getResourceAsStream("stencilset/stencilset_bpmn.json"));
return stencilNode;
} catch (Exception e) {
LOGGER.error("Error reading bpmn stencil set json", e);
throw new InternalServerErrorException("Error reading bpmn stencil set json");
}
}
@GetMapping(value = "/rest/stencil-sets/cmmneditor", produces = "application/json")
public JsonNode getCmmnStencilSetForEditor() {
try {
JsonNode stencilNode = objectMapper.readTree(this.getClass().getClassLoader().getResourceAsStream("stencilset/stencilset_cmmn.json"));
return stencilNode;
} catch (Exception e) {
LOGGER.error("Error reading bpmn stencil set json", e);
throw new InternalServerErrorException("Error reading bpmn stencil set json");
}
}
为了实现免登录还需要添加内置用户以及修改调用idm获取用户的接口,首先复写文件
SecurityUtils.java------modules\flowable-ui-common\src\main\java\org\flowable\ui\common\security
需要注意的是要在本地项目中建立相同的路径org.flowable.ui.common.security才能实现复写,在文件中添加设置默认登录的用户。
public static User getCurrentUserObject() {
if (assumeUser != null) {
return assumeUser;
}
RemoteUser user = new RemoteUser();
user.setId("admin");
user.setDisplayName("Administrator");
user.setPassword("123456");
List<String> pris = new ArrayList<>();
pris.add(DefaultPrivileges.ACCESS_MODELER);
pris.add(DefaultPrivileges.ACCESS_IDM);
pris.add(DefaultPrivileges.ACCESS_ADMIN);
pris.add(DefaultPrivileges.ACCESS_TASK);
pris.add(DefaultPrivileges.ACCESS_REST_API);
user.setPrivileges(pris);
return user;
}
然后在前端url-config.js文件中修改前获取用户信息接口同时自定义实现返回默认用户接口:
getAccountUrl: function () {
return FLOWABLE.CONFIG.contextRoot + '/rest/account';
},
@RequestMapping(value = "/rest/account", method = RequestMethod.GET, produces = "application/json")
public UserRepresentation getAccount() {
UserRepresentation userRepresentation = new UserRepresentation();
userRepresentation.setId("admin");
userRepresentation.setEmail("admin@flowable.org");
userRepresentation.setFullName("Administrator");
userRepresentation.setFirstName("Administrator");
List<String> privileges = new ArrayList<>();
privileges.add(DefaultPrivileges.ACCESS_MODELER);
privileges.add(DefaultPrivileges.ACCESS_IDM);
privileges.add(DefaultPrivileges.ACCESS_ADMIN);
privileges.add(DefaultPrivileges.ACCESS_TASK);
privileges.add(DefaultPrivileges.ACCESS_REST_API);
userRepresentation.setPrivileges(privileges);
return userRepresentation;
}
最后修改启动器,加载本地的配置文件。
//启用全局异常拦截器
@Import(value={
// 引入修改的配置
ApplicationConfiguration.class,
AppDispatcherServletConfiguration.class, DatabaseAutoConfiguration.class})
@EnableCustomConfig
@EnableRyFeignClients
@SpringBootApplication(exclude = {FlowableSecurityAutoConfiguration.class,SecurityAutoConfiguration.class, ManagementWebSecurityAutoConfiguration.class,SecurityFilterAutoConfiguration.class})
@EnableTransactionManagement
@EnableCustomSwagger2
public class SkyFlowableApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(SkyFlowableApplication.class, args);
}
}
如果需要适配达梦数据库或其他国产化数据库的可以参考适配达梦