1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | package com.claudeduguay.mbeans.spring; import javax.management.*; import javax.management.modelmbean.*; import org.springframework.jmx.export.*; import org.springframework.jmx.export.assembler.*; public class MBeanDescriptorEnabledExporter extends MBeanExporter { protected MBeanInfoAssembler mBeanInfoAssembler; public MBeanDescriptorEnabledExporter() { setAssembler(new MBeanDescriptorBasedAssembler()); } public ModelMBean createModelMBean() throws MBeanException { if (mBeanInfoAssembler instanceof MBeanDescriptorBasedAssembler) { return new ModelMBeanExtension(); } return super.createModelMBean(); } public void setAssembler(MBeanInfoAssembler mBeanInfoAssembler) { this.mBeanInfoAssembler = mBeanInfoAssembler; super.setAssembler(mBeanInfoAssembler); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | package com.claudeduguay.mbeans.spring; import java.io.*; import javax.management.modelmbean.*; import org.springframework.core.io.*; import org.springframework.jmx.export.assembler.*; import com.claudeduguay.mbeans.model.*; public class MBeanDescriptorBasedAssembler implements MBeanInfoAssembler { public ModelMBeanInfo getMBeanInfo( Object managedBean, String beanKey) { String name = managedBean.getClass().getName(); String path = name.replace('.', '/') + ".mbean.xml"; ClassPathResource resource = new ClassPathResource(path); InputStream input = null; try { input = resource.getInputStream(); MBeanDescriptor descriptor = MBeanDescriptorUtil.read(input); return descriptor.createMBeanInfo(); } catch (Exception e) { throw new IllegalStateException( "Unable to load resource: " + path); } finally { if (input != null) { try { input.close(); } catch (Exception x) {} } } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | package com.claudeduguay.jmx.demo.server; public class ExampleService { protected String propertyValue = "default value"; public ExampleService() {} public String getPropertyValue() { System.out.println("ExampleService: Get Property Value"); return propertyValue; } public void setPropertyValue(String propertyValue) { System.out.println("ExampleService: Set Property Value"); this.propertyValue = propertyValue; } public void startService() { System.out.println("ExampleService: Start Service Called"); } public void stopService() { System.out.println("ExampleService: Stop Service Called"); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <?xml version="1.0"?> <mbean name="ExampleService" description="Example Service" type="com.claudeduguay.jmx.demo.server.ExampleService"> <attribute name="propertyValue" description="Property Value Access" type="java.lang.String" readable="true" writable="true" /> <operation name="stopService" description="Stop Example Service" /> <operation name="startService" description="Start Example Service" /> <notification name="PropertyValueSet" types="example.service.set.propertyValue" description="PropertyValue was set" /> <notification name="BeforeStartService" types="example.service.before.startService" description="Example Service is Starting" /> <notification name="AfterStartService" types="example.service.after.startService" description="Example Service is Started" /> <notification name="BeforeStopService" types="example.service.before.stopService" description="Example Service is Stopping" /> <notification name="AfterStopService" types="example.service.after.stopService" description="Example Service is Stopped" /> </mbean> |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |