多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
JMX,今天需要使用这个技术,实现一些手动接口的调用,调研了下简单的例子,如下 首先我们创建一个类,用来执行我们要调用的方法 ~~~ package com.redhorse.agent.jmx; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jmx.export.annotation.ManagedOperation; import org.springframework.jmx.export.annotation.ManagedResource; import com.redhorse.agent.cron.job.DayDataSummaryJob; //实例标记为由JMX管理的资源 @ManagedResource(objectName = "bean:name=Job", description = "My Managed Bean", log = true, logFile = "jmx.log", currencyTimeLimit = 15, persistPolicy = "OnUpdate", persistPeriod = 200, persistLocation = "foo", persistName = "bar") public class JobBean { @Autowired private DayDataSummaryJob dayDataSummaryJob; @ManagedOperation(description = "dayDataSummaryJob") public void dayDataSummaryJob() { dayDataSummaryJob.doJob(); } public void dontExposeMe() { throw new RuntimeException(); } } ~~~ spring的配置文件如下: ~~~ <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.0.xsd"> <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter"> <property name="assembler" ref="assembler" /> <property name="namingStrategy" ref="namingStrategy" /> <property name="autodetect" value="true" /> </bean> <bean id="jmxAttributeSource" class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource" /> <!-- will create management interface using annotation metadata --> <bean id="assembler" class="org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler"> <property name="attributeSource" ref="jmxAttributeSource" /> </bean> <!-- will pick up ObjectName from annotation --> <bean id="namingStrategy" class="org.springframework.jmx.export.naming.MetadataNamingStrategy"> <property name="attributeSource" ref="jmxAttributeSource" /> </bean> <bean id="testBean" class="com.redhorse.agent.jmx.JobBean"> </bean> </beans> ~~~ 代码中需要们改动的只有28行,其他代码不需要动。 这样就成功了。 这里我们通过jconsole访问,来执行方法的调用,如图 ![这里写图片描述](https://box.kancloud.cn/2016-03-15_56e7afc253498.jpg "")