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 阅读(242) 评论(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;
    }

    
//   
}

 


只有注册用户登录后才能发表评论。


网站导航: