flowable 监听器无法获取spring bean ,自动注入bean为null,解决方案

导读:本篇文章讲解 flowable 监听器无法获取spring bean ,自动注入bean为null,解决方案,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

package com.sinochem.it.bpm.flowable.handler;

import com.sinochem.it.bpm.flowable.config.TaskCallbackUrlConfig;
import com.sinochem.it.bpm.flowable.exception.FlowException;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.delegate.TaskListener;
import org.flowable.task.service.delegate.DelegateTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * 多实例任务监听器
 * @author : jiagang
 * @date : Created in 2021/9/14 9:06
 */
@Component
@Slf4j
public class MultiInstanceUserTaskListener implements TaskListener {


    @Autowired
    private TaskCallbackUrlConfig taskCallbackUrlConfig;

    @Autowired
    private RestTemplate restTemplate;


    private static MultiInstanceUserTaskListener myListener;

    /**
     * 监听器中无法获取spring bean配置
     */
    @PostConstruct
    public void init() {
        myListener = this;
        myListener.taskCallbackUrlConfig = this.taskCallbackUrlConfig;
        myListener.restTemplate = this.restTemplate;
    }

    @Override
    public void notify(DelegateTask delegateTask) {
        try {
            String[] strings = delegateTask.getAssignee().split("_");

            Map<String, Object> paramsMap = new HashMap<>();
            paramsMap.put("taskAssignee",delegateTask.getAssignee()); // 审批人code
            paramsMap.put("taskAssigneeName",delegateTask.getAssignee()); // 审批人name
            if (strings.length >= 2){
                paramsMap.put("taskAssignee",strings[0]); // 审批人code
                paramsMap.put("taskAssigneeName",strings[1]); // 审批人name
            }
            paramsMap.put("tenantId",delegateTask.getTenantId());
            paramsMap.put("businessInstanceId",delegateTask.getVariable("businessInstanceId"));
            paramsMap.put("initiatorUserCode", delegateTask.getVariable("userCode")); //发起人用户标识
            paramsMap.put("initiatorUserName", delegateTask.getVariable("userName")); //发起人用户名
            paramsMap.put("taskId", delegateTask.getId());
            paramsMap.put("taskName", delegateTask.getName());
            paramsMap.put("description", delegateTask.getDescription());
            paramsMap.put("wfName", delegateTask.getVariable("wfName"));
            paramsMap.put("billId", delegateTask.getVariable("billId")); //单据id
            paramsMap.put("approveTitle", delegateTask.getVariable("approveTitle")); //送审标题
            paramsMap.put("billType", delegateTask.getVariable("billType"));
            paramsMap.put("approveURL",delegateTask.getVariable("approveURL"));

            paramsMap.put("variables",delegateTask.getVariables());


            HttpHeaders headers = new HttpHeaders();
            headers.add("Content-Type","application/json");
            HttpEntity<Object> entity = new HttpEntity<Object>(paramsMap,headers);

            Map s = myListener.restTemplate.postForObject(myListener.taskCallbackUrlConfig.getAddMultiInstanceUserTaskCallbackUrl(), entity,Map.class);

            if (!s.get("code").equals(0)) {
                throw new FlowException("流程任务事件(多实例会签任务回调)回调API接口异常,异常信息:" + s.get("msg"));
            }

            log.info("流程实例id:" + delegateTask.getProcessInstanceId() + ",任务id:" + delegateTask.getId() + ",审批人:" + delegateTask.getAssignee());

        }catch (Exception e){
            String strMsg = "流程任务事件(多实例会签任务回调)回调API接口"
                    + ",流程实例ID=" + delegateTask.getProcessInstanceId()
                    + ",任务TaskID=" + delegateTask.getId()
                    + ",异常信息:" + e.getMessage();
            log.error(strMsg);
            throw new FlowException(strMsg);
        }

    }
}

注意调用时需要如下方式调用:
在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/99542.html

(1)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!