本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2024-11(3)

flowable生成流程图报空指针NPE:org.flowable.image.impl.DefaultProcessDiagramGenerator.initProcessDiagramCanvas

发布于2020-11-19 20:26     阅读(1273)     评论(0)     点赞(14)     收藏(2)


按照flowable的官方文档https://tkjohn.github.io/flowable-userguide/,创建好流程模型后,想把当前的流程图打印出来。但是报以下错误

19:55:16.855 [main] DEBUG org.flowable.common.engine.impl.interceptor.LogInterceptor - --- GetBpmnModelCmd finished --------------------------------------------------------
Exception in thread "main" java.lang.NullPointerException
    at org.flowable.image.impl.DefaultProcessDiagramGenerator.initProcessDiagramCanvas(DefaultProcessDiagramGenerator.java:897)
    at org.flowable.image.impl.DefaultProcessDiagramGenerator.generateProcessDiagram(DefaultProcessDiagramGenerator.java:541)
    at org.flowable.image.impl.DefaultProcessDiagramGenerator.generateDiagram(DefaultProcessDiagramGenerator.java:456)
    at Draw.main(Draw.java:86)

原因是,没有用BpmnAutoLayout自动布局,把图形信息生成。在generateDiagram前加入以下代码

  1. BpmnAutoLayout bpmnAutoLayout = new BpmnAutoLayout(bpmnModel);
  2. bpmnAutoLayout.setTaskHeight(120);
  3. bpmnAutoLayout.setTaskWidth(120);
  4. bpmnAutoLayout.execute();

pom依赖如下:

<dependency>
  <groupId>org.flowable</groupId>
  <artifactId>flowable-bpmn-layout</artifactId>
  <version>6.5.0</version>
</dependency>

完整代码如下:

  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8. import org.apache.commons.io.FileUtils;
  9. import org.flowable.bpmn.BpmnAutoLayout;
  10. import org.flowable.bpmn.model.BpmnModel;
  11. import org.flowable.engine.ProcessEngine;
  12. import org.flowable.engine.ProcessEngineConfiguration;
  13. import org.flowable.engine.RepositoryService;
  14. import org.flowable.engine.RuntimeService;
  15. import org.flowable.engine.history.HistoricActivityInstance;
  16. import org.flowable.engine.history.HistoricProcessInstance;
  17. import org.flowable.engine.impl.cfg.StandaloneProcessEngineConfiguration;
  18. import org.flowable.engine.repository.Deployment;
  19. import org.flowable.engine.runtime.ProcessInstance;
  20. import org.flowable.image.impl.DefaultProcessDiagramGenerator;
  21. public class Draw {
  22. static ProcessEngine processEngine = null;
  23. public static void main(String[] args) throws Exception {
  24. ProcessEngineConfiguration cfg = new StandaloneProcessEngineConfiguration()
  25. .setJdbcUrl("jdbc:mysql://localhost:3306/flowable?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false")
  26. .setJdbcUsername("root")
  27. .setJdbcPassword("")
  28. .setJdbcDriver("com.mysql.jdbc.Driver")
  29. .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
  30. processEngine = cfg.buildProcessEngine();
  31. RepositoryService repositoryService = processEngine.getRepositoryService();
  32. Deployment deployment = repositoryService.createDeployment()
  33. .addClasspathResource("holiday-request.bpmn20.xml")
  34. .deploy();
  35. RuntimeService runtimeService = processEngine.getRuntimeService();
  36. Map<String, Object> variables = new HashMap<String, Object>();
  37. variables.put("employee", "john");
  38. variables.put("nrOfHolidays", 3);
  39. variables.put("description", "home");
  40. ProcessInstance pi =
  41. runtimeService.startProcessInstanceByKey("holidayRequest", variables);
  42. String procInsId = pi.getId();
  43. String procDefId;
  44. ProcessInstance processInstance = runtimeService.createProcessInstanceQuery()
  45. .processInstanceId(procInsId)
  46. .singleResult();
  47. if (processInstance == null) {
  48. HistoricProcessInstance historicProcessInstance = processEngine.getHistoryService().createHistoricProcessInstanceQuery().processInstanceId(procInsId).singleResult();
  49. procDefId = historicProcessInstance.getProcessDefinitionId();
  50. } else {
  51. procDefId = processInstance.getProcessDefinitionId();
  52. }
  53. DefaultProcessDiagramGenerator defaultProcessDiagramGenerator = new DefaultProcessDiagramGenerator(); // 创建默认的流程图生成器
  54. String imageType = "png"; // 生成图片的类型
  55. List<String> highLightedActivities = new ArrayList<>(); // 高亮节点集合
  56. List<String> highLightedFlows = new ArrayList<>(); // 高亮连线集合
  57. List<HistoricActivityInstance> hisActInsList = processEngine.getHistoryService().createHistoricActivityInstanceQuery()
  58. .processInstanceId(procInsId)
  59. .list(); // 查询所有历史节点信息
  60. hisActInsList.forEach(historicActivityInstance -> { // 遍历
  61. if("sequenceFlow".equals(historicActivityInstance.getActivityType())) {
  62. // 添加高亮连线
  63. highLightedFlows.add(historicActivityInstance.getActivityId());
  64. } else {
  65. // 添加高亮节点
  66. highLightedActivities.add(historicActivityInstance.getActivityId());
  67. }
  68. });
  69. String activityFontName = "宋体"; // 节点字体
  70. String labelFontName = "微软雅黑"; // 连线标签字体
  71. String annotationFontName = "宋体"; // 连线标签字体
  72. ClassLoader customClassLoader = null; // 类加载器
  73. double scaleFactor = 1.0d; // 比例因子,默认即可
  74. boolean drawSequenceFlowNameWithNoLabelDI = true; // 不设置连线标签不会画
  75. BpmnModel bpmnModel = repositoryService.getBpmnModel(procDefId);
  76. BpmnAutoLayout bpmnAutoLayout = new BpmnAutoLayout(bpmnModel);
  77. bpmnAutoLayout.setTaskHeight(120);
  78. bpmnAutoLayout.setTaskWidth(120);
  79. bpmnAutoLayout.execute();
  80. InputStream inputStream = defaultProcessDiagramGenerator.generateDiagram(bpmnModel, imageType, highLightedActivities
  81. , highLightedFlows, activityFontName, labelFontName, annotationFontName, customClassLoader,
  82. scaleFactor, drawSequenceFlowNameWithNoLabelDI); // 获取输入流
  83. try {
  84. // 先将图片保存
  85. FileUtils.copyInputStreamToFile(inputStream, new File("E:\\", "1.png"));
  86. } catch (IOException e) {
  87. e.printStackTrace();
  88. }
  89. }
  90. }

 

原文链接:https://blog.csdn.net/lianggx3/article/details/109751448



所属网站分类: 技术文章 > 博客

作者:java战神

链接:http://www.javaheidong.com/blog/article/925/a5ea33d47363e2853d46/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

14 0
收藏该文
已收藏

评论内容:(最多支持255个字符)