Different from EJB services, which are provided by a pool of managed objects, a JMX MBean service is provided by a singleton object in the server JVM. This service object is stateful and has an application-wide scope.
JMX MBean服務(wù)以單例的形式存在于JVM中,它是有狀態(tài)并且存在于application范圍內(nèi)。
1,定義JMX服務(wù)接口
?? // ?Attribute
?? public ? void ?setGrowthrate?( double ?g);
?? public ? double ?getGrowthrate?();
?? // ?The?management?method
?? public ? double ?calculate?( int ?start,? int ?end,? double ?saving);
?? // ?Life?cycle?method
?? public ? void ?create?()? throws ?Exception;
?? public ? void ?destroy?()? throws ?Exception;
}
你可以實(shí)現(xiàn)下面任意的JMX生命周期方法:
create() --
start() --
stop() --
destroy() --
2,定義JMX服務(wù)實(shí)現(xiàn)
@Management(Calculator. class )
public ? class ?CalculatorMBean? implements ?Calculator?{
?? double ?growthrate;
?? public ? void ?setGrowthrate?( double ?growthrate)?{
???? this .growthrate? = ?growthrate;
??}
?? public ? double ?getGrowthrate?()?{
???? return ?growthrate;
??}
?? public ? double ?calculate?( int ?start,? int ?end,? double ?saving)?{
???? double ?tmp? =
????????Math.pow( 1 .? + ?growthrate? / ? 12 .,? 12 .? * ?(end? - ?start)? + ? 1 );
???? return ?saving? * ? 12 .? * ?(tmp? - ? 1 )? / ?growthrate;
??}
?? // ?Lifecycle?methods
?? public ? void ?create()? throws ?Exception?{
????growthrate? = ? 0.08 ;
????System.out.println( " Calculator?-?Creating " );
??}
?? public ? void ?destroy()?{
????System.out.println( " Calculator?-?Destroying " );
??}
}
MBean的實(shí)現(xiàn)必須使用@Service標(biāo)注,它用來(lái)告訴JBoss需要以objectName為命名將其注冊(cè)為一個(gè)MBean Service,@Management表示服務(wù)接口,可以有多個(gè), 你可以在JMX控制臺(tái)(http://localhost:8080/jmx-console/) 通過(guò)trail:service=calculator找到這個(gè)服務(wù)
3,使用JMX MBean服務(wù)
calculator.jsp???
??????????????????java.text. * ,?javax.management. * " %>
?
<%!
?? private ?Calculator?cal? = ? null ;
?? public ? void ?jspInit?()?{
???? try ?{
????????MBeanServer?server? = ?MBeanServerLocator.locate();
????????cal? = ?(Calculator)?MBeanProxyExt.create(
????????????Calculator. class ,
???????????? " trail:service=calculator " ,
????????????server);
????}? catch ?(Exception?e)?{
????????e.printStackTrace?();
????}
??}
%>
?
? <%
???String?result;
??? int ?start? = ? 25 ;
??? int ?end? = ? 65 ;
??? double ?saving? = ? 300.0 ;
??? try ?{
?????start? = ?Integer.parseInt(request.getParameter?( " start " ));
?????end? = ?Integer.parseInt(request.getParameter?( " end " ));
?????saving? = ?Double.parseDouble(request.getParameter?( " saving " ));
?
?????NumberFormat?nf? = ?NumberFormat.getInstance();
?????nf.setMaximumFractionDigits( 2 );
?????result? = ?nf.format(cal.calculate(start,?end,?saving));
???}? catch ?(Exception?e)?{
?????e.printStackTrace?();
?????result? = ? " Not?valid " ;
???}
? %>
二、定義MBeans之間的依賴關(guān)系,
如同Ant中的任務(wù)一樣,用@Depends標(biāo)注就可以了
@Depends?({ " trail:service=calculator " ,
??????????? " trail:service=riskAnalysis " })
public ? class ?InvestmentAdvisorMBean? implements ?InvestmentAdvisor?{
???? // ?


}
還有一種方法,使用依賴注入
public ? class ?InvestmentAdvisorMBean? implements ?InvestmentAdvisor?{
????@Depends?( " trail:service=calculator " )
???? public ? void ?setCalculator?(Calculator?calculator)?{
?????? this .calculator? = ?calculator;
????}
???? // ?


}
?