JMX-표준 MBean 과 모델 MBean 데모

9426 단어 자바jmxMBean
더 읽 기
모두 네 가지 MBean 이 있 습 니 다.
   
표준 MBean(Standard MBeans)의 디자인 과 실현 은 가장 간단 하 다.이런 MBean 은 자신의 방법 명 을 관리 인터페이스 로 사용한다.
    동적 MBean(Dynamic MBeans)은 지정 한 인 터 페 이 스 를 실현 해 야 합 니 다.동적 MBeans 가 실행 하 는 동안 관리 인 터 페 이 스 를 노출 하기 때문에 더욱 유연 합 니 다.
    MBean(Open MBeans)을 개방 하 는 것 은 동적 MBeans 에 속 합 니 다.이러한 MBean 은 기본 데이터 형식 에 의 해 유 니 버 설 관 리 를 실현 하고 우정 사용 자 를 위해 자기 성명 을 합 니 다.
   
모델 MBean(Model MBeans)역시 동적 MBeans 입 니 다.이런 MBeans 는 완전히 설정 할 수 있 고 운영 기간 에 자기 성명 을 할 수 있 습 니 다.그것들 은 자원 동적 도구 에 일반적인 기본 행동 을 하 는 MBeans 클래스 를 제공한다.
표준 MBean 과 모델 MBean 의 사용 방법 을 보 여 줍 니 다.
다음 프레젠테이션 의 원본 주소:https://github.com/smallbug-vip/repo이외에 도 자신 이 인터넷 에서 동적 MBean 의 Demo 를 검색 했다.
표준 MBean:
우선 관 리 된 bean 을 정의 합 니 다:

public class Bug implements BugMBean{

	private String name;
	private String food;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getFood() {
		return food;
	}

	public void setFood(String food) {
		this.food = food;
	}

	public void eat(String eat) {
		System.out.println("the bug is eating " + eat + ".");
	}

	@Override
	public String toString() {
		return "Bug [name=" + name + ", food=" + food + "]";
	}

}

이 빈 은 MBean 으로 끝 나 는 인 터 페 이 스 를 실현 했다.이 인터페이스 에서 설명 하 는 방법 은 모두 빈 대상 이 밖으로 노출 되 어야 하 는 조작 인터페이스 방법 이다.

public interface BugMBean {
	/**
	 *     
	 * 
	 * @timestamp Feb 10, 2016 3:22:58 PM
	 * @return
	 */
	public String getName();

	/**
	 *     
	 * 
	 * @timestamp Feb 10, 2016 3:23:23 PM
	 * @param name
	 */
	public void setName(String name);

	/**
	 *          
	 * 
	 * @timestamp Feb 10, 2016 3:23:37 PM
	 * @return
	 */
	public String getFood();

	/**
	 *         
	 * 
	 * @timestamp Feb 10, 2016 3:23:55 PM
	 * @param food
	 */
	public void setFood(String food);

	/**
	 *  
	 * 
	 * @timestamp Feb 10, 2016 3:24:08 PM
	 * @param eat
	 */
	public void eat(String eat);

	/**
	 *     
	 * 
	 * @timestamp Feb 10, 2016 3:24:15 PM
	 * @return
	 */
	public String toString();
}

사용 도구 Adaptor 테스트 코드:

try {
			helloName = new ObjectName("smallbug:name=Bug");
			server.registerMBean(new Bug(), helloName);

			ObjectName adapterName = new ObjectName("StandardAgent:name=htmladapter,port=8082");
			HtmlAdaptorServer adapter = new HtmlAdaptorServer();
			server.registerMBean(adapter, adapterName);
			adapter.start();
			System.out.println("start.....");
		} catch (Exception e) {
			throw new RuntimeException(e);
		}

이후 브 라 우 저 에 입력 할 수 있 습 니 다:localhost:8082 버그 대상 을 관리 합 니 다.HtmlAdaptorServer 를 사용 하려 면 pom.xml 에 도입 해 야 합 니 다:


			com.sun.jdmk
			jmxtools
			1.2.1
		

Adapter 를 사용 하지 않 아 도 됩 니 다:

private void test02() {
		try {
			helloName = new ObjectName("smallbug:name=Bug");
			server.registerMBean(new Bug(), helloName);

			changeProperties();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	private void changeProperties() {
		Attribute name = new Attribute("Name", "smallbug");
		Attribute food = new Attribute("Food", "leaves");
		try {
			server.setAttribute(helloName, name);
			server.setAttribute(helloName, food);

			System.out.println("the name is " + server.getAttribute(helloName, "Name"));
			Object[] params = { "beans" };
			String[] sign = { "java.lang.String" };
			server.invoke(helloName, "eat", params, sign);
			String s = (String) server.invoke(helloName, "toString", null, null);
			System.out.println(s);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

템 플 릿 MBean:
관 리 된 빈 은 인 터 페 이 스 를 실현 하지 않 는 것 외 에 위 버그 와 같다.그리고 BugMBean 클래스 를 정의 합 니 다.이 클래스 는 BaseModelMBean 을 계승 합 니 다.

public class BugMBean extends BaseModelMBean {

	public BugMBean() throws MBeanException, RuntimeOperationsException {
		super();
	}

	@Override
	public String getClassName() {

		return (this.resource.getClass().getName());

	}

	@Override
	public Object getAttribute(String name) throws AttributeNotFoundException, MBeanException, ReflectionException {
		System.out.println("-------------------" + name);
		return super.getAttribute(name);
	}

	@Override
	public void setAttribute(Attribute attribute) throws AttributeNotFoundException, MBeanException, ReflectionException {
		System.out.println("+++++++++++++++++++" + attribute);
		super.setAttribute(attribute);
	}

}

노출 이 필요 한 인터페이스 설정:






	

		
		
		
		
			
		
		
		
			
		
	




테스트:

public class ModelAgent {
	private Registry registry;
	private MBeanServer mBeanServer;

	public ModelAgent() {
		registry = createRegistry();
		try {
			mBeanServer = Registry.getServer();
		} catch (Throwable t) {
			t.printStackTrace(System.out);
			System.exit(1);
		}
	}

	public MBeanServer getMBeanServer() {
		return mBeanServer;
	}

	public Registry createRegistry() {
		Registry registry = null;
		try {
			URL url = ModelAgent.class.getResource("/jmx/bug-mbean.xml");
			InputStream stream = url.openStream();
			Registry.loadRegistry(stream);
			stream.close();
			registry = Registry.getRegistry();
		} catch (Throwable t) {
			System.out.println(t.toString());
		}
		return (registry);
	}

	public ModelMBean createModelMBean(String mBeanName) throws Exception {
		ManagedBean managed = registry.findManagedBean(mBeanName);
		if (managed == null) {
			System.out.println("ManagedBean null");
			return null;
		}
		ModelMBean mbean = managed.createMBean();
		ObjectName objectName = createObjectName();
		return mbean;
	}

	private ObjectName createObjectName() {
		ObjectName objectName = null;
		String domain = mBeanServer.getDefaultDomain();
		try {
			objectName = new ObjectName(domain + ":type=MyCar");
		} catch (MalformedObjectNameException e) {
			e.printStackTrace();
		}
		return objectName;
	}

	public static void main(String[] args) {
		ModelAgent agent = new ModelAgent();
		MBeanServer mBeanServer = agent.getMBeanServer();
		Bug bug = new Bug();
		System.out.println("Creating ObjectName");
		ObjectName objectName = agent.createObjectName();
		try {
			ModelMBean modelMBean = agent.createModelMBean("myMBean");
			modelMBean.setManagedResource(bug, "ObjectReference");
			mBeanServer.registerMBean(modelMBean, objectName);
		} catch (Exception e) {
			System.out.println(e.toString());
		}
		// manage the bean
		try {
			Attribute name = new Attribute("Name", "smallbug");
			Attribute food = new Attribute("Food", "leaves");
			mBeanServer.setAttribute(objectName, name);
			mBeanServer.setAttribute(objectName, food);
			String color = (String) mBeanServer.getAttribute(objectName, "Name");
			System.out.println("Name:" + color);

			Object[] params = { "beans" };
			String[] sign = { "java.lang.String" };
			mBeanServer.invoke(objectName, "eat", params, sign);
			String s = (String) mBeanServer.invoke(objectName, "toString", null, null);
			System.out.println(s);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}

좋은 웹페이지 즐겨찾기