如何通过jmx在运行时修改ThreadPoolTaskExecutor
我在通过 JConsole 修改 MBean 属性时遇到问题。我有一个调用的 Threading 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();
}
我从 ThreadPoolTaskExecutor 继承了 ThreadPoolManager,以便让它能够访问 Thread 属性的 getter 和 setter 方法,例如: public void setCorePoolSize(int corePoolSize)
编辑:
我已经实现了以下内容的使用:
public void setCorePoolSize(int corePoolSize){
super.setCorePoolSize(corePoolSize);
}
包装在 a: 中:
public void changeCorePoolSize(int x){
setCorePoolSize(x);
}
所以现在操作出现在 MBeans 选项卡中。然而,属性显示为与所使用的不同的值。我已经在 ThreadContext.xml 中进行了设置,
property name="corePoolSize" value="5"
但是当查看属性设置为 1 时,这是默认值。我可以通过 Jconsole 通过 changeCorePoolSize
操作更改此设置,但仅具有更改显示值的装饰效果,但不会更改仍具有 5 个 TaskExecutor 的正在进行的进程
线程仍在继续。
我在做的事情中遗漏了什么吗?什么可能导致我通过 ThreadContext.xml 设置的属性与 Jconsole 属性中显示的属性之间断开连接?
I'm having trouble modifying my MBean properties through JConsole. I have a Threading bean which invoked with:
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 contains all default property values.
SimpleJmxAgent looks like:
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();
}
I have the ThreadPoolManager inherting from ThreadPoolTaskExecutor in order to give it access to the getter and setter methods of the Thread properties such as:public void setCorePoolSize(int corePoolSize)
EDIT:
I have implemented the use of:
public void setCorePoolSize(int corePoolSize){
super.setCorePoolSize(corePoolSize);
}
wrapped in a:
public void changeCorePoolSize(int x){
setCorePoolSize(x);
}
So now the Operation appears in MBeans tab. However the Attributes are shown as different values as what is being used. I have set in my ThreadContext.xml
property name="corePoolSize" value="5"
However when viewing attribute is set to 1 which is a default value. I can change this through the Jconsole via the changeCorePoolSize
operation but has only a cosmetic effect changing the value displayed but not changing the ongoing process which still has 5 TaskExecutor
threads still going.
Am I missing something in what I am doing? What could causing the disconnect between the properties I'm setting through ThreadContext.xml and that being displayed in the attributes in Jconsole?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你需要 super.setCorePoolSize(corePoolSize);
You need super.setCorePoolSize(corePoolSize);
如果我理解正确的话,您可以使用核心大小 5 初始化池,但随后在运行时将池大小重置为 1。当您说“仍然有 5 个 TaskExecutor 线程的正在进行的进程仍在运行”,是 5 个繁忙线程,还是 5 个空闲线程?
如果它们很忙,则核心大小重置将不会生效,直到 4 个“多余”线程变得空闲。
If I understand correctly, you initialize the pool with a core size of 5, but then at runtime, reset the pool size to 1. When you say, "the ongoing process which still has 5 TaskExecutor threads still going", is that 5 busy threads, or 5 idle threads ?
If they are busy, you core size reset will not take effect until the 4 "excess" threads become idle.
减少 CorePoolSize 应该足以减少活动线程的数量,但只有在当前运行的命令完成后才会生效。
请注意 MaxPoolSize 的影响,如果工作队列已满,它可能会增加活动线程的数量。
如果您有兴趣,我们打包了一个支持 JMX 的 util.concurrent ThreadPoolExecutor 和通过一个简单的基于 spring XML 命名空间的配置来公开它。它公开指标(activeCount、completedTackCount 等)和运行时配置参数(corePoolsize、maxPoolsize)。
您只需声明:
该库包含 JSP 页面和一个 Hyperic HQ 插件来监视这些线程池。
这个<执行者服务/>>与许多其他 JMX 附加功能打包在一起,以简化对常见组件(dbcp、util.concurrent、cxf、jms 等)的监控,并在商业友好的 Apache 软件许可证下建议,网址为 http://code.google.com/p/xebia-france/wiki/XebiaManagementExtras 。
希望这有帮助,
西里尔
Reducing the CorePoolSize should be enough to reduce the the number of active thread but it only takes effect after the currently running commands complete.
Beware of the effect of MaxPoolSize wich may increse the number of active threads if the workQueue is full.
If you are interested, we packaged a JMX enabled util.concurrent ThreadPoolExecutor and expose it via a simple spring XML namespaced based configuration. It exposes both metrics (activeCount, completedTackCount, etc) and runtime configuration parameters (corePoolsize, maxPoolsize).
You simply have to declare :
The library contains JSP page and an Hyperic HQ plugin to monitor these thread pools.
This <executor-service /> is packaged with many other JMX extras to ease monitoring of common components (dbcp, util.concurrent, cxf, jms, etc) and proposed under a business friendly Apache Software License at http://code.google.com/p/xebia-france/wiki/XebiaManagementExtras .
Hope this helps,
Cyrille
使用
super
调用超类中的方法,以避免无限循环:Use
super
to call the method in the superclass, to avoid the infinite loop: