Coder Social home page Coder Social logo

alibaba / transmittable-thread-local Goto Github PK

View Code? Open in Web Editor NEW
7.3K 275.0 1.7K 3.46 MB

📌 a missing Java std lib(simple & 0-dependency) for framework/middleware, provide an enhanced InheritableThreadLocal that transmits values between threads even using thread pooling components.

Home Page: https://github.com/alibaba/transmittable-thread-local

License: Apache License 2.0

Shell 1.13% Java 60.88% Kotlin 37.99%
java threadlocal threadsafe transmittable context thread-pool executor transmittable-thread-local middleware framework

transmittable-thread-local's Issues

MtContext的设置和恢复时没有处理子中多余Key的情况

  1. 恢复子时没有处理子中多余Key的情况,结果是:
    子比父多的MtContext被保留在留在子中。
  2. 设置子时没有处理子中多余Key的情况,结果是:
    在运行子时,有比父多多的Context!

上面2点合起来,结果是:
在子运行中多出来的Key,被一直保留了。

这打破了MtContext执行时独立的上下文的效果。

去slf4j & log4j的依赖

Agent的使用方式,Jar要加到Boot ClassPath上(-Xbootclasspath/a:
所以依赖一定要精简。

改成JDK的Log:java.util.logging.Logger

支持Timer

  • 提供TimerTask的MtContext的Wrapper
  • Agent方式修改TimerTask类

以解决MTC可能失效的问题

能否提供与LOG4J(2)中的MDC集成或增强

Hi~

注意到multiple-thread-context提供了上下文线程变量的传递,并提供了针对RunnableCallable的支持。由于上述场景中有一部分是用在日志中,当前log4j(2)提供了MDC的支持。

想问的是,能否提供一个针对其的增强支持,以log4j2为例已经提供了入口操作的类ThreadContext,若在Executor中传递则需要 使用 该类中的 getContext() and cloneStack() 方法。

此外,框架是否考虑了如何清除这些线程安全变量(以WEB为例大多数是在filter里面使用PUT然后 执行完毕后 POPCLEAR)——是否有好的建议(以multi-thread-context的使用为例)。

com.alibaba.mtc.MtContextRunnable#get、com.alibaba.mtc.MtContextCallable#get方法不应该幂等

调用com.alibaba.mtc.MtContextRunnable#getcom.alibaba.mtc.MtContextCallable#get方法的意图是保存的是方法调用时的线程上下文。

这样方法如果幂等,则实际效果是之前生成的MtContextRunnableMtContextCallable时所保存的上下文。即抓取的上下文是错的。

如果传入的MtContextRunnable、MtContextCallable应该 FastFail!实现上可以抛出IllegalStateException异常。

API调整成ThreadLocal风格

使用方式由:

// 静态方式,设置和读取配置 
MtContext.set("key", "value");
MtContext.get("key");

改成:

// 一个对象维护一个对象
static MtContext = new MtContext() {
        @Override
        protected MtContext initialValue() {
            return new FooInfo();
        }

        @Override
        protected MtContext childValue(FooInfo parentValue) {
            return new FooInfo(parentValue);
        }
}

这样API的好处:

  • 不需要接管对象的创建,这样不需要提供Copyable接口。更友好。
  • 不需要检查调用线程,只能是当前线程 #6 。更安全。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.