Add method for TraceContextUtil (#777)

* add method for TraceContextUtil

* modify something

* delete blank line
pull/781/head
maxisvest 2 years ago committed by GitHub
parent 7729ceab69
commit 3f8dfb4c31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -23,7 +23,7 @@ import cn.hippo4j.core.executor.manage.GlobalNotifyAlarmManage;
import cn.hippo4j.core.executor.manage.GlobalThreadPoolManage;
import cn.hippo4j.core.executor.support.ThreadPoolBuilder;
import cn.hippo4j.core.toolkit.IdentifyUtil;
import cn.hippo4j.core.toolkit.TraceContextUtil;
import cn.hippo4j.core.toolkit.ExecutorTraceContextUtil;
import cn.hippo4j.message.service.Hippo4jSendMessageService;
import cn.hippo4j.message.enums.NotifyTypeEnum;
import cn.hippo4j.message.service.ThreadPoolNotifyAlarm;
@ -178,7 +178,7 @@ public class ThreadPoolNotifyAlarmHandler implements Runnable, CommandLineRunner
alarmNotifyRequest.setThreadPoolId(threadPoolId);
alarmNotifyRequest.setExecuteTime(executeTime);
alarmNotifyRequest.setExecuteTimeOut(executeTimeOut);
String executeTimeoutTrace = TraceContextUtil.getAndRemove();
String executeTimeoutTrace = ExecutorTraceContextUtil.getAndRemoveTimeoutTrace();
if (StringUtil.isNotBlank(executeTimeoutTrace)) {
alarmNotifyRequest.setExecuteTimeoutTrace(executeTimeoutTrace);
}

@ -1,37 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.hippo4j.core.executor.support;
import org.slf4j.MDC;
import static cn.hippo4j.common.constant.Constants.EXECUTE_TIMEOUT_TRACE;
/**
* Executor context.
*/
public class ExecutorContext {
/**
* Put execute timeout trace.
*
* @param executeTimeoutTrace
*/
public static void putExecuteTimeoutTrace(String executeTimeoutTrace) {
MDC.put(EXECUTE_TIMEOUT_TRACE, executeTimeoutTrace);
}
}

@ -24,7 +24,7 @@ import static cn.hippo4j.common.constant.Constants.EXECUTE_TIMEOUT_TRACE;
/**
* Trace context util.
*/
public class TraceContextUtil {
public class ExecutorTraceContextUtil {
/**
* Execute timeout trace key.
@ -36,12 +36,21 @@ public class TraceContextUtil {
*
* @return
*/
public static String getAndRemove() {
public static String getAndRemoveTimeoutTrace() {
String val = MDC.get(EXECUTE_TIMEOUT_TRACE_KEY);
MDC.remove(EXECUTE_TIMEOUT_TRACE_KEY);
return val;
}
/**
* Put execute timeout trace.
*
* @param trace
*/
public static void putExecuteTimeoutTrace(String trace) {
MDC.put(EXECUTE_TIMEOUT_TRACE, trace);
}
/**
* Set execute timeout trace key.
*

@ -18,7 +18,7 @@
package cn.hippo4j.example.core.handler;
import cn.hippo4j.common.toolkit.StringUtil;
import cn.hippo4j.core.executor.support.ExecutorContext;
import cn.hippo4j.core.toolkit.ExecutorTraceContextUtil;
import org.slf4j.MDC;
import org.springframework.core.task.TaskDecorator;
@ -34,7 +34,7 @@ public final class TaskTraceBuilderHandler implements TaskDecorator {
String executeTimeoutTrace = MDC.get(EXECUTE_TIMEOUT_TRACE);
Runnable taskRun = () -> {
if (StringUtil.isNotBlank(executeTimeoutTrace)) {
ExecutorContext.putExecuteTimeoutTrace(executeTimeoutTrace);
ExecutorTraceContextUtil.putExecuteTimeoutTrace(executeTimeoutTrace);
}
runnable.run();
// There is no need to clean up here, and it will be cleaned up after the thread task is executed.

Loading…
Cancel
Save