1 / 116

NSY102 Conception de logiciels Intranet JMX Java Management eXtension API

NSY102 Conception de logiciels Intranet JMX Java Management eXtension API. Cnam Paris jean-michel Douin, douin au cnam point fr version 08 Avril 2011. Architecture à base de composants. Sommaire. Objectifs Supervision d’une JVM, locale ou distante

jaguar
Télécharger la présentation

NSY102 Conception de logiciels Intranet JMX Java Management eXtension API

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. NSY102Conception de logiciels Intranet JMXJava Management eXtension API Cnam Paris jean-michel Douin, douin au cnam point fr version 08 Avril 2011 Architecture à base de composants

  2. Sommaire • Objectifs • Supervision d’une JVM, locale ou distante • Lecture/écriture/mise à jour/téléchargement/… • Une Première approche • Un exemple simple • Un Managed Bean (MBean) • Un agent • Une supervision comme démonstration • De plus près : 3 niveaux • Instrumentation • Standard, dynamic, open, model Beans et MXBeans. • Agent / serveur • Installation et accès aux « MBeans » • Distribué • Connecteurs et adaptateurs • Applette et JMX, Plug-in et JMX

  3. Bibliographie utilisée • La présentation de Christophe Ebro • http://rangiroa.essi.fr/cours/internet/02-JMX-partie2.pdf • L’indispensable tutoriel de Sun • http://java.sun.com/docs/books/tutorial/jmx/index.html • http://java.sun.com/javase/technologies/core/mntr-mgmt/javamanagement/ • Hello world • http://java.sun.com/j2se/1.5.0/docs/guide/jmx/examples.html • Le blog de Daniel Fuchs et celui de Luis-Miguel Alventosa • http://blogs.sun.com/jmxetc/http://blogs.sun.com/lmalventosa/ • JMX et Design Patterns chez hp • http://devresource.hp.com/drc/resources/jmxbestp_dp_pres/index.jsp • Côté développeur ibm • http://www-128.ibm.com/developerworks/java/library/j-jmx2/ • JMX in Action, ed. Manning • Benjamin G.Sullins et Mark B. Whipple http://java.sun.com/developer/Books/javaprogramming/jmx/ http://www.xmojo.org/products/xmojo/index.html Spécialisé MXBean / accès à la JVM • http://www-128.ibm.com/developerworks/java/library/j-mxbeans/ divers http://www-adele.imag.fr/users/Didier.Donsez/ujf/sujet/jmx.html

  4. Pré-requis • Notions de • Introspection • En tant qu’utilisateur • Client/serveur, • Protocole JRMP( rmi) et HTTP • Patrons Factory, Publish-Subscribe, DynamicProxy

  5. JMX : Objectifs • Supervision de JVM locales ou distantes • En cours d’exécution • Gestion/administration de Ressources • Matérielles comme logicielles • Configuration/déploiement/mise à jour • Statique et dynamique • Contrôle • Du Cycle de vie : start/stop/suspend/resume • De la Charge en vue d’une meilleure répartition • Supervision • Performance • Des erreurs/ exceptions • De l’état (cf. cycle de vie)

  6. Supervision : mouture fine… • Détection de la mémoire utilisée • Déclenchement du ramasse-miettes, ou bien son arrêt • Chargement de classe en mode verbose • Détection d’un interblocage • Accès aux ressources de l’OS • Gestion d’application de type MBean • Gestion de serveurs • Gestion d’applettes ! • Au sein d’un navigateur … • …

  7. JMX API • Hypothèse : tout est JAVA-JVM • Ressources matérielles • Ressources logicielles • Depuis J2SE 1.5, mais 1.6 sera préféré, et JMX >= 1.4 • Adoptée par de nombreuses entreprises • http://java.sun.com/javase/technologies/core/mntr-mgmt/javamanagement/jmxadoption.jsp • Déjà présent au sein de plate-formes commerciales … JBoss, Apache, GlassFish, • Outils prédéfinis • jconsole, jvisualvm(1.6 update11), interface vers rmi/http/snmp/jini … • JDMK en open source • Java Dynamic Management Kit http://java.sun.com/products/jdmk/

  8. Architecture • 3 niveaux • Instrumentation • Gestion de la ressource par un composant (MBean, Managed Bean) • Agents • Initialisation, installation au sein du serveur de MBean • Distribué/intégration • Adaptateurs de Protocoles • RMI/HTTP/SNMP-CMIP • Sécurité • Common Management Information Protocol (CMIP), protocole ISO • Simple Network Management Protocol (SNMP)

  9. Schéma de Christophe Ebro de Sun • 3 niveaux instrumentation, serveur, distribué JVM JVM

  10. Objectifs rappel contexte JVM • Accès à une JVM « de l’extérieur » • Supervision d’une JVM en cours d’exécution … • Gestion d’objets Java déjà en place… MBean • Accès aux attributs (introspection) • Lecture/écriture • Appels de méthodes (introspection) • Passage de paramètres / retour de résultat • Notifications, (publish-subscribe) • évènements • Ajout dynamique de nouveaux objets MBean • Code des classes en place spécialisé • Code « hors-place » à télécharger • Retrait dynamique d’objets MBean • Un premier exemple …

  11. Par l’exemple : un capteur… • SensorMBean Instrumentation • Un capteur / une mesure • SensorAgent Serveur • Installation du service • Outil jconsole Distribué • Un client en standard (j2SE)

  12. Un exemple en introduction • SensorMBean • Sensor Instrumentation Outil de Sun jconsole Distribué • SensorAgent • Serveur

  13. Les classes en présence Outil de Sun jconsole SensorMBean Sensor SensorAgent • SensorMBean.java • Une interface, les méthodes proposées • Sensor.java • Une implémentation de l’interface • SensorAgent • Inscription auprès du serveur de MBean (prédéfini) • Les méthodes deviennent des services, accesibles via le serveur de MBean • jconsole • Une application prédéfinie Instrumentation Serveur Distribué

  14. Instrumentation, StandardMBean public interface SensorMBean { // getter/setter public int getValue(); public void setValue(int val); // operations public void reset(); } MBeansuffixe imposé … syntaxe à respecter

  15. Remarques de l’introspecteur … getter/setter selon Sun, une convention d’écriture public int getValue(); public void setValue(int val); Un outil peut donc en déduire qu’il existe un attribut int value; accessible en lecture et en écriture

  16. Sensor implements SensorMBean public class Sensor implements SensorMBean{ private final int PERIOD; private int value; // Acquisition est une classe interne // une simulation du capteur, page suivante private Acquisition local; public Sensor(int period){ this.PERIOD = period; local = this.new Acquisition(); } public synchronized int getValue(){ return value; } public synchronized void setValue(int value){ this.value = value; } public void reset(){… page suivante Classe Sensorimposée …

  17. Sensor suite public void reset(){ local.interrupt(); try{local.join();}catch(InterruptedException ie){} System.out.println("reset ...."); local = this.new Acquisition(); } • private class Acquisition extends Thread implements java.io.Serializable{ • private Random random= new Random(); • public Acquisition(){ • random= new Random(); start(); • } • public void run(){ • int valeur = 0; • try{ • while(!isInterrupted()){ • Thread.sleep(PERIOD/2); • int value = random.nextInt(100); • Thread.sleep(PERIOD/2); • System.out.println("." + value);} // cf page suivante • }catch(InterruptedException ie){} • } • }

  18. Démonstration • SensorMBean s = new Sensor(2000); Une « mesure » toutes les deux secondes

  19. Agent ou impresario • L’agent du bean se charge de • L’installation (instanciation) du MBean • L’enregistrement auprès du serveur de MBeans • MBeanServer • Un nom unique lui est attribué • en général par l’utilisateur, • selon une convention de nommage • Apparenté Properties exemple : SensorAgent:name=Sensor1 • namela clé, Sensor1 la valeur • Cet agent est ici installé sur la même JVM • D’autres peuvent le faire : un autre MBean, le serveur, de l’extérieur …

  20. Agent : SensorAgent public class SensorAgent{ private MBeanServer mbs; public SensorAgent(){ try{ mbs = ManagementFactory.getPlatformMBeanServer(); ObjectName name = new ObjectName("SensorAgent:name=Sensor1"); Sensor mbean = new Sensor(2000); // création du mbean mbs.registerMBean(mbean, name); // enregistrement }catch(Exception e){ …} } public static void main(String[] args)throws Exception{ SensorAgent agent = new SensorAgent();

  21. Commentaires sur l’agent mbs = ManagementFactory.getPlatformMBeanServer(); Le gestionnaire de MBean prédéfini ObjectName name = new ObjectName("SensorAgent:name=Sensor1"); Un nom, éventuellement suivi de propriétés SensorMBeanmbean = new Sensor(2000); Création du mbean, même JVM mbs.registerMBean(mbean, name); Enregistrement auprès du gestionnaire

  22. Supervisions prédéfinies • Outils • jconsole • jvisualVM • jps

  23. Distribué : jconsole • Accès à l’une des JVM, • Déjà équipée de son MBeanServer* SensorAgent (ici numérotation PID « windows ») Les PID des JVM : console> jps * Plusieurs MBeanServer Peuvent co-exister

  24. Distribué : jconsole • name=Sensor1 • Accès aux attributs • getter/setter, par introspection

  25. Distribué : jconsole • name=Sensor1 • opération reset()

  26. Distribué : jconsole • Onglet Threads, un MXBean abordé par la suite • SensorAgent est bien endormi…

  27. Java VisualVM • jvisualVM • https://visualvm.dev.java.net/download.html • https://visualvm.dev.java.net/docindex.html • jconsole global

  28. jvisualVM, puissant …

  29. jvisualVM, à essayer

  30. Résumé, Pause … • MBean • Un composant • Agent • Chargé du dialogue avec l’infrastructure(canevas/framework) • Jconsole, jvisualvm deux outils prédéfinis • Ce sont des applications écrites en Java… • À suivre : • Accès aux MBean depuis n’importe quelle application • Locale ou distante

  31. Jconsole comme SensorClient • SensorMBean • Sensor Instrumentation Outil de Sun jconsole • SensorAgent • SensorClient • SensorClient ou comment accéder au bean • Depuis une application java ordinaire ?

  32. Client : SensorClient, même JVM public class SensorClient{ // même JVM // SensorAgent a = new SensorAgent(); préalable MBeanServer mbs = …… // à la recherche du MBean ObjectName name = new ObjectName("SensorAgent:name=Sensor1"); // accès aux attributs par leur nom // accès à l’attribut, getValue() System.out.println(mbs.getAttribute(name, "Value")); // exécuter une opération Object[] paramètres = new Object[]{}; String[] signature = new String[]{}; mbs.invoke(name, "reset", paramètres, signature);

  33. Remarques // même JVM // SensorAgent a = new SensorAgent(); au préalable // SensorAgent et le client ont accès au même MBeanServer MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); // à la recherche du MBean ObjectName name = new ObjectName("SensorAgent:name=Sensor1"); // depuis une autre JVM, ce capteur n’est pas accessible (pour le moment)

  34. SensorClient & DynamicProxy // accès à l’aide d’un dynamicProxy SensorMBean sensor = (SensorMBean) MBeanServerInvocationHandler.newProxyInstance( mbs, // MBeanServer name, // ObjectName SensorMBean.class, // interface false); // détaillé par la suite System.out.println(sensor.getValue()); sensor.reset(); DynamicProxy cf le cours Proxy … attention ici même JVM

  35. Résumé • Instrumentation un MBean • Serveur un agent qui pourrait devenir un MBean… • Client même JVM, à l’aide d’un mandataire dynamique • Distribué jconsole, jvisualvm

  36. Notifications • Réveil, alarmes, pannes … • Ou bien Notifications, évènements asynchrones, réseau ? • Patron Observateur distribué … • Publish-subscribe : filtre d’évènements possible • Notifications • Vers des JVM installées sur la même machine • Vers des JVM distantes Notification  NotificationBroadcaster  NotificationListener

  37. Réveil & notifications • À la suite d’un changement d’état • Patron publish/subscribe, mode pull (pull : inscription à la demande du client)

  38. Comment ? notify • Une classe prédéfinie • NotificationBroadcasterSupport sendNotification(Notification notification) Ajout/retrait d’un observateur/écouteur addNotificationListener( NotificationListener listener, NotificationFilter filter, Object handback) … removeNotificationListener( …

  39. Comment : Listener • L’interface NotificationListener handleNotification(Notification notification, Object handback)

  40. Comment ? • A chaque changement d’état une notification est effectuée • sendNotification (new Notification(….,….,….)); • Éventuellement filtrée • Interface NotificationFilter • booleanisNotificationEnabled(Notification notification) • Exécutée avant chaque notification • Une notification : classe Notification • Constructeurs • Notification(String type, Object source, long sequenceNumber) • Notification(String type, Object source, long sequenceNumber, long timeStamp, String message)

  41. Avec SensorMBean public interface SensorMBean extends NotificationBroadcaster{ // getter/setter public int getValue(); public void setValue(int val); // operation public void reset(); }

  42. jconsole a tout compris … • L’item Notifications apparaît • Souscription possible depuis une autre JVM …

  43. Sensor • A chaque modification de la valeur du capteur • Une notification est engendrée • Notifications distantes sur le réseau • Arrivée possible dans le désordre  estampille

  44. Instrumentation & Notifications public class Sensor extends NotificationBroadcasterSupport implements SensorMBean{ public synchronized void setValue(int value){ this.value = value; this.sequenceNumber++; sendNotification( new Notification( "setValue", // un nom this, sequenceNumber, // un numéro System.currentTimeMillis(), // une estampille Integer.toString(value))); // un message }

  45. L’  Agent & notifications • L’agent est un ici observateur de « son » MBean public class SensorAgent implements NotificationListener{ private MBeanServer mbs; public SensorAgent(){ try{ … mbean.addNotificationListener(this,null,null); }catch(Exception e){ e.printStackTrace(); } } public void handleNotification( Notification notification, Object handback){ System.out.print(notification.getMessage()); System.out.println(" number : " + notification.getSequenceNumber()); }

  46. Client : jconsole & notifications jconsole a souscrit

  47. SensorClient est un écouteur public class SensorClient implements NotificationListener{ public void handleNotification( Notification notification, Object handback){ …. } public static void main(String[] args) throws Exception { SensorAgent a = new SensorAgent(); // même JVM ici MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); ObjectName name = new ObjectName("SensorAgent:name=Sensor"); NotificationListener nl = new SensorClient(); SensorMBean sensor = (SensorMBean) MBeanServerInvocationHandler.newProxyInstance(mbs, name, SensorMBean.class, false); sensor.addNotificationListener(nl, null,null);

  48. SensorClient : le mandataire encore lui SensorMBean sensor = (SensorMBean) MBeanServerInvocationHandler.newProxyInstance(mbs, name, SensorMBean.class, true); true retourne un proxy qui impléménte l’interface NotificationEmitter, soit la possibilité de retirer un écouteur de ce MBean (par procuration …) SensorMBean sensor = (SensorMBean) MBeanServerInvocationHandler.newProxyInstance( mbs, name, SensorMBean.class, true); … ((NotificationEmitter)sensor).removeNotificationListener(nl, null, null) ;

  49. Cycle de vie, pre, prede, post, postde • interface MBeanRegistration • Méthodes déclenchées • à l’installation du MBean pre… • à la désinstallation post • Pour • Lire une configuration globale ou d’un autre MBean • S’enquérir d’une valeur • Mise à jour d’un journal • …

  50. interface MBeanRegistration interface MBeanRegistration{ public void postDeregister(); public void postRegister(Boolean done); public void preDeregister(); // le MBeanServer est transmis  accès aux autres MBean public ObjectName preRegister(MBeanServer server, ObjectName name); } Un exemple : obtenir la valeur courante du capteur via le MBean Sensor préalablement installé

More Related