如何增强tomcat线程池行为

如何增强tomcat线程池行为

本文介绍了如何增强tomcat线程池行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 tomcat7 运行 java 应用程序.我需要存储每个 tomcat 线程的信息,所以我可以使用 ThreadLocals 方法.

I'm running the java application using tomcat7.I need to store the information per tomcat thread, so I can use the ThreadLocals approach.

在我的 server.xml 中,线程池定义如下所示:

In my server.xml the threadpool definition looks like the following:

 <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
    maxThreads="10000" minSpareThreads="2000"/>

我有一个 EnhanceThread 类

I have a class EnhanceThread

public class EnhanceThread extends Thread {
    ...
    @Override
    public void run() {
       SomeThreadLocals.set(data);
       super.run();
     }
}

如何覆盖 tomcat 线程池定义并使其使用我的类?有没有更好的方法来威胁这个问题?

How can I override the tomcat thread pool definition and make it use use my class?Is there a better approach to threat this problem?

推荐答案

您不需要为此而弄乱 Tomcat 线程.只需使用以下代码:

You don't need to mess with Tomcat threads for that. Just use the following code:

public class JsonSerializerFactory {
    private static final ThreadLocal<JsonSerializer> JSON_SERIALIZER =
        new ThreadLocal<JsonSerializer>() {
            @Override
            protected JsonSerializer initialValue() {
                 return new JsonSerializer();
            }
        };

    public static JsonSerializer get() {
        return JSON_SERIALIZER;
    }
}

每次需要序列化器时,调用JsonSerializerFactory.get(),它将返回线程本地实例,如果尚不存在则懒惰地创建它.

Each time you need a serializer, call JsonSerializerFactory.get(), and it will return the thread-local instance, and lazily create it if it doesn't exist yet.

这篇关于如何增强tomcat线程池行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 05:59