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訪問,來執行方法的調用,如圖
