一:Java class
Java class实现JavaDelegate接口,只需要配置类的全限定名即可,不需要被Spring容器管理。
public class JavaClassServiceTask implements JavaDelegate {
@Override
public void execute(DelegateExecution execution) throws Exception {
System.out.println("JavaClassServiceTask");
String currentActivityId = execution.getCurrentActivityId();
execution.setVariable(currentActivityId, "ok");
}
}
二:Delegate expression
实现JavaDelegate接口,配置成Bean的名字即可。
@Service
public class DelegateExpressionServiceTask implements JavaDelegate {
@Override
public void execute(DelegateExecution execution) throws Exception {
System.out.println(execution.getVariable("JavaClass"));
System.out.println("DelegateExpressionServiceTask");
}
}
三:Expression
配置成普通的方法调用,可以是任意类的任意方法,方法的返回值会赋值给Result variable变量,该变量可以从下一个节点获取。
@Service
public class ExpressionServiceTask {
public boolean execute(DelegateExecution execution) {
System.out.println("ExpressionServiceTask");
return true;
}
}
四:External
@Configuration
public class CamundaHandlerConfig {
@Bean
@ExternalTaskSubscription(topicName = "java_external_task_topic",
processDefinitionKeyIn = {"ServiceTaskProcess"},
lockDuration = 5000)
public ExternalTaskHandler externalTaskHandler() {
return (ExternalTask externalTask, ExternalTaskService externalTaskService) -> {
boolean expressionResult = (boolean)externalTask.getVariable("expressionResult");
// 处理业务逻辑...
if (!expressionResult) {
externalTaskService.handleFailure(externalTask, "error msg...", "error detail...", 0, 5000);
}
Map<String, Object> variables = new HashMap<>();
variables.put("result", 1);
externalTaskService.complete(externalTask, variables);
};
}
}
# pip3 install camunda-external-task-client-python3
# pip3 install pydantic
from camunda.external_task.external_task import ExternalTask, TaskResult
from camunda.external_task.external_task_worker import ExternalTaskWorker
default_config = {
"maxTasks": 1, # 一次只拉一个任务,这样多实例处理就不会所有任务被一个实例锁住
"lockDuration": 10000, # 锁任务的时间
"asyncResponseTimeout": 30000,
"retries": 0,
"retryTimeout": 5000,
"sleepSeconds": 30000, # 每次拉取的间隔时间
"auth_basic": {"username": "admin", "password": "123456"}
}
def handle_task(task: ExternalTask) -> TaskResult:
result = task.get_variable("result")
if result:
return task.failure(error_message="error_message",
error_details="error_details 如异常堆栈信息",
max_retries=0, retry_timeout=5000)
return task.complete()
if __name__ == '__main__':
worker = ExternalTaskWorker(worker_id="python-client",
base_url="http://localhost:8080/engine-rest",
config=default_config).subscribe("python_external_task_topic", handle_task)
五:运行
- 启动工作流引擎
- 启动Java客户端
- 启动Python客户端
repositoryService.createDeployment()
.name("服务任务流程")
.addClasspathResource("bpmn/service_task.bpmn")
.deploy();
identityService.setAuthenticatedUserId("huihui");
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("ServiceTaskProcess");
System.out.println("processInstance=" + processInstance.getId());
五:连接器
<dependency>
<groupId>org.camunda.bpm</groupId>
<artifactId>camunda-engine-plugin-connect</artifactId>
<version>${camunda.spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.camunda.connect</groupId>
<artifactId>camunda-connect-connectors-all</artifactId>
<version>1.5.6</version>
</dependency>
public class HttpConnectorConfigurator implements ConnectorConfigurator<HttpConnector> {
public Class<HttpConnector> getConnectorClass() {
return HttpConnector.class;
}
public void configure(HttpConnector connector) {
CloseableHttpClient client = HttpClients.custom()
.setMaxConnPerRoute(10)
.setMaxConnTotal(200)
.build();
((AbstractHttpConnector) connector).setHttpClient(client);
}
}
在文件src/main/resources/META-INF/services/org.camunda.connect.spi.ConnectorConfigurator中配置以下完全限制类。
com.example.camunda.config.HttpConnectorConfigurator