:index-group: Other Features
:jbake-type: page
:jbake-status: status=published
= Registro automático do Mbean

Este exemplo mostra como criar e registrar automaticamente Mbeans usando features do TomEE.

== Dependências

Para poder usá-lo, você precisa importar a API do mbean (anotações):

....
<dependency>
  <groupId>org.apache.openejb</groupId>
  <artifactId>mbean-annotation-api</artifactId>
  <version>4.5.0</version>
  <scope>provided</scope>
</dependency>
....

== O MBean

O mbean implementa um jogo simples em que o objetivo é adivinhar um número.

Também permite que o usuário altere o valor.

....
package org.superbiz.mbean;

import javax.management.Description;
import javax.management.MBean;
import javax.management.ManagedAttribute;
import javax.management.ManagedOperation;

@MBean
@Description("play with me to guess a number")
public class GuessHowManyMBean {
    private int value = 0;

    @ManagedAttribute
    @Description("you are cheating!")
    public int getValue() {
        return value;
    }

    @ManagedAttribute
    public void setValue(int value) {
        this.value = value;
    }

    @ManagedOperation
    public String tryValue(int userValue) {
        if (userValue == value) {
            return "winner";
        }
        return "not the correct value, please have another try";
    }
}
....

Para registrar um MBean, basta especificar uma propriedade em
system.properties ou nas propriedades do contexto inicial.

....
Properties properties = new Properties();
properties.setProperty("openejb.user.mbeans.list", GuessHowManyMBean.class.getName());
EJBContainer.createEJBContainer(properties);
....

== Acessando o MBean

Em seguida, basta obter o servidor da plataforma e você pode jogar com parâmetros e operações:

....
package org.superbiz.mbean;

import org.junit.Test;

import javax.ejb.embeddable.EJBContainer;
import javax.management.Attribute;
import javax.management.MBeanInfo;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import java.lang.management.ManagementFactory;
import java.util.Properties;

import static junit.framework.Assert.assertEquals;

public class GuessHowManyMBeanTest {
    private static final String OBJECT_NAME = "openejb.user.mbeans:group=org.superbiz.mbean,application=mbean-auto-registration,name=GuessHowManyMBean";

    @Test
    public void play() throws Exception {
        Properties properties = new Properties();
        properties.setProperty("openejb.user.mbeans.list", GuessHowManyMBean.class.getName());
        EJBContainer container = EJBContainer.createEJBContainer(properties);

        MBeanServer server = ManagementFactory.getPlatformMBeanServer();
        ObjectName objectName = new ObjectName(OBJECT_NAME);
        MBeanInfo infos = server.getMBeanInfo(objectName);
        assertEquals(0, server.getAttribute(objectName, "value"));
        server.setAttribute(objectName, new Attribute("value", 3));
        assertEquals(3, server.getAttribute(objectName, "value"));
        assertEquals("winner", server.invoke(objectName, "tryValue", new Object[]{3}, null));
        assertEquals("not the correct value, please have another try", server.invoke(objectName, "tryValue", new Object[]{2}, null));

        container.close();
    }
}
....

=== Nota

Se o OpenEJB não encontrar nenhum módulo, ele não poderá ser iniciado. Então, para forçá-lo a iniciar, mesmo que o exemplo tenha apenas o mbean como classe java, adicionamos um arquivo `beans.xml` para transformar nosso projeto em um módulo Java EE.

== Executando

....
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running org.superbiz.mbean.GuessHowManyMBeanTest
Apache OpenEJB 4.0.0-beta-1    build: 20111002-04:06
http://tomee.apache.org/
INFO - openejb.home = /Users/dblevins/examples/mbean-auto-registration
INFO - openejb.base = /Users/dblevins/examples/mbean-auto-registration
INFO - Using 'javax.ejb.embeddable.EJBContainer=true'
INFO - Configuring Service(id=Default Security Service, type=SecurityService, provider-id=Default Security Service)
INFO - Configuring Service(id=Default Transaction Manager, type=TransactionManager, provider-id=Default Transaction Manager)
INFO - Found EjbModule in classpath: /Users/dblevins/examples/mbean-auto-registration/target/classes
INFO - Beginning load: /Users/dblevins/examples/mbean-auto-registration/target/classes
INFO - Configuring enterprise application: /Users/dblevins/examples/mbean-auto-registration
INFO - MBean openejb.user.mbeans:application=,group=org.superbiz.mbean,name=GuessHowManyMBean registered.
INFO - MBean openejb.user.mbeans:application=mbean-auto-registration,group=org.superbiz.mbean,name=GuessHowManyMBean registered.
INFO - MBean openejb.user.mbeans:application=EjbModule1847652919,group=org.superbiz.mbean,name=GuessHowManyMBean registered.
INFO - Configuring Service(id=Default Managed Container, type=Container, provider-id=Default Managed Container)
INFO - Auto-creating a container for bean mbean-auto-registration.Comp: Container(type=MANAGED, id=Default Managed Container)
INFO - Enterprise application "/Users/dblevins/examples/mbean-auto-registration" loaded.
INFO - Assembling app: /Users/dblevins/examples/mbean-auto-registration
INFO - Jndi(name="java:global/mbean-auto-registration/mbean-auto-registration.Comp!org.apache.openejb.BeanContext$Comp")
INFO - Jndi(name="java:global/mbean-auto-registration/mbean-auto-registration.Comp")
INFO - Jndi(name="java:global/EjbModule1847652919/org.superbiz.mbean.GuessHowManyMBeanTest!org.superbiz.mbean.GuessHowManyMBeanTest")
INFO - Jndi(name="java:global/EjbModule1847652919/org.superbiz.mbean.GuessHowManyMBeanTest")
INFO - Created Ejb(deployment-id=mbean-auto-registration.Comp, ejb-name=mbean-auto-registration.Comp, container=Default Managed Container)
INFO - Created Ejb(deployment-id=org.superbiz.mbean.GuessHowManyMBeanTest, ejb-name=org.superbiz.mbean.GuessHowManyMBeanTest, container=Default Managed Container)
INFO - Started Ejb(deployment-id=mbean-auto-registration.Comp, ejb-name=mbean-auto-registration.Comp, container=Default Managed Container)
INFO - Started Ejb(deployment-id=org.superbiz.mbean.GuessHowManyMBeanTest, ejb-name=org.superbiz.mbean.GuessHowManyMBeanTest, container=Default Managed Container)
INFO - Deployed Application(path=/Users/dblevins/examples/mbean-auto-registration)
INFO - Undeploying app: /Users/dblevins/examples/mbean-auto-registration
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.063 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
....
