Liberty : Exemples d'accès aux opérations et aux attributs de bean géré

Vous pouvez utiliser Liberty pour accéder aux attributs et appeler les opérations des beans de gestion JMX (Java™ Management Extensions).

Une fois que vous avez obtenu une instance MBeanServer (pour une application qui s'exécute dans Liberty) ou une instance MBeanServerConnection (pour un client externe), vous pouvez accéder aux attributs ou appeler les opérations des beans gérés fournis par Liberty. Voir Utilisation de beans gérés JMX sous Liberty.

Les exemples de code ci-dessous supposent que la variable mbs est une instance MBeanServer ou MBeanServerConnection. Vous pouvez utiliser les méthodes fournies pour accéder aux attributs et aux opérations de la même façon qu'une réflexion Java. Sinon, chaque bean géré possède une interface de gestion avec des méthodes d'accès get pour les attributs et des méthodes pour les opérations. Vous pouvez utiliser ces interfaces by l'une des méthodes javax.managementJMX.newMBeanProxy ou l'une des méthodes javax.management.JMX.newMXBeanProxy pour les beans gérés de type MXBean afin d'obtenir un objet proxy. Le nom de l'interface de gestion se termine par "MXBean". Pour les noms des interfaces de gestion, voir Liste des MBeans fournis.

Exemple 1 : Vérification de l'état de l'application "myApp"

import javax.management.ObjectName;
import javax.management.JMX;
import com.ibm.websphere.application.ApplicationMBean;
...

ObjectName myAppMBean = new ObjectName(
"WebSphere:service=com.ibm.websphere.application.ApplicationMBean,name=myApp");
if (mbs.isRegistered(myAppMBean)) {
	String state = (String) mbs.getAttribute(myAppMBean, "State");	
	// alternatively, obtain a proxy object
	ApplicationMBean app = JMX.newMBeanProxy(mbs, myAppMBean, ApplicationMBean.class);
	state = app.getState();
}

Exemple 2 : Obtention de statistiques sur les temps de réponse pour le servlet "Example Servlet" de l'application "myApp"

import javax.management.ObjectName;
import javax.management.openmbean.CompositeData;
import javax.management.JMX;
import com.ibm.websphere.webcontainer.ServletStatsMXBean;

...

ObjectName servletMBean = new ObjectName("WebSphere:type=ServletStats,name=myApp.Example Servlet");
if (mbs.isRegistered(servletMBean)) {
	CompositeData responseTimeDetails = (CompositeData) mbs.getAttribute(servletMBean, "ResponseTimeDetails");
	CompositeData responseTimeReading = (CompositeData) responseTimeDetails.get("reading");
	Double mean = (Double) responseTimeReading.get("mean");
	Double standardDeviation = (Double) responseTimeReading.get("standardDeviation");
	// alternatively, obtain a proxy object
	ServletStatsMXBean servletStats = JMX.newMXBeanProxy(mbs, servletMBean, ServletStatsMXBean.class);
	StatisticsMeter meter = servletStats.getResponseTimeDetails();
	StatisticsReading reading = meter.getReading();
	mean = reading.getMean();
	standardDeviation = reading.getStandardDeviation();
}

Exemple 3 : Création d'un fichier de configuration de plug-in de serveur Web

import com.ibm.websphere.webcontainer.GeneratePluginConfigMBean;

...

ObjectName pluginMBean = new ObjectName("WebSphere:name=com.ibm.ws.jmx.mbeans.generatePluginConfig");
if (mbs.isRegistered(pluginMBean)) {
	mbs.invoke(pluginMBean, "generatePluginConfig", new Object[] {
    "installRoot", "serverName"}, new String[] {
    String.class.getName(), String.class.getName() 
  });
	// alternatively, use a proxy object
	GeneratePluginConfigMBean plugin = JMX.newMBeanProxy(mbs, name, GeneratePluginConfigMBean.class);
	plugin.generatePluginConfig("installRoot", "serverName");
}

Exemple 4 : Statut de la requête du noeud final de service Web

import javax.management.ObjectName;
import javax.management.MBeanServerConnection;
import javax.management.MBeanInfo;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanOperationInfo;

...

// Init mbs as needed
MBeanServerConnection mbs;

// Get MBeanInfo for specific ObjectName
ObjectName objName = new ObjectName("WebSphere:feature=jaxws,bus.id=testCXFJMXSupport-Server-Bus,
    type=Bus.Service.Endpoint,service=\"{http://jaxws.samples.ibm.com.jmx/}TestEndpointService\",
    port=\"TestEndpoint\",instance.id=1816106538");
MBeanInfo beanInfo = mbsc.getMBeanInfo(objName);

// Go through attributes to find the interested one
for (MBeanAttributeInfo attr : beanInfo.getAttributes()) {
	if (attr.getName().equals("State")) {
	    String status = String.valueOf(mbs.getAttribute(objName, attr.getName()));
		break;
	}
}

Exemple 5 : Arrêt du bus de serveur CXF

import javax.management.ObjectName;
import javax.management.MBeanServerConnection;
import javax.management.MBeanInfo;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanOperationInfo;

...

// Init mbsc as needed
MBeanServerConnection mbs;

// Get MBeanInfo for specific ObjectName
ObjectName objName = new ObjectName("WebSphere:feature=jaxws,bus.id=testCXFJMXSupport-Server-Bus,
    type=Bus,instance.id=1618108530");
MBeanInfo beanInfo = mbsc.getMBeanInfo(objName);

// Go through operation to find the interested one and invoke
for (MBeanOperationInfo operation : beanInfo.getOperations()) {
	if (operation.getName().equals("shutdown")) {
		mbs.invoke(objName, operation.getName(), new Object[] { true }, new String[] { boolean.class.getName() });
		break;
	}
}

Icône indiquant le type de rubrique Rubrique de référence



Icône d'horodatage Dernière mise à jour: Tuesday, 6 December 2016
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=rwlp_mbeans_operation
Nom du fichier : rwlp_mbeans_operation.html