一、导入依赖
<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
camunda.bpm.client.lock-duration=10000
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);
}
};
}
}