【问题标题】:how to modify ThreadPoolTaskExecutor at runtime through jmx如何通过jmx在运行时修改ThreadPoolTask​​Executor
【发布时间】:2011-08-08 19:45:31
【问题描述】:

我无法通过 JConsole 修改我的 MBean 属性。我有一个线程 bean 调用:

public static void main(String[] args) throws Exception {
    // JMX
    new SimpleJmxAgent();

    // spring executor context
    ApplicationContext ctx = new FileSystemXmlApplicationContext(
            "src/resources/ThreadContent.xml");

    startThreads(ctx);
}

private static void startThreads(ApplicationContext ctx) {

    TaskExecutor tE = (TaskExecutor) ctx.getBean("TaskExecutor");

    System.out.println("Starting threads");

    for (int i = 0; i < 10; i++) {
        tE.execute(new RepeatingGrpPoC());
    }

ThreadContent.xml 包含所有默认属性值。

SimpleJmxAgent 看起来像:

public SimpleJmxAgent() {

    mbs = ManagementFactory.getPlatformMBeanServer();

    // Spring context - used to load MBeans
    XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource(
            "resources/JMXcontent.xml"));

    // Unique identification of MBeans
    ThreadPoolManager threadBean = (ThreadPoolManager) factory.getBean("ThreadPoolBean");
    ObjectName threadName = null;

      try {
          // Uniquely identify the MBeans and register them with the platform MBeanServer 
          threadName = new ObjectName("THREADING:name=ThreadBean");
          mbs.registerMBean(threadBean, threadName);
       } catch(Exception e) {
          e.printStackTrace();
       }

我有 ThreadPoolManager 从 ThreadPoolTask​​Executor 继承,以便让它访问 Thread 属性的 getter 和 setter 方法,例如: public void setCorePoolSize(int corePoolSize)

编辑:

我已经实现了使用:

public void setCorePoolSize(int corePoolSize){
    super.setCorePoolSize(corePoolSize);
}

包裹在一个:

    public void changeCorePoolSize(int x){
    setCorePoolSize(x);

}

所以现在 Operation 出现在 MBeans 选项卡中。但是,属性显示为与所使用的值不同的值。我已经在我的 ThreadContext.xml 中设置了

property name="corePoolSize" value="5"

但是,当查看属性设置为默认值 1 时。我可以通过 changeCorePoolSize 操作通过 Jconsole 更改此设置,但只有外观效果会更改显示的值,但不会更改仍然有 5 个 TaskExecutor 线程仍在运行的正在进行的进程。

我在做的事情中是否遗漏了什么?什么可能导致我通过 ThreadContext.xml 设置的属性与在 Jconsole 的属性中显示的属性之间断开连接?

【问题讨论】:

    标签: java spring threadpool jmx mbeans


    【解决方案1】:

    使用super调用超类中的方法,避免死循环:

    public void setCorePoolSize(int corePoolSize){
        super.setCorePoolSize(corePoolSize);
    }
    

    【讨论】:

      【解决方案2】:

      你需要 super.setCorePoolSize(corePoolSize);

      【讨论】:

        【解决方案3】:

        如果我理解正确,您初始化池的核心大小为 5,但在运行时,将池大小重置为 1。当您说“仍然有 5 个 TaskExecutor 线程的正在进行的进程仍在运行”,是 5 个忙线程,还是 5 个空闲线程?

        如果它们很忙,那么在 4 个“多余”线程空闲之前,您的核心大小重置不会生效。

        【讨论】:

        • 你好 Nicholas,我不会在运行时重置。只是当我在 Jconsole 上检查它的值时显示为 1。我认为这是默认值。然而,在 Jconsole 中可以观察到 TaskExecutor 线程的 5 个实例。那么为什么这不显示 5 而不是 1?
        【解决方案4】:

        减少CorePoolSize应该足以减少活动线程的数量,但只有在当前运行的命令完成后才会生效。

        注意 MaxPoolSize 的影响,如果 workQueue 已满,可能会增加活动线程的数量。

        如果您有兴趣,我们打包了一个JMX enabled util.concurrent ThreadPoolExecutor,并通过一个简单的基于 Spring XML 命名空间的配置公开它。它公开了指标(activeCount、completeTackCount 等)和运行时配置参数(corePoolsize、maxPoolsize)。 您只需声明:

        <beans 
           xmlns:management="http://www.xebia.fr/schema/xebia-management-extras"
           ... >
        
           <!-- MBeanExporter is in charge of registering the ExecutorService MBean -->
           <context:mbean-export />
        
           <management:executor-service 
               id="my-executor" 
               pool-size="1-10" 
               queue-capacity="5"
               keep-alive="5"
               rejection-policy="ABORT" />
           ...
        <beans>

        该库包含 JSP 页面和一个 Hyperic HQ 插件来监控这些线程池。

        这个 与许多其他 JMX extra 打包在一起,以简化对常见组件(dbcp、util.concurrent、cxf、jms 等)的监控,并在 http://code.google.com/p/xebia-france/wiki/XebiaManagementExtras 的商业友好 Apache 软件许可下提出。

        希望这会有所帮助,

        西里尔

        【讨论】:

          猜你喜欢
          • 2019-04-30
          • 1970-01-01
          • 2016-03-29
          • 2017-03-02
          • 2021-01-02
          • 1970-01-01
          • 2017-04-27
          • 1970-01-01
          • 2015-08-09
          相关资源
          最近更新 更多