Camunda——外部任务(spring-starter版)

一、导入依赖

<dependency>
    <groupId>org.camunda.bpm.springboot</groupId>
    <artifactId>camunda-bpm-spring-boot-starter-external-task-client</artifactId>
    <version>7.15.0</version>
</dependency>

二、修改配置文件

# 端口
server.port=8081
# 引擎根路径
camunda.bpm.client.base-url=http://localhost:8080/engine-rest
# 长轮询的时间间隔
camunda.bpm.client.async-response-timeout=20000
# 一次最多拉取任务数量
camunda.bpm.client.max-tasks=1
# 全局定于topic上锁时间,单位毫秒,锁住期间,其他订阅者拉取不了任务
camunda.bpm.client.lock-duration=10000
# 指定工作节点的id
camunda.bpm.client.worker-id=java-client

三、创建流程图,在之前的基础上,添加一个流程

在这里插入图片描述

四、编写代码

package com.gykalc.camundatestjava.camunda.shopping;

import org.camunda.bpm.client.spring.annotation.ExternalTaskSubscription;
import org.camunda.bpm.client.task.ExternalTaskHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

@Configuration
public class SelfRepairService {

    @Bean
    @ExternalTaskSubscription(topicName = "try_self_repair", processDefinitionKeyIn = { "Process_service" }, lockDuration = 50000)
    public ExternalTaskHandler doSelfRepair() {
        return (externalTask, externalTaskService) -> {
            System.out.println("外部任务进入尝试自修");
            try {
                TimeUnit.SECONDS.sleep(1);
            }catch (Exception e) {
                e.printStackTrace();
            }
            boolean isFree = (boolean) externalTask.getVariable("isFree");
            if (isFree) {
                System.out.println("免费维修");
                externalTaskService.handleFailure(externalTask, "维修是免费的,我不想自修了", "这里可以打印异常的堆栈", 0, 5000);
            }else {
                System.out.println("维修是收费的");
                externalTaskService.complete(externalTask);
            }
        };
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值