Skip to content

Instantly share code, notes, and snippets.

@jwlyn
Created April 7, 2015 12:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jwlyn/64409fa2c76339c5b2f6 to your computer and use it in GitHub Desktop.
Save jwlyn/64409fa2c76339c5b2f6 to your computer and use it in GitHub Desktop.
JMX 动态推送参数
package test;
import thread.SystemConfig;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import java.lang.management.ManagementFactory;
/**
* Created by cxu on 2015/4/7.
*/
public class Main {
private static final int DEFAULT_NO_THREADS = 10;
private static final String DEFAULT_SCHEMA = "default";
public static void main(String[] args) throws Exception {
// Get the MBean server
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
// register the MBean
SystemConfig mBean = new SystemConfig(DEFAULT_NO_THREADS, DEFAULT_SCHEMA);
ObjectName name = new ObjectName("xman:type=SystemConfig");
mbs.registerMBean(mBean, name);
do {
Thread.sleep(3000);
System.out.println("Thread Count=" + mBean.getThreadCount() + ":::Schema Name=" + mBean.getSchemaName());
} while (mBean.getThreadCount() != 0);
}
}
package thread;
/**
* Created by cxu on 2015/4/7.
*/
public class SystemConfig implements SystemConfigMBean {
private int threadCount;
private String schemaName;
public SystemConfig(int threadCount, String schemaName)
{
this.threadCount = threadCount;
this.schemaName = schemaName;
}
@Override
public void setThreadCount(int noOfThreads) {
threadCount = noOfThreads;
}
@Override
public int getThreadCount() {
return threadCount;
}
@Override
public void setSchemaName(String schemaName) {
this.schemaName = schemaName;
}
@Override
public String getSchemaName() {
return schemaName;
}
@Override
public String doConfig() {
return "No of Threads=" + this.threadCount + " and DB Schema Name=" + this.schemaName;
}
}
package thread;
/**
* Created by cxu on 2015/4/7.
*/
public interface SystemConfigMBean {
public void setThreadCount(int noOfThreads);
public int getThreadCount();
public void setSchemaName(String schemaName);
public String getSchemaName();
public String doConfig();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment