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 閱讀(262) 評(píng)論(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服務(wù)以單例的形式存在于JVM中,它是有狀態(tài)并且存在于application范圍內(nèi)。

          1,定義JMX服務(wù)接口

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

          你可以實(shí)現(xiàn)下面任意的JMX生命周期方法:
          create() --
          start() --
          stop() --
          destroy() --

          2,定義JMX服務(wù)實(shí)現(xiàn)

          @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的實(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???

          <% @?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之間的依賴關(guān)系,
          如同Ant中的任務(wù)一樣,用@Depends標(biāo)注就可以了

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

          ????
          // ??
          }

          ?

          主站蜘蛛池模板: 土默特右旗| 岫岩| 军事| 加查县| 安多县| 中江县| 吉林市| 莫力| 全椒县| 西乌| 沭阳县| 石棉县| 金平| 佛学| 静海县| 乌拉特中旗| 酒泉市| 新绛县| 林芝县| 奉节县| 花垣县| 大丰市| 突泉县| 都兰县| 剑阁县| 饶河县| 保德县| 镇康县| 罗田县| 舟山市| 腾冲县| 永福县| 红桥区| 临澧县| 康平县| 临桂县| 同心县| 瑞安市| 浙江省| 宁都县| 万年县|