Let's go inside

          this blog is deprecated as a result of laziness.
          posts - 59, comments - 2, trackbacks - 0, articles - 0

          TrailBlazer第6天--JMX

          Posted on 2006-07-27 13:10 Earth 閱讀(257) 評論(0)  編輯  收藏 所屬分類: JavaEE5/EJB3

          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;
          }

          你可以實現下面任意的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 " );
          ??}
          }

          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 " ;
          ???}
          ?
          %>


          二、定義MBeans之間的依賴關系,
          如同Ant中的任務一樣,用@Depends標注就可以了

          @Service?(objectName = " trail:service=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;
          ????}

          ????
          // ??
          }

          ?

          主站蜘蛛池模板: 岢岚县| 武陟县| 墨竹工卡县| 壤塘县| 彝良县| 子洲县| 天镇县| 庄浪县| 黑河市| 廉江市| 石泉县| 泸溪县| 蓬溪县| 来凤县| 渑池县| 黔西县| 电白县| 宿州市| 正镶白旗| 临西县| 新野县| 屯留县| 正定县| 化州市| 嘉峪关市| 兰坪| 安龙县| 庆阳市| 容城县| 黄浦区| 尼木县| 探索| 阳泉市| 绥棱县| 双鸭山市| 宿州市| 陈巴尔虎旗| 德庆县| 东乡县| 罗甸县| 石泉县|