yangys
2024-04-01 14f1953b1944b3e53d8312e151902c4695faa2e1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.qianwen.core.context;
 
import java.util.Map;
import java.util.concurrent.Callable;
import org.slf4j.MDC;
import com.qianwen.core.tool.utils.ThreadLocalUtil;
import org.springframework.lang.Nullable;
 
/* loaded from: blade-core-context-9.3.0.0-SNAPSHOT.jar:org/springblade/core/context/BladeCallableWrapper.class */
public class BladeCallableWrapper<V> implements Callable<V> {
    private final Callable<V> delegate;
    private final Map<String, Object> tlMap = ThreadLocalUtil.getAll();
    @Nullable
    private final Map<String, String> mdcMap = MDC.getCopyOfContextMap();
 
    public BladeCallableWrapper(Callable<V> callable) {
        this.delegate = callable;
    }
 
    @Override // java.util.concurrent.Callable
    public V call() throws Exception {
        if (!this.tlMap.isEmpty()) {
            ThreadLocalUtil.put(this.tlMap);
        }
        if (this.mdcMap != null && !this.mdcMap.isEmpty()) {
            MDC.setContextMap(this.mdcMap);
        }
        try {
            return this.delegate.call();
        } finally {
            this.tlMap.clear();
            if (this.mdcMap != null) {
                this.mdcMap.clear();
            }
            ThreadLocalUtil.clear();
            MDC.clear();
        }
    }
}