栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

21、camunda外部任务

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

21、camunda外部任务

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

依赖
 
    
      org.camunda.bpm.springboot
      camunda-bpm-spring-boot-starter-external-task-client
      7.15.0
    
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 {

  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;


@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 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;


@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/

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/306863.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号