Camunda - 参数lockDuration与asyncResponseTimeout

目录

前言

1. external task运行机制

2.lockDuration参数介绍

3.asyncResponseTimeout参数介绍

总结


前言

如果项目中采用external-task的方式与Camunda集成,建议你关注lockDuration和asyncResponseTimeout参数。如果项目中的external-task运行在多实例的微服务中,那么强烈建议你关注lockDuration和asyncResponseTimeout参数,并且合理的设置值。


1. external task运行机制

由于lockDuration是在external task的方式下衍生的参数,所以首先我们要先了解external task的运行机制。

官网里面定义了2种角色

1.Process Engine:流程引擎

2.External Worker:实际完成External Task任务的工作者

具体的执行步骤

步骤1:当流程引擎遇到配置为external task时,会创建一个外部任务实例,并将其添加到外部任务列表中(external task list)。

步骤2:任务实例接收到将要执行的external task topic。External Worker此时可以获取并锁定特定topic的任务(fetchAndLock)。为了防止多个Worker同时获取一个external task,任务具有基于时间戳的锁,该锁在获取任务时设置。只有当锁定到期时,另一个工作人员才能再次获取任务。

步骤3:当External Worker完成了期望的工作时,它可以向Process Engine发出信号,继续执行流程。

官网链接: External Tasks | docs.camunda.org

2.lockDuration参数介绍

在上面external task运行机制里的介绍中,步骤2中提到“为了防止多个Worker同时获取一个external task,任务具有基于时间戳的锁,该锁在获取任务时设置。只有当锁定到期时,另一个工作人员才能再次获取任务。”这个就是lockDuration参数的作用。

官网对于lockDuration参数的定义和描述如下

描述:Specifies for how many milliseconds an External Task is locked. Must be greater than zero. It is overridden by the lock duration configured on a topic subscription

概括来看就是用设置外部任务锁时间的,并且可以给每个订阅的topic设置此值。

默认值: 20000

官网链接: External Task Client Spring Boot Starter | docs.camunda.org

怀揣着好奇,又去camunda-external-task-client.jar的源码中探索答案。这是在topic中给lockDuration设置值的方法。

/**
 * <p>A fluent builder to configure the topic subscription</p>
 *
 * @author Tassilo Weidner
 */
public interface TopicSubscriptionBuilder {

  /**
   * @param lockDuration <ul>
   *                       <li> in milliseconds to lock the external tasks
   *                       <li> must be greater than zero
   *                       <li> the default lock duration is 20 seconds (20,000 milliseconds)
   *                       <li> overrides the lock duration configured on bootstrapping the client
   *                     </ul>
   * @return the builder
   */
  TopicSubscriptionBuilder lockDuration(long lockDuration);

根据注释可以知道:

1.lockDuration参数是用来给external task设置上锁时间的

2.lockDuration值必须大于0

3.lockDuration默认的上锁时间是20秒

4.可以在client上设置lockDuration的值,如下所示,将值设置为30秒

client.subscribe("lock-duration")
                .lockDuration(30000)

3.asyncResponseTimeout参数介绍

官网对于asyncResponseTimeout参数的定义和描述如下

描述:Asynchronous response (long polling) is enabled if a timeout is given. Specifies the maximum waiting time for the response of fetched and locked External Tasks. The response is performed immediately if External Tasks are available at the moment of the request.

默认值: null

针对这种默认值是null的参数,在启用时还是要慎重。

理解下描述的意思:如果设置了超时时间,则启用了异步响应(长轮询)。指定获取和锁定外部任务响应的最长等待时间。如果请求时有外部任务可用,则立即执行响应。

怀揣着好奇,又去camunda-external-task-client.jar的源码中探索答案。这是在ExternalTaskClientBuilder中给asyncResponseTimeout设置值的方法。

  /**
   * Asynchronous response (long polling) is enabled if a timeout is given.
   * Specifies the maximum waiting time for the response of fetched and locked external tasks.
   * The response is performed immediately, if external tasks are available in the moment of the request.
   * This information is optional. Unless a timeout is given, fetch and lock responses are synchronous.
   *
   * @param asyncResponseTimeout of fetched and locked external tasks in milliseconds
   * @return the builder
   */
  ExternalTaskClientBuilder asyncResponseTimeout(long asyncResponseTimeout);

注释中的最后一句“Unless a timeout is given, fetch and lock responses are synchronous.

除非给出超时设置,否则获取和锁定响应是同步的。

理解了一下,如果设置了这个参数,那么很有可能将获取和锁定变成异步操作,那么如果获取了一个任务,但是没有锁定,就有可能被其它客户端同时获取,这种情况会在后面的文章中告诉大家会产生的效果。


总结

本文介绍了Camunda External Task中两个重要的参数lockDuration与asyncResponseTimeout,如果你的项目里面是以external task的方式与Camunda进行集成,请不要忽略这两个参数。之后的文章中会介绍,参数使用不当出现的效果

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhao_rock_2016

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值