JMX no JBoss

4 respostas
cv1

Galera, nao sei se vcs se lembram de um topico que eu postei aqui mes passado sobre Timer Tasks (url pros esquecidos: http://guj.com.br/forum/viewtopic.php?t=7983 ;)), e agora chegou finalmente a hora de botar o treco pra funcionar.

No topico original, eu falei algo sobre nao usar recursos do appserver, mas a gente resolveu de vez que vai usar JBoss. E, ja que eh assim, o jeito mais facil de fazer um Timer desses eh criando um MBean e registrando ele no servico de timer do JBoss, e tudo se resolve. :wink:

Os pobrema aqui sao: existe, de fato, um servico de timer no JBoss? Como eu posso usar o dito? E mais, como fazer um MBean direito? Achei alguns tutoriais bem rasinhos sobre desenvolvimento de MBeans, mas, no fundo no fundo, JMX ainda eh um grande ponto de interrogacao na minha cabeca. Alguem tem bons links sobre essa historia toda? :smiley:

4 Respostas

Luca

Olá

CV, posso emprestar-lhe o livro Managing J2EE with JMX do Juha Lindfors e Marc Fleury. Lá explica o Timer Service e tem um exemplo na listagem 7.5 chamada TimerAgent.java

Pegue os fontes deste livro em http://jboss.org/docs/index sob o item JMX, JAVA MANAGEMENT EXTENSIONS, BOOK BY PEARSON EDUCATION

Faz tempo que não mexo com JMX e tb estou sem tempo para reler. Me manda uma msg em PVT para combinarmos como lhe passo o livro.

[]s
Luca

Luca

Olá

Aí vai o código copiado do livro:

package book.jmx.examples;

import javax.management.*;
import javax.management.timer.*;
import java.util.Date;
import java.util.List;

public class TimerAgent {

  private MBeanServer server  = null;
  private ObjectName timer    = null;
  private ObjectName receiver = null;
  
  public void run() {
      
    // Find an agent from this JVM. Null argument will
    // return a list of all MBeanServer instances.  
    List list = MBeanServerFactory.findMBeanServer(null);
    server = (MBeanServer)list.iterator().next();    
      
    try {
        
      // register the timer and receiver mbeans
      timer    = new ObjectName("service:name=timer");
      receiver = new ObjectName("example:name=listener," +
                                "source=timer");
      
      server.registerMBean(new Timer(), timer);  
      server.registerMBean(new TimerReceiver(), receiver);

      // start the timer service
      server.invoke(timer, "start", null, null);
      
      // add scheduled notification to five seconds
      // past the registration
      Date date = new Date(System.currentTimeMillis() +
          Timer.ONE_SECOND * 5);
          
      Integer id = (Integer)server.invoke(
        timer,                          // MBean
        "addNotification",              // operation
        
        new Object[] {                  // arguments:
          "timer.notification",         // type
          "Scheduled notification.",    // message
          null,                         // user data
          date
        },
        
        new String[] {                  // signature
          String.class.getName(),
          String.class.getName(),
          Object.class.getName(),
          Date.class.getName()
        }
      );

      // add listener to the timer
      NotificationFilter filter = new TimerFilter(id);
      server.addNotificationListener(
                  timer, receiver, filter, null);
    }
    catch (JMException e) {
        e.printStackTrace();
    }
  }
  

  //
  // Notification filter implementation.
  //    
  class TimerFilter implements NotificationFilter {
      
    private Integer id = null;
      
    TimerFilter(Integer id) {
      this.id = id;
    }
      
    public boolean isNotificationEnabled(Notification n) {
              
      if (n.getType().equals("timer.notification")) {                  
        TimerNotification notif = (TimerNotification)n;
            
        if (notif.getNotificationID().equals(id))
          return true;
      }
          
      return false;
    }
  }
      

  //
  // Main method for the client. This will instantiate
  // an agent in the JVM.
  //
  public static void main(String[] args) {

    MBeanServer server = 
        MBeanServerFactory.createMBeanServer();
        
    new TimerAgent().run();
  }
}
package book.jmx.examples;

import javax.management.*;
import javax.management.timer.*;

public class TimerReceiver implements TimerReceiverMBean, NotificationListener {

    public void handleNotification(Notification n, Object handback) {
    
        System.out.println(n.getMessage());
    
    }

}
package book.jmx.examples;

import javax.management.*;
import javax.management.timer.*;

public interface TimerReceiverMBean {

}
javac -d . -classpath jmx-1_0_1-ri_bin\jmx\lib\jmxri.jar TimerAgent.java  TimerReceiver.javaTimerReceiverBean.java

java  -classpath .;jmx-1_0_1-ri_bin\jmx\lib\jmxri.jar  book.jmx.examples.TimerAgent

jmxri é a implementação de referência da Sun. Substitua pelo JBossMX MBean server.
http://www.jboss.org/index.html?module=html&op=userdisplay&id=developers/projects/jboss/jbossmx

http://sourceforge.net/project/showfiles.php?group_id=22866

http://sourceforge.net/project/shownotes.php?release_id=133660

Desculpe-me se não cheguei a captar seu problema.

[]s
Luca

cv1

Yeeeeeeeeeee-haaaaaaw!

Era disso mesmo que eu precisava, Luca, muchas muchas gracias! :smiley: :smiley: :smiley:

alberto.pereto

Oi pessoal, só pra documentar, tambem estava procurando algum tutorial sobre a criação de serviços JMX no JBoss…e tudo que eu precisei, encontrei nos links abaixo:

Writing JBoss Custom Services
http://blog.platinumsolutions.com/node/85
http://blog.platinumsolutions.com/node/119

Só mais uma coisa…se depois de vocês montarem toda a infra ocorrer um errro do tipo “Class does not expose a management interface…” tente verificar se a classe de vocês utilizada como Serviço e a classe de Interface estão no mesmo pacote…no meu caso eu tinha colocado a classe de Interface em uma subfolder e demorou pra eu descobrir que era isso.

abraço!

Criado 13 de janeiro de 2004
Ultima resposta 24 de jul. de 2006
Respostas 4
Participantes 3