This is a custom system context demo, if there are deficiencies and omissions, welcome to point out the exchange ~

package com.xxx.lcloud.common.context; import com.alibaba.ttl.TransmittableThreadLocal; import java.util.Date; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicLong; /** * @classname: CustomContext * @desc: CustomContext * @author: Hinson * @date: Private static final TransmittableThreadLocal<String> public Class CustomContext{private static final TransmittableThreadLocal<String> CURRENT_CTXID_LOCAL = new TransmittableThreadLocal<>(); Private static Final AtomicLong CTXID_CREATER = new AtomicLong(); // Context pool private static final Map<String, CustomContextBean> CONTEXT_BEAN_POOL = new ConcurrentHashMap<>(); Private static Map<String, Map<String, Object>> CONTEXT_CACHE_MAP = new ConcurrentHashMap<>(); Private static final Map<String, String> CTXID_INHERITED_TABLE = new ConcurrentHashMap<>(); public static CustomContextBeaninit() {
        String ctxId = CURRENT_CTXID_LOCAL.get();
        if(ctxId ! = null) {return CONTEXT_BEAN_POOL.get(ctxId);
        } else{// This object is used to store some common values, such as the serial number in the following example, or the field CustomContextBean CustomContextBean = new CustomContextBean () throughout the business process; ctxId = currentCtxID(); CURRENT_CTXID_LOCAL.set(ctxId); CONTEXT_BEAN_POOL.put(ctxId, customContextBean); CONTEXT_CACHE_MAP.put(ctxId, new ConcurrentHashMap<>());returncustomContextBean ; }} /** * gets the current context object ** @return
     */
    public static ThreadLocal<String> getContext() {
        returnCURRENT_CTXID_LOCAL; } /** * gets the current context business field Bean ** @return
     */
    public static CustomContextBean getContextBean() {
        returnCONTEXT_BEAN_POOL.get(ctxId()); } /** * get the current context CTXID * @return
     */
    public static String currentCtxID() {
        String ctxID = CURRENT_CTXID_LOCAL.get();
        if (ctxID == null) {
            ctxID = CTXID_INHERITED_TABLE.get(assembleSubThreadID());
            if(ctxID == null) { ctxID = String.valueOf(nextCtxID()); CURRENT_CTXID_LOCAL.set(ctxID); }}returnctxID; } /** * the assembly child thread uniquely identifies * @return
     */
    private static String assembleSubThreadID() {
        returnString.valueOf(Thread.currentThread().getId()); } /** * generate context ID * @return
     */
    private static long nextCtxID() {
        long ctxID = CTXID_CREATER.incrementAndGet();
        if (CTXID_CREATER.compareAndSet(Long.MAX_VALUE, 0)) {
            LoggerUtil.info("Context CTXID is about to exceed long.max_value, the next context CTXID will start from [1]!" ,ctxID ,Long.MAX_VALUE );
        }
        returnctxID; } /** * get the map ** @ of the context cachereturn
       */
    public static Map<String, Object> getContextCache() {
        returnCONTEXT_CACHE_MAP.get(ctxId()); } /** * free context */ public static voidrelease() { String cid = ctxId(); // Remove context_bean_pool.remove (cid); Context_cache_map. remove(cid); // Remove context_cache_map. remove(cid); // Remove the thread variable current_ctxid_local.remove (); } /** * get the context Id ** @return
     */
    public static String ctxId() {
        String ctxId = CURRENT_CTXID_LOCAL.get();
        if (ctxId == null) {
            ctxId = CTXID_INHERITED_TABLE.get(Thread.currentThread().getName());
        }
        returnctxId; } /** * Add custom cache value ** @param key * @param object */ public static voidsetCache(String key, Object object) {
        Map<String, Object> cache = getContextCache();
        if(cache ! = null) { cache.put(key, object); }} /** * get the custom cache value ** @param key * @return
     */
    public static Object getCache(String key) {
        Map<String, Object> cache = getContextCache();
        return cache == null ? null : getContextCache().get(key);
    }

   Copy the code