update - 添加job日志处理器

This commit is contained in:
yangqicheng 2024-05-15 11:07:09 +08:00
parent 02e89eeae9
commit c3e16e4dd4
2 changed files with 88 additions and 1 deletions

View File

@ -12,6 +12,7 @@ import com.google.common.collect.Lists;
import org.flowable.common.engine.api.delegate.event.FlowableEventListener;
import org.flowable.common.engine.impl.history.HistoryLevel;
import org.flowable.form.spring.SpringFormEngineConfiguration;
import org.flowable.job.service.JobProcessor;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.flowable.spring.boot.EngineConfigurationConfigurer;
import org.springframework.beans.factory.ObjectProvider;
@ -19,6 +20,8 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import java.util.List;
import static org.flowable.common.engine.impl.AbstractEngineConfiguration.DB_SCHEMA_UPDATE_TRUE;
/**
@ -35,11 +38,13 @@ public class FlowableConfiguration {
ObjectProvider<FlowableEventListener> listeners,
CustomActivityBehaviorFactory customActivityBehaviorFactory,
StringRedisTemplate stringRedisTemplate,
ExtAxHiTaskInstService extAxHiTaskInstService) {
ExtAxHiTaskInstService extAxHiTaskInstService,
List<JobProcessor> jobProcessors) {
return configuration -> {
configuration.setEnableHistoricTaskLogging(true);
configuration.setHistoryLevel(HistoryLevel.AUDIT);
configuration.setHistory(HistoryLevel.AUDIT.getKey());
configuration.setJobProcessors(jobProcessors);
configuration.setEventListeners(Lists.newArrayList(listeners));
configuration.setActivityBehaviorFactory(customActivityBehaviorFactory);
configuration.setDatabaseSchemaUpdate(DB_SCHEMA_UPDATE_TRUE);

View File

@ -0,0 +1,82 @@
package cn.axzo.workflow.core.engine.job.service;
import cn.azxo.framework.common.constatns.Constants;
import cn.hutool.json.JSONUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.flowable.job.service.JobProcessor;
import org.flowable.job.service.JobProcessorContext;
import org.flowable.job.service.impl.persistence.entity.AbstractJobEntity;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class JobLogProcessor implements JobProcessor {
private static final String X_REQUEST_ID = "x-request-id";
private static final String WRAPPED_PREFIX = "@@wrapped@@";
@Override
public void process(JobProcessorContext jobProcessorContext) {
if (jobProcessorContext.getPhase() == JobProcessorContext.Phase.BEFORE_EXECUTE) {
AbstractJobEntity jobEntity = jobProcessorContext.getJobEntity();
String customValues = jobEntity.getCustomValues();
if (isWrappedCustomValues(customValues)) {
WrappedJobCustomValuesInfo bean = getWrapperCustomValueInfo(customValues);
if (bean != null) {
//原始的值重新设置回去
String oriCustomValues = bean.getCustomValues();
jobEntity.setCustomValues(oriCustomValues);
MDC.put(X_REQUEST_ID, bean.getXRequestId());
MDC.put(Constants.CTX_LOG_ID_MDC, bean.getCtxLogId());
return;
}
}
//不是包装的customValues,MDC删除log的两个字段
MDC.remove(X_REQUEST_ID);
MDC.remove(Constants.CTX_LOG_ID_MDC);
} else if (jobProcessorContext.getPhase() == JobProcessorContext.Phase.BEFORE_CREATE) {//持久化之前做处理
AbstractJobEntity jobEntity = jobProcessorContext.getJobEntity();
String customValues = jobEntity.getCustomValues();
jobEntity.setCustomValues(getWrappedCustomValues(customValues));
}
}
private String getWrappedCustomValues(String originalCustomValues) {
WrappedJobCustomValuesInfo jobLogInfo = new WrappedJobCustomValuesInfo();
String traceId = MDC.get(X_REQUEST_ID);
String cxtLogId = MDC.get(Constants.CTX_LOG_ID_MDC);
jobLogInfo.setCtxLogId(cxtLogId);
jobLogInfo.setXRequestId(traceId);
jobLogInfo.setCustomValues(originalCustomValues);
String jsonStr = JSONUtil.toJsonStr(jobLogInfo);
return WRAPPED_PREFIX + jsonStr;
}
private boolean isWrappedCustomValues(String customValues) {
return customValues != null && !customValues.trim().isEmpty() && customValues.startsWith(WRAPPED_PREFIX);
}
private WrappedJobCustomValuesInfo getWrapperCustomValueInfo(String wrappedCustomValues) {
WrappedJobCustomValuesInfo bean = null;
if (isWrappedCustomValues(wrappedCustomValues)) {
try {
String str = StringUtils.substring(wrappedCustomValues, WRAPPED_PREFIX.length());
bean = JSONUtil.toBean(str, WrappedJobCustomValuesInfo.class);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
return bean;
}
@Data
public static final class WrappedJobCustomValuesInfo {
private String xRequestId;
private String ctxLogId;
private String customValues;
}
}