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服務以單例的形式存在于JVM中,它是有狀態并且存在于application范圍內。
1,定義JMX服務接口
public
?
interface
?Calculator?{
?? // ?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;
}
?? // ?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;
}
你可以實現下面任意的JMX生命周期方法:
create() --
start() --
stop() --
destroy() --
2,定義JMX服務實現
@Service?(objectName
=
"
trail:service=calculator
"
)
@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 " );
??}
}
@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的實現必須使用@Service標注,它用來告訴JBoss需要以objectName為命名將其注冊為一個MBean Service,@Management表示服務接口,可以有多個, 你可以在JMX控制臺(http://localhost:8080/jmx-console/) 通過trail:service=calculator找到這個服務
3,使用JMX MBean服務
calculator.jsp???
<%
@?page?
import
=
"
trail.jmx.*,?org.jboss.mx.util.*,
??????????????????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 " ;
???}
? %>
??????????????????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之間的依賴關系,
如同Ant中的任務一樣,用@Depends標注就可以了
@Service?(objectName
=
"
trail:service=investmentAdvisor
"
)
@Depends?({ " trail:service=calculator " ,
??????????? " trail:service=riskAnalysis " })
public ? class ?InvestmentAdvisorMBean? implements ?InvestmentAdvisor?{
???? // ?
?
}
@Depends?({ " trail:service=calculator " ,
??????????? " trail:service=riskAnalysis " })
public ? class ?InvestmentAdvisorMBean? implements ?InvestmentAdvisor?{
???? // ?


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


}
?