Java Management Extensions (JMX)

Introdução

Uma demo

fig2.png (106556 bytes)

fig3.png (25473 bytes)

fig4.png (37009 bytes)

fig5_full.png (19664 bytes)

fig6_full.png (18240 bytes)

fig7_full.png (14251 bytes)

fig8_full.png (19962 bytes)

fig9_full.png (17026 bytes)

fig10.png (44903 bytes)

Arquitetura do JMX

fig1.png (12427 bytes)

Standard MBeans

A interface MBean

package com.example.mbeans; 
 
public interface HelloMBean { 
 
    public void sayHello(); 
    public int add(int x, int y); 
 
    public String getName(); 
 
    public int getCacheSize(); 
    public void setCacheSize(int size); 
} 

Implementação de um MBean

package com.example.mbeans; 
 
public class Hello implements HelloMBean { 
    public void sayHello() { 
	System.out.println("hello, world"); 
    } 
 
    public int add(int x, int y) { 
	return x + y; 
    } 
 
    public String getName() { 
	return this.name; 
    } 
 
 
    public int getCacheSize() { 
	return this.cacheSize; 
    } 
 
    public synchronized void setCacheSize(int size) { 
	this.cacheSize = size; 
 
	System.out.println("Cache size now " + this.cacheSize); 
    } 
 
    private final String name = "Reginald"; 
    private int cacheSize = DEFAULT_CACHE_SIZE; 
    private static final int DEFAULT_CACHE_SIZE = 200; 
} 

Gerenciando um recurso

package com.example.mbeans; 
 
import java.lang.management.*; 
import javax.management.*; 
 
public class Main { 
 
   public static void main(String[] args) throws Exception { 
 
      MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); 
 
      ObjectName name = new ObjectName("com.example.mbeans:type=Hello"); 
 
      Hello mbean = new Hello(); 
 
      mbs.registerMBean(mbean, name); 
 
      System.out.println("Waiting forever..."); 
      Thread.sleep(Long.MAX_VALUE); 
   } 
} 

Execução do exemplo

wpeB.jpg (15651 bytes)

wpeC.jpg (69594 bytes)

wpeD.jpg (16133 bytes)

wpeE.jpg (26610 bytes)

Envio de Notificações

Interface NotificationBroadcaster

package com.example.mbeans; 
 
import javax.management.*; 
 
public class Hello 
        extends NotificationBroadcasterSupport implements HelloMBean { 
 
    public void sayHello() { 
        System.out.println("hello, world"); 
    } 
 
    public int add(int x, int y) { 
        return x + y; 
    } 
 
    public String getName() { 
        return this.name; 
    } 
 
    public int getCacheSize() { 
        return this.cacheSize; 
    } 
 
    public synchronized void setCacheSize(int size) { 
        int oldSize = this.cacheSize; 
        this.cacheSize = size; 
 
        System.out.println("Cache size now " + this.cacheSize); 
 
        Notification n = 
            new AttributeChangeNotification(this, 
					    sequenceNumber++, 
					    System.currentTimeMillis(), 
					    "CacheSize changed", 
					    "CacheSize", 
					    "int", 
					    oldSize, 
					    this.cacheSize); 
 
	sendNotification(n); 
    } 
 
    @Override 
    public MBeanNotificationInfo[] getNotificationInfo() { 
        String[] types = new String[] { 
            AttributeChangeNotification.ATTRIBUTE_CHANGE 
        }; 
        String name = AttributeChangeNotification.class.getName(); 
        String description = "An attribute of this MBean has changed"; 
        MBeanNotificationInfo info = 
            new MBeanNotificationInfo(types, name, description); 
        return new MBeanNotificationInfo[] {info}; 
    } 
 
    private final String name = "Reginald"; 
    private int cacheSize = DEFAULT_CACHE_SIZE; 
    private static final int DEFAULT_CACHE_SIZE = 200; 
 
    private long sequenceNumber = 1; 
} 

Execução do exemplo

wpeF.jpg (31824 bytes)

Finalmente ...

Bibliografia

programa