21、camunda外部任务

依赖

 <!--Camunda 外部任务-->
    <dependency>
      <groupId>org.camunda.bpm.springboot</groupId>
      <artifactId>camunda-bpm-spring-boot-starter-external-task-client</artifactId>
      <version>7.15.0</version>
    </dependency>

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

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

外部任务流程案例

在这里插入图片描述# Spring boot使用外部任务客户端案例
在这里插入图片描述

自定开启订阅配置 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/在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值