You can connect to MBeans with Java applications. These applications use the interfaces in the com.ibm.websphere.objectgrid.management package.
The following example program connects to a stand-alone catalog service MBean server and returns an XML formatted string that lists each container server along with its allocated shards for a given ObjectGrid and MapSet.
package com.ibm.websphere.sample.xs.admin;
import java.util.Set;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
/**
* Collects the placement information from the Catalog Server for a given ObjectGrid.
*/
public final class CollectPlacementPlan {
private static String hostName = "localhost";
private static int port = 1099;
private static String objectGridName = "library";
private static String mapSetName = "ms1";
/**
* Connects to the ObjectGrid Catalog Service to retrieve placement information and
* prints it out.
*
* @param args
* @throws Exception
* If there is a problem connecting to the catalog service MBean server.
*/
public static void main(String[] args) throws Exception {
String serviceURL = "service:jmx:rmi:///jndi/rmi://" + hostName + ":" + port +
"/objectgrid/MBeanServer";
JMXServiceURL jmxUrl = new JMXServiceURL(serviceURL);
JMXConnector jmxCon = JMXConnectorFactory.connect(jmxUrl);
try {
MBeanServerConnection catalogServerConnection = jmxCon.getMBeanServerConnection();
Set placementSet = catalogServerConnection.queryNames(new
ObjectName("com.ibm.websphere.objectgrid"
+ ":*,type=PlacementService"), null);
ObjectName placementService = (ObjectName) placementSet.iterator().next();
Object placementXML = catalogServerConnection.invoke(placementService,
"listObjectGridPlacement", new Object[] {
objectGridName, mapSetName }, new String[] { String.class.getName(),
String.class.getName() });
System.out.println(placementXML);
} catch (Exception e) {
if(jmxCon != null) {
jmxCon.close();
}
}
}
}
<objectGrid name="library" mapSetName="ms1">
<container name="Container-1" zoneName="DefaultZone"
hostName="myhost.mycompany.com" serverName="ogserver">
<shard type="SynchronousReplica" partitionName="0"/>
<shard type="SynchronousReplica" partitionName="1"/>
<shard type="SynchronousReplica" partitionName="2"/>
<shard type="SynchronousReplica" partitionName="3"/>
</container>
<container name="Container-0" zoneName="DefaultZone"
hostName="myhost.mycompany.com" serverName="ogserver">
<shard type="Primary" partitionName="0"/>
<shard type="Primary" partitionName="1"/>
<shard type="Primary" partitionName="2"/>
<shard type="Primary" partitionName="3"/>
</container>
<container name="library:ms1:_UnassignedContainer_" zoneName="_ibm_SYSTEM"
hostName="UNASSIGNED" serverName="UNNAMED">
<shard type="SynchronousReplica" partitionName="0"/>
<shard type="SynchronousReplica" partitionName="1"/>
<shard type="SynchronousReplica" partitionName="2"/>
<shard type="SynchronousReplica" partitionName="3"/>
<shard type="AsynchronousReplica" partitionName="0"/>
<shard type="AsynchronousReplica" partitionName="1"/>
<shard type="AsynchronousReplica" partitionName="2"/>
<shard type="AsynchronousReplica" partitionName="3"/>
</container>
</objectGrid>
Container servers host MBeans to query information about the individual maps and ObjectGrid instances that are running within the container server. The following example program prints the status of each container server that is hosted by the catalog server with the JMX address of localhost:1099:
package com.ibm.websphere.sample.xs.admin;
import java.util.List;
import java.util.Set;
import javax.management.MBeanServerConnection;
import javax.management.ObjectInstance;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
/**
* Collects placement status from each of the available containers directly.
*/
public final class CollectContainerStatus {
private static String hostName = "localhost";
private static int port = 1099;
/**
* @param args
*/
public static void main(String[] args) throws Exception {
String serviceURL = "service:jmx:rmi:///jndi/rmi://" + hostName + ":" + port + "/objectgrid/MBeanServer";
JMXServiceURL jmxUrl = new JMXServiceURL(serviceURL);
JMXConnector jmxCon = JMXConnectorFactory.connect(jmxUrl);
try {
MBeanServerConnection catalogServerConnection = jmxCon.getMBeanServerConnection();
Set placementSet = catalogServerConnection.queryNames(new ObjectName("com.ibm.websphere.objectgrid"
+ ":*,type=PlacementService"), null);
ObjectName placementService = (ObjectName) placementSet.iterator().next();
List<String> containerJMXAddresses = (List<String>) catalogServerConnection.invoke(placementService,
"retrieveAllServersJMXAddresses", new Object[0], new String[0]);
for (String address : containerJMXAddresses) {
JMXServiceURL containerJMXURL = new JMXServiceURL(address);
JMXConnector containerConnector = JMXConnectorFactory.connect(containerJMXURL);
MBeanServerConnection containerConnection = containerConnector.getMBeanServerConnection();
Set<ObjectInstance> containers = containerConnection.queryMBeans(
new ObjectName("*:*,type=ObjectGridContainer"), null);
for (ObjectInstance container : containers) {
System.out.println(containerConnection.getAttribute(container.getObjectName(), "Status"));
}
}
} finally {
if(jmxCon != null) {
jmxCon.close();
}
}
}
}
<container name="Container-0" zoneName="DefaultZone" hostName="descartes.rchland.ibm.com"
serverName="ogserver">
<shard type="Primary" partitionName="1"/>
<shard type="Primary" partitionName="0"/>
<shard type="Primary" partitionName="3"/>
<shard type="Primary" partitionName="2"/>
</container>
The method for programmatically accessing MBeans in WebSphere Application Server is slightly different from accessing MBeans in a stand-alone configuration.
package com.ibm.websphere.sample.xs.admin;
import java.util.Set;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
/**
* Collects the placement information from the catalog server running in a deployment manager for a given ObjectGrid.
*/
public final class CollectPlacementPlanWAS {
private static String hostName = "localhost";
private static int port = 9809;
private static String objectGridName = "library";
private static String mapSetName = "ms1";
/**
* Connects to the catalog service to retrieve placement information and prints it out.
*
* @param args
* @throws Exception
* If there is a problem connecting to the catalog service MBean server.
*/
public static void main(String[] args) throws Exception {
// connect to bootstrap port of the deployment manager
String serviceURL = "service:jmx:iiop://" + hostName + ":" + port + "/jndi/JMXConnector";
JMXServiceURL jmxUrl = new JMXServiceURL(serviceURL);
JMXConnector jmxCon = JMXConnectorFactory.connect(jmxUrl);
try {
MBeanServerConnection catalogServerConnection = jmxCon.getMBeanServerConnection();
Set placementSet =
catalogServerConnection.queryNames(new ObjectName("com.ibm.websphere.objectgrid"
+ ":*,type=PlacementService"), null);
ObjectName placementService = (ObjectName) placementSet.iterator().next();
Object placementXML = catalogServerConnection.invoke(placementService,
"listObjectGridPlacement", new Object[] {
objectGridName, mapSetName }, new String[] { String.class.getName(),
String.class.getName() });
System.out.println(placementXML);
} finally {
if(jmxCon != null) {
jmxCon.close();
}
}
}
}
"$JAVA_HOME/bin/java" "$WAS_LOGGING" -Djava.security.auth.login.config="$app_server_root/properties/wsjaas_client.conf" \
-Djava.ext.dirs="$JAVA_HOME/jre/lib/ext:$WAS_EXT_DIRS:$WAS_HOME/plugins:$WAS_HOME/lib/WMQ/java/lib" \
-Djava.naming.provider.url=<an_IIOP_URL_or_a_corbaloc_URL_to_your_application_server_machine_name> \
-Djava.naming.factory.initial=com.ibm.websphere.naming.WsnInitialContextFactory \
-Dserver.root="$WAS_HOME" "$CLIENTSAS" "$CLIENTSSL" $USER_INSTALL_PROP \
-classpath "$WAS_CLASSPATH":<list_of_your_application_jars_and_classes> \
<fully_qualified_class_name_to_run> <your_application_parameters>
This
command assumes that the was_root/bin/setupCmdLine.sh script
has been run to set the variables properly. An example of the format
of the java.naming.provider.url property value is corbaloc:iiop:1.0@<host>:<port>/NameService.For more information about connecting to the catalog service MBean with security enabled, see Java Management Extensions (JMX) security.
You can also find more information about MBeans in the com.ibm.websphere.objectgrid.management package.