21、camunda外部任务

外部任务

  • 依赖
  • Spring boot application.yml 配置camunda引擎地址
  • 外部任务流程案例
    • 自定开启订阅配置 Subscriptions
    • 订阅外部任务配置 HandlerConfiguration
    • 外部任务拦截器
  • camunda 外部任务可通过HTTPClient或者Postman调用

依赖

 
    <dependency>
      <groupId>org.camunda.bpm.springbootgroupId>
      <artifactId>camunda-bpm-spring-boot-starter-external-task-clientartifactId>
      <version>7.15.0version>
    dependency>

Spring boot application.yml 配置camunda引擎地址

#camunda 外部任务配置
camunda:
  bpm:
    client:
      base-url: http://localhost:9090/engine-rest
      async-response-timeout: 6000
      worker-id: secondHouseSystem

外部任务流程案例

21、camunda外部任务_第1张图片# Spring boot使用外部任务客户端案例
21、camunda外部任务_第2张图片

自定开启订阅配置 Subscriptions

package com.rhino.camunda;

import org.camunda.bpm.client.spring.SpringTopicSubscription;
import org.camunda.bpm.client.spring.event.SubscriptionInitializedEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class Subscriptions implements ApplicationListener<SubscriptionInitializedEvent> {

  protected static final Logger LOG = LoggerFactory.getLogger(Subscriptions.class);

  @Autowired
  public SpringTopicSubscription invoiceCreatorHandlerSubscription;

  @Autowired
  public SpringTopicSubscription invoiceArchiverHandlerSubscription;

  @PostConstruct
  public void listSubscriptionBeans() {
    LOG.info("Subscription bean 'invoiceCreatorHandlerSubscription' has topic name: {} ",
        invoiceCreatorHandlerSubscription.getTopicName());
    LOG.info("Subscription bean 'invoiceArchiverHandlerSubscription' has topic name: {} ",
        invoiceArchiverHandlerSubscription.getTopicName());
  }

  @Override
  public void onApplicationEvent(SubscriptionInitializedEvent event) {
    SpringTopicSubscription springTopicSubscription = event.getSource();
    String topicName = springTopicSubscription.getTopicName();
    LOG.info("Subscription with topic name '{}' initialized", topicName);

    if (!springTopicSubscription.isOpen()) {
      LOG.info("Subscription with topic name '{}' not yet opened", topicName);

      // do something before subscription is opened

      springTopicSubscription.open();

      LOG.info("Subscription with topic name '{}' opened", topicName);

      springTopicSubscription.close();

      LOG.info("Subscription with topic name '{}' temporarily closed", topicName);

      // do something with subscription temporarily closed

      springTopicSubscription.open();

      LOG.info("Subscription with topic name '{}' reopened again", topicName);
    }
  }

}

订阅外部任务配置 HandlerConfiguration

package com.rhino.camunda;

import org.camunda.bpm.client.spring.annotation.ExternalTaskSubscription;
import org.camunda.bpm.client.task.ExternalTaskHandler;
import org.camunda.bpm.client.variable.ClientValues;
import org.camunda.bpm.engine.variable.value.ObjectValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * Camunda 外部任务,订阅主题
 */
@Configuration
public class HandlerConfiguration {

  protected static final Logger LOG = LoggerFactory.getLogger(HandlerConfiguration.class);

  @Bean
  @ExternalTaskSubscription(value = "External_Work",autoOpen = false)
  public ExternalTaskHandler invoiceCreatorHandler() {
    return (externalTask, externalTaskService) -> {

      String businessKey = externalTask.getBusinessKey();
      // 设置变量
      Map<String, Object> variables = new HashMap<>();
      variables.put("invoiceId", "spring external work");

      // 完成外部任务
      externalTaskService.complete(externalTask, variables);

      LOG.info("The External Task {} has been completed!,businessKey:{}", externalTask.getId(),businessKey);

    };
  }

  @Bean
  @ExternalTaskSubscription(
      topicName = "invoiceArchiver",
      autoOpen = false
  )
  public ExternalTaskHandler invoiceArchiverHandler() {
    return (externalTask, externalTaskService) -> {
		// 执行业务逻辑,完成任务
		// ....
		
      //报告camunda完成外部任务
      externalTaskService.complete(externalTask);
    };
  }

}

外部任务拦截器

外部任务客户端调用 camunda引擎服务接口时,如果camunda引擎接口需要用户认证时,可在拦截器中添加认证信息。这里使用JWT认证

package com.rhino.camunda;

import org.camunda.bpm.client.interceptor.ClientRequestInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Camunda 外部任务的请求拦截器 ,Token验证
 */
@Configuration
public class InterceptorConfiguration {

  protected static Logger LOG = LoggerFactory.getLogger(InterceptorConfiguration.class);

  @Bean
  public ClientRequestInterceptor interceptor() {
    return context -> {
      LOG.info("Request interceptor called!");
      context.addHeader("Authorization", "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ6bGciLCJuYW1lIjoi5byg56uL5Zu9IiwiZXhwIjoxNjMyNzI5MDkyLCJ1c2VySWQiOiI4NzY3MzI2QTAxMjI0OTlFQTMwNzA1QTA5OTg1RjcwQSJ9.XZRdbbksqRwMoqyqRz12mrWuadEB_aMA_f9b6xjKul2dhBZUKEOxmtTbVoOYs7tB9Qi0wSK_AEGUGPG0B7FrgA");
    };
  }

}

camunda 外部任务可通过HTTPClient或者Postman调用

http://camunda-cn.shaochenfeng.com/reference/rest/external-task/21、camunda外部任务_第3张图片

你可能感兴趣的:(Activiti7,与,SpringBoot,spring,spring,boot,java)