100行代码搞定 activiti

Dynamic Process Creation and Deployment in 100 Lines of Code

We recently released the 5.12 version of Activiti and it’s packed with a lot of new features and improvements. As of the 5.11 version, it’s possible to build BPMN 2.0 processes using a POJO-model. In the latest release, we embrace that POJO-model even more and use it in the core of Activiti as a means of retrieving and deploying process-definitions (on top of the existing deployment formats) using the API.

Combined with other features of Activiti this allows us to build a process, deploy it, start it, test it and retrieve the process definition diagram in under 100 lines of code. By leveraging the new activiti-bpmn-autolayout module, the process elements can be automatically layout, getting the graphical information (BPMN-DI) for free.

The code

I started off with an Activiti unit-test template, which uses a default Activiti-engine running on an in-memory H2 database. The code is written as a simple unit-test, using the built-in JUnit 4 support to have a fully initialized engine and API ready to use when the test starts to run. Full version of the code below can be found on Github.

  1. Using the BPMN-model, we create a simple process containing a start-event, 2 usertaks, an end-event and the nessecairy flows connecting them.
  2. We use the BpmnAutoLayout class, found in the activiti-bpmn-autolayout module, to make sure all processes in the BpmnModel have a graphical representation defined.
  3. Using the new addBpmnModel(…) method on DeploymentBuilder, we make sure out created process gets deployed in the engine.
  4. We start a new instance of our process by using the process-key we defined in our process.
  5. Fetch all waiting tasks for the started process and check if the task’s name and assignee are correct.
  6. To check what the process actually looks like, we save the diagram-image (created based on the BPMN-DI information):Process-definition diagram
  7. Finally, save the BPMN 2.0 xml representation of this process. This allows us to, for example, further refine the process in other modeling tools like Activiti Designer.

Possibilities

For demonstration purposes I created a relatively simple process, but you can imagine the potential if you consider that the POJO-model allows you to use all supported BPMN 2.0 constructs as well as all Activiti-specific extentions.

Using this approach you can create processes at runtime without the need for a design-tool or having to juggle around with XML. It can be used, for example, to create a process-model based on your own “intermediate model” or “building-blocks”, hiding complexity to end-users without sacrificing the richness of the BPMN language.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值