一碟小菜,请各位大虾品尝!!!!
本人菜鸟一个,最近在看jmx. 在spring下应用jmx出现问题. 抄了一篇相关文章的代码结果在jconsole中没有出现MBean的类.
代码如下:
1.MBean:
public interface HelloMBean {
// operations
public void sayHello();
public int add(int x, int y);
// attributes
// a read-only attribute called Name of type String
public String getName();
// a read-write attribute called CacheSize of type int
public int getCacheSize();
public void setCacheSize(int size);
}
==========================================================================================
2.implementsMBean
public class Hello implements HelloMBean {
public void sayHello() {
System.out.println("hello, world");
}
public int add(int x, int y) {
return x + y;
}
public String getName() {
return this.name;
}
public int getCacheSize() {
return this.cacheSize;
}
public synchronized void setCacheSize(int size) {
this.cacheSize = size;
System.out.println("Cache size now " + this.cacheSize);
}
private final String name = "Reginald";
private int cacheSize = DEFAULT_CACHE_SIZE;
private static final int DEFAULT_CACHE_SIZE = 200;
}
==========================================================================================
3.Main
public class Main {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Hello mbean = (Hello)context.getBean("hello");
System.out.println("Waiting forever...");
Thread.sleep(Long.MAX_VALUE);
}
}
=========================================================================================
4.spring的配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="hello" class="test.Hello"> </bean>
<bean id="mBeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean"> </bean>
<bean id="startServer" class="test.StartServer">
<property name="server">
<ref local="mBeanServer"/>
</property>
</bean>
<bean id="exportor" class="org.springframework.jmx.export.MBeanExporter">
<property name="beans">
<map>
<entry key="bean:name=hello" value-ref="hello"/>
</map>
</property>
<property name="server">
<ref local="mBeanServer"/>
</property>
</bean>
</beans>
===========================================================================
请教各位大虾了!!!