ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Flowable源码注释(十六)事件日志处理类

2022-02-03 19:59:17  阅读:220  来源: 互联网

标签:engine String Flowable flowable 源码 org import 日志 event


Flowable源码地址:https://github.com/flowable/flowable-engine

包路径:org.flowable.engine.impl.event.logger.handler

EventLoggerEventHandler 事件日志处理接口类

package org.flowable.engine.impl.event.logger.handler;

import java.util.Date;

import org.flowable.common.engine.api.delegate.event.FlowableEvent;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.engine.impl.persistence.entity.EventLogEntryEntity;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @author Joram Barrez
 */
public interface EventLoggerEventHandler {

    // 生成事件日志对象
    EventLogEntryEntity generateEventLogEntry(CommandContext commandContext);

    // 设置事件
    void setEvent(FlowableEvent event);

    // 设置时间戳
    void setTimeStamp(Date timeStamp);

    // 设置映射对象
    void setObjectMapper(ObjectMapper objectMapper);

}

AbstractDatabaseEventLoggerEventHandler 概要数据库事件日志处理类

package org.flowable.engine.impl.event.logger.handler;

import java.util.Date;
import java.util.Map;

import org.flowable.common.engine.api.delegate.event.FlowableEntityEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEvent;
import org.flowable.common.engine.impl.identity.Authentication;
import org.flowable.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.flowable.engine.impl.persistence.entity.EventLogEntryEntity;
import org.flowable.engine.impl.util.CommandContextUtil;
import org.flowable.engine.impl.util.ProcessDefinitionUtil;
import org.flowable.engine.repository.ProcessDefinition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @author Joram Barrez
 */
public abstract class AbstractDatabaseEventLoggerEventHandler implements EventLoggerEventHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(AbstractDatabaseEventLoggerEventHandler.class);

    protected FlowableEvent event;
    protected Date timeStamp;
    protected ObjectMapper objectMapper;

    // 空参构造方法
    public AbstractDatabaseEventLoggerEventHandler() {
    }

    protected EventLogEntryEntity createEventLogEntry(Map<String, Object> data) {
        return createEventLogEntry(null, null, null, null, data);
    }

    protected EventLogEntryEntity createEventLogEntry(String processDefinitionId, String processInstanceId, String executionId, String taskId, Map<String, Object> data) {
        return createEventLogEntry(event.getType().name(), processDefinitionId, processInstanceId, executionId, taskId, data);
    }

    protected EventLogEntryEntity createEventLogEntry(String type, String processDefinitionId, String processInstanceId, String executionId, String taskId, Map<String, Object> data) {

        // 从命令上下文工具类中获取事件日志对象管理器
        EventLogEntryEntity eventLogEntry = CommandContextUtil.getEventLogEntryEntityManager().create();
        // 设置流程定义ID
        eventLogEntry.setProcessDefinitionId(processDefinitionId);
        // 设置流程实例ID
        eventLogEntry.setProcessInstanceId(processInstanceId);
        // 设置执行器ID
        eventLogEntry.setExecutionId(executionId);
        // 设置任务ID
        eventLogEntry.setTaskId(taskId);
        // 设置类型
        eventLogEntry.setType(type);
        eventLogEntry.setTimeStamp(timeStamp);
        putInMapIfNotNull(data, Fields.TIMESTAMP, timeStamp);

        // 当前用户
        String userId = Authentication.getAuthenticatedUserId();
        if (userId != null) {
            // 事件日志对象中写入当前用户ID
            eventLogEntry.setUserId(userId);
            putInMapIfNotNull(data, "userId", userId);
        }

        // 当前租户
        if (!data.containsKey(Fields.TENANT_ID) && processDefinitionId != null) {
            // 根据流程定义ID获取流程定义
            ProcessDefinition processDefinition = ProcessDefinitionUtil.getProcessDefinition(processDefinitionId);
            if (processDefinition != null && !ProcessEngineConfigurationImpl.NO_TENANT_ID.equals(processDefinition.getTenantId())) {
                putInMapIfNotNull(data, Fields.TENANT_ID, processDefinition.getTenantId());
            }
        }

        try {
            eventLogEntry.setData(objectMapper.writeValueAsBytes(data));
        } catch (Exception e) {
            // 无法序列化事件数据。数据不会写入数据库
            LOGGER.warn("Could not serialize event data. Data will not be written to the database", e);
        }

        return eventLogEntry;

    }

    // 重写方法,设置事件
    @Override
    public void setEvent(FlowableEvent event) {
        this.event = event;
    }

    // 重写方法,设置时间戳
    @Override
    public void setTimeStamp(Date timeStamp) {
        this.timeStamp = timeStamp;
    }

    // 重写方法,设置对象映射
    @Override
    public void setObjectMapper(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

    // 辅助器方法 //

    @SuppressWarnings("unchecked")
    // 从事件获取对象
    public <T> T getEntityFromEvent() {
        return (T) ((FlowableEntityEvent) event).getEntity();
    }

    // value非空时,向map中写入新值
    public void putInMapIfNotNull(Map<String, Object> map, String key, Object value) {
        if (value != null) {
            map.put(key, value);
        }
    }

}

标签:engine,String,Flowable,flowable,源码,org,import,日志,event
来源: https://blog.csdn.net/JinYJ2014/article/details/122778219

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有