springboot2.0集成activiti modeler

项目采用Springboot 2.0.3.RELEASE版本以及activiti 5.22.0版本

在acitiviti官网下载完整包https://github.com/Activiti/Activiti/releases/tag/activiti-5.22.0

springboot2.0集成activiti modeler_第1张图片

下载完成后

1、将Activiti-activiti-5.22.0\modules\activiti-webapp-explorer2\src\main\webapp下的diagram-viewer、editor-app以及modeler.html文件放置在项目resources\static文件夹下。

2、将Activiti-activiti-5.22.0\modules\activiti-webapp-explorer2\src\resources下的stencilset.json放置在项目resources\static文件夹下。

3、将Activiti-activiti-5.22.0\modules\activiti-modeler\src\main\java\org\activiti\rest\editor下的main以及model中的java文件放置到项目mian\java目录下

springboot2.0集成activiti modeler_第2张图片

复制完成后,首先将ModelEditoeJsonRestResource.java、ModelSaveRestResource.java、StencilsetRestResource.java上添加 @RequestMapping(value = “/service”)

然后修改resources\static\editor-app\app-cfg.js,如下图

springboot2.0集成activiti modeler_第3张图片

修改ModelSaveRestResource.java,如下(参考https://blog.csdn.net/qq_21770005/article/details/78713912)


     
     
     
     
  1. @RestController
  2. @RequestMapping(value = “/service”)
  3. public class ModelSaveRestResource implements ModelDataJsonConstants {
  4. private static final Logger LOGGER = LogManager.getLogger(ModelSaveRestResource.class);
  5. @Autowired
  6. private RepositoryService repositoryService;
  7. @Autowired
  8. private ObjectMapper objectMapper;
  9. @RequestMapping(value=”/model/{modelId}/save”, method = RequestMethod.PUT)
  10. @ResponseStatus(value = HttpStatus.OK)
  11. public void saveModel(@PathVariable String modelId, @RequestParam(“name”) String name,
  12. @RequestParam(“json_xml”) String json_xml, @RequestParam(“svg_xml”) String svg_xml,
  13. @RequestParam(“description”) String description) {
  14. try {
  15. Model model = repositoryService.getModel(modelId);
  16. ObjectNode modelJson = (ObjectNode) objectMapper.readTree(model.getMetaInfo());
  17. modelJson.put(MODEL_NAME, name);
  18. modelJson.put(MODEL_DESCRIPTION, description);
  19. model.setMetaInfo(modelJson.toString());
  20. model.setName(name);
  21. repositoryService.saveModel(model);
  22. repositoryService.addModelEditorSource(model.getId(), json_xml.getBytes(“utf-8”));
  23. InputStream svgStream = new ByteArrayInputStream(svg_xml.getBytes(“utf-8”));
  24. TranscoderInput input = new TranscoderInput(svgStream);
  25. PNGTranscoder transcoder = new PNGTranscoder();
  26. // Setup output
  27. ByteArrayOutputStream outStream = new ByteArrayOutputStream();
  28. TranscoderOutput output = new TranscoderOutput(outStream);
  29. // Do the transformation
  30. transcoder.transcode(input, output);
  31. final byte[] result = outStream.toByteArray();
  32. repositoryService.addModelEditorSourceExtra(model.getId(), result);
  33. outStream.close();
  34. } catch (Exception e) {
  35. LOGGER.error(“Error saving model”, e);
  36. throw new ActivitiException(“Error saving model”, e);
  37. }
  38. }
  39. }

在pom.xml添加activiti依赖


     
     
     
     
  1. <dependency>
  2. <groupId>org.activiti groupId>
  3. <artifactId>activiti-spring-boot-starter-basic artifactId>
  4. <version>${activiti.version}</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.activiti</groupId>
  8. <artifactId>activiti-spring-boot-starter-actuator</artifactId>
  9. <version>${activiti.version} version>
  10. dependency>
  11. <dependency>
  12. <groupId>org.activiti groupId>
  13. <artifactId>activiti-rest artifactId>
  14. <version>${activiti.version}</version>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.apache.xmlgraphics</groupId>
  18. <artifactId>batik-codec</artifactId>
  19. <version>1.7</version>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.apache.xmlgraphics</groupId>
  23. <artifactId>batik-css</artifactId>
  24. <version>1.7</version>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.apache.xmlgraphics</groupId>
  28. <artifactId>batik-svg-dom</artifactId>
  29. <version>1.7</version>
  30. </dependency>
  31. <dependency>
  32. <groupId>org.apache.xmlgraphics</groupId>
  33. <artifactId>batik-svggen</artifactId>
  34. <version>1.7</version>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.activiti</groupId>
  38. <artifactId>activiti-explorer</artifactId>
  39. <version>${activiti.version} version>
  40. dependency>
  41. <dependency>
  42. <groupId>org.activiti groupId>
  43. <artifactId>activiti-diagram-rest artifactId>
  44. <version>${activiti.version}</version>
  45. </dependency>
  46. <dependency>
  47. <groupId>org.activiti</groupId>
  48. <artifactId>activiti-simple-workflow</artifactId>
  49. <version>${activiti.version} version>
  50. dependency>
  51. <dependency>
  52. <groupId>org.activiti groupId>
  53. <artifactId>activiti-spring artifactId>
  54. <version>${activiti.version} version>
  55. dependency>

在application.yml中添加activiti的数据源,如下


     
     
     
     
  1. spring:
  2. datasource:
  3. activiti:
  4. url: jdbc:mysql://localhost:3306/activity?useUnicode=true&characterEncoding=utf8&useSSL=false
  5. username: root
  6. password: 123
  7. driverClassName: com.mysql.jdbc.Driver
  8. type: com.alibaba.druid.pool.DruidDataSource

添加activiti数据源配置类


     
     
     
     
  1. @Configuration
  2. public class ActitytiDataSourceConfig extends AbstractProcessEngineAutoConfiguration {
  3. private static final Logger log = LogManager.getLogger(ActitytiDataSourceConfig.class);
  4. @Bean(name = "activitiDataSource")
  5. @ConfigurationProperties(prefix = "spring.datasource.activiti")
  6. public DataSource activitiDataSource(){
  7. log.info( "activitiDataSource 初始化...");
  8. return new DruidDataSource();
  9. }
  10. @Bean
  11. public PlatformTransactionManager transactionManager() {
  12. return new DataSourceTransactionManager(activitiDataSource());
  13. }
  14. @Bean
  15. public SpringProcessEngineConfiguration springProcessEngineConfiguration() {
  16. SpringProcessEngineConfiguration configuration = new SpringProcessEngineConfiguration();
  17. configuration.setDataSource(activitiDataSource());
  18. configuration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
  19. configuration.setJobExecutorActivate( true);
  20. configuration.setTransactionManager(transactionManager());
  21. return configuration;
  22. }
  23. }

以上,基本配置已完成。在启动项目后,将自动创建activiti所需要的25张表。

如报错无法启动,在application.yml中添加


     
     
     
     
  1. spring:
  2. activiti:
  3. check-process-definitions: false

成功启动项目后,发现访问项目任何路径都需要输入用户名和密码。这是因为acitivi-rest中集成了spring-security,需要在启动类上添加

@EnableAutoConfiguration(exclude={org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class, org.activiti.spring.boot.SecurityAutoConfiguration.class})
     
     
     
     

访问http://localhost:8080/modeler.html,发现页面未显示内容,这是因为目前还未创建任何model

此时创建ActivitiModelController.java,如下


     
     
     
     
  1. @Controller
  2. public class ActivitiModelController {
  3. private static final Logger log = LogManager.getLogger(ActivitiModelController.class);
  4. @Autowired
  5. ProcessEngine processEngine;
  6. @Autowired
  7. ObjectMapper objectMapper;
  8. /**
  9. * 新建一个空模型
  10. */
  11. @RequestMapping( "/create")
  12. public void newModel(HttpServletResponse response) throws IOException {
  13. RepositoryService repositoryService = processEngine.getRepositoryService();
  14. //初始化一个空模型
  15. Model model = repositoryService.newModel();
  16. //设置一些默认信息
  17. String name = "new-process";
  18. String description = "";
  19. int revision = 1;
  20. String key = "process";
  21. ObjectNode modelNode = objectMapper.createObjectNode();
  22. modelNode.put(ModelDataJsonConstants.MODEL_NAME, name);
  23. modelNode.put(ModelDataJsonConstants.MODEL_DESCRIPTION, description);
  24. modelNode.put(ModelDataJsonConstants.MODEL_REVISION, revision);
  25. model.setName(name);
  26. model.setKey(key);
  27. model.setMetaInfo(modelNode.toString());
  28. repositoryService.saveModel(model);
  29. String id = model.getId();
  30. //完善ModelEditorSource
  31. ObjectNode editorNode = objectMapper.createObjectNode();
  32. editorNode.put( "id", "canvas");
  33. editorNode.put( "resourceId", "canvas");
  34. ObjectNode stencilSetNode = objectMapper.createObjectNode();
  35. stencilSetNode.put( "namespace",
  36. "http://b3mn.org/stencilset/bpmn2.0#");
  37. editorNode.put( "stencilset", stencilSetNode);
  38. repositoryService.addModelEditorSource(id,editorNode.toString().getBytes( "utf-8"));
  39. response.sendRedirect( "/modeler.html?modelId="+id);
  40. }
  41. /**
  42. * 获取所有模型
  43. */
  44. @RequestMapping( "/modelList")
  45. @ResponseBody
  46. public Object modelList(){
  47. RepositoryService repositoryService = processEngine.getRepositoryService();
  48. return repositoryService.createModelQuery().list();
  49. }
  50. /**
  51. * 发布模型为流程定义
  52. */
  53. @RequestMapping( "/deploy")
  54. @ResponseBody
  55. public Object deploy(String modelId) throws Exception {
  56. //获取模型
  57. RepositoryService repositoryService = processEngine.getRepositoryService();
  58. Model modelData = repositoryService.getModel(modelId);
  59. byte[] bytes = repositoryService.getModelEditorSource(modelData.getId());
  60. if (bytes == null) {
  61. return "模型数据为空,请先设计流程并成功保存,再进行发布。";
  62. }
  63. JsonNode modelNode = new ObjectMapper().readTree(bytes);
  64. BpmnModel model = new BpmnJsonConverter().convertToBpmnModel(modelNode);
  65. if(model.getProcesses().size()== 0){
  66. return "数据模型不符要求,请至少设计一条主线流程。";
  67. }
  68. byte[] bpmnBytes = new BpmnXMLConverter().convertToXML(model);
  69. //发布流程
  70. String processName = modelData.getName() + ".bpmn20.xml";
  71. Deployment deployment = repositoryService.createDeployment()
  72. .name(modelData.getName())
  73. .addString(processName, new String(bpmnBytes, "UTF-8"))
  74. .deploy();
  75. modelData.setDeploymentId(deployment.getId());
  76. repositoryService.saveModel(modelData);
  77. return "SUCCESS";
  78. }
  79. /**
  80. * 启动流程
  81. */
  82. @RequestMapping( "/start")
  83. @ResponseBody
  84. public Object startProcess(String keyName) {
  85. ProcessInstance process = processEngine.getRuntimeService().startProcessInstanceByKey(keyName);
  86. return process.getId() + " : " + process.getProcessDefinitionId();
  87. }
  88. /**
  89. * 提交任务
  90. */
  91. @RequestMapping( "/run")
  92. @ResponseBody
  93. public Object run(String processInstanceId) {
  94. Task task = processEngine.getTaskService().createTaskQuery().processInstanceId(processInstanceId).singleResult();
  95. log.info( "task {} find ", task.getId());
  96. processEngine.getTaskService().complete(task.getId());
  97. return "SUCCESS";
  98. }
  99. }

通过访问http://localhost:8080/create创建一个空白的model并跳转到编辑页面

在绘制流程完成后,访问http://localhost:8080/deploy?modelId=1 对该流程进行部署

http://localhost:8080/start?keyName=hello 启动流程

http://localhost:8080/run?processInstanceId=1 提交

至此,springboot集成activiti modeler完成


           from :https://blog.csdn.net/zhengyidi1357/article/details/80858865

参考:https://blog.csdn.net/hj7jay/article/details/50895936

你可能感兴趣的:(java)