![]() |
![]() |
e-docs > WebLogic Server > Programming WebLogic Management Services with JMX > Accessing Runtime Information |
Programming WebLogic Management Services with JMX
|
WebLogic Server includes a large number of MBeans that provide information about the runtime state of managed resources. If you want to create applications that view and modify this runtime data, you must first use the MBeanServer interface or the WebLogic Server type-safe interface to retrieve Runtime MBeans. Then you use APIs in the weblogic.management.runtime package to view or change the runtime data. For information about viewing the API documentation, refer to Documentation for Runtime MBean APIs.
The following sections provide examples for retrieving and modifying runtime information about WebLogic Server domains and server instances:
Example: Determining the Active Domain and Servers
The MBeanHome interface includes APIs that you can use to determine the name of the currently active domain and the name of a server instance. You can use this information in other APIs that take the domain name or a server name as arguments.
The example class in Listing 4-1 does the following:
While this example uses the Administration MBeanHome, you can also use a Local MBeanHome interface. A Local MBeanHome interface can retrieve only the name of the current domain and server instance. The Administration MBeanHome interface can retrieve the names of all currently active servers in the domain.
In the following example, weblogic is the username and password for a user who has permission to view and modify MBean attributes. For information about permissions to modify MBeans, refer to "Protecting System Administration Operations" in WebLogic Server Administration Guide.
Listing 4-1 Determining the Active Domain and Servers
import java.util.Set;
import java.util.Iterator;
import javax.naming.Context;
import weblogic.jndi.Environment;
import weblogic.management.MBeanHome;
import weblogic.management.runtime.ServerRuntimeMBean;
public class getActiveDomainAndServers {
public static void main(String[] args) {
MBeanHome home = null;
//url of the Administration Server
String url = "t3://localhost:7001";
String username = "weblogic";
String password = "weblogic";
//setting the initial context
try {
Environment env = new Environment();
env.setProviderUrl(url);
env.setSecurityPrincipal(username);
env.setSecurityCredentials(password);
Context ctx = env.getInitialContext();
//getting the Administration MBeanHome
home = (MBeanHome) ctx.lookup(MBeanHome.ADMIN_JNDI_NAME);
} catch (Exception e) {
System.out.println("Exception caught: " + e);
}
//getting the name of the active domain
try {
System.out.println("Active Domain: " +
home.getActiveDomain().getName() );
} catch (Exception e) {
System.out.println("Exception: " + e);
}
//getting the names of servers in the domain
System.out.println("Active Servers: ");
Set mbeanSet = home.getMBeansByType("ServerRuntime");
Iterator mbeanIterator = mbeanSet.iterator();
while(mbeanIterator.hasNext()) {
ServerRuntimeMBean serverRuntime =
(ServerRuntimeMBean)mbeanIterator.next();
//printing the names of active servers
if(serverRuntime.getState().equalsIgnoreCase("RUNNING")) {
System.out.println("Name: " + serverRuntime.getName());
System.out.println("ListenAddress: " +
serverRuntime.getListenAddress());
System.out.println("ListenPort: " +
serverRuntime.getListenPort());
//count++;
}
}
System.out.println("Number of servers active in the domain: " +
mbeanSet.size());
}
}
Using weblogic.Admin to Determine Active Domains and Servers
While you can compile and run the example code in Listing 4-1 to determine active domains and servers, you can use the weblogic.Admin utility to accomplish a similar task without having to compile Java classes.
The following command returns the name of the currently active domain, where AdminServer is the domain's Administration Server, MyHost is the Administration Server's host computer, and weblogic is the name and password of a user who has permission to view MBean attributes:
java weblogic.Admin -url MyHost:8001 -username weblogic -password
weblogic GET -type DomainRuntime -property Name
The command output includes the WebLogicObjectName of the DomainRuntimeMBean and the value of its Name attribute:
{MBeanName="myDomain:Location=AdminServer,Name=myDomain,ServerRun
time=AdminServer,Type=DomainRuntime"{Name=myDomain}}
Example: Viewing and Changing the Runtime State of a WebLogic Server Instance
The weblogic.management.runtime.ServerRuntimeMBean interface provides runtime information about a WebLogic Server instance. For example, it indicates which listen ports and addresses a server is using. It also includes operations that can gracefully or forcefully shut down a server.
This section provides examples of finding ServerRuntimeMBean and using it to gracefully shut down a server instance. When you initiate a graceful shutdown, the server notifies subsystems to complete all in-work requests. After the subsystems complete their work, the server stops.
Each example illustrates a different way of retrieving ServerRuntimeMBean:
You cannot use the weblogic.Admin utility to change the value of Runtime MBean attributes.
Using a Local MBeanHome and getRuntimeMBean()
Each WebLogic Server instance hosts its own MBeanHome interface, which provides access to the Local Configuration and Runtime MBeans on the server instance. As opposed to using the Administration MBeanHome interface, using the local MBeanHome saves you the trouble of filtering Runtime MBeans to find those that apply to the current server. It also uses fewer network hops to access MBeans, because you are connecting directly to the server (instead of routing requests through the Administration Server).
The MBeanHome interface includes the getRuntimeMBean() method, which returns only the top-level Runtime MBeans that reside on the current WebLogic Server (see Walking the Hierarchy of Local Configuration and Runtime MBeans). If you invoke MBeanHome.getRuntimeMBean()on the Administration Server, it returns only the Runtime MBeans that manage and monitor the Administration Server.
The example class in Listing 4-2 does the following:
In the following example, weblogic is the username and password for a user who has permission to view and modify MBean attributes and Server1 is the name of the WebLogic Server instance for which you want to view and change status. For information about permissions to modify MBeans, refer to "Protecting System Administration Operations" in WebLogic Server Administration Guide.
Listing 4-2 Using a Local MBeanHome and getRuntimeMBean()
import javax.naming.Context;
import weblogic.jndi.Environment;
import weblogic.management.MBeanHome;
import weblogic.management.runtime.ServerRuntimeMBean;
public class serverRuntime1 {
public static void main(String[] args) {
MBeanHome home = null;
//domain variables
String url = "t3://ServerHost:7001";
String serverName = "Server1";
String username = "weblogic";
String password = "weblogic";
ServerRuntimeMBean serverRuntime = null;
//setting the initial context
try {
Environment env = new Environment();
env.setProviderUrl(url);
env.setSecurityPrincipal(username);
env.setSecurityCredentials(password);
Context ctx = env.getInitialContext();
//getting the local MBeanHome
home = (MBeanHome) ctx.lookup(MBeanHome.LOCAL_JNDI_NAME);
System.out.println("Got the MBeanHome: " + home + " for server: " +
serverName);
} catch (Exception e) {
System.out.println("Caught exception: " + e);
}
// Here we use the getRuntimeMBean method to access the
//ServerRuntimeMbean of the server instance.
try {
serverRuntime =
(ServerRuntimeMBean)home.getRuntimeMBean
(serverName,"ServerRuntime");
System.out.println("Got serverRuntimeMBean: " + serverRuntime);
//Using ServerRuntimeMBean to retrieve and change state.
System.out.println("Current state: " + serverRuntime.getState() );
} catch (javax.management.InstanceNotFoundException e) {
System.out.println("Caught exception: " + e);
}
try{
serverRuntime.shutdown();
System.out.println("Current state: " + serverRuntime.getState() );
} catch (weblogic.server.ServerLifecycleException e) {
System.out.println("Caught exception: " + e);
}
}
}
Using the Administration MBeanHome and getMBeansByType()
Like the example in Listing 4-1, the example class in this section uses the Administration MBeanHome interface to retrieve a ServerRuntime MBean. The Administration MBeanHome provides a single access point for all MBeans in the domain, but it requires you to either construct the WebLogicObjectName of the MBean you want to retrieve or to filter MBeans to find those that apply to a specific current server.
The example class in Listing 4-3 does the following:
In the following example, weblogic is the username and password for a user who has permission to change the state of servers, and Server1 is the name of the WebLogic Server instance for which you want to view and change status. For information about permissions to modify MBeans, refer to "Protecting System Administration Operations" in WebLogic Server Administration Guide.
Listing 4-3 Using the Administration MBeanHome and getMBeansByType()
import java.util.Set;
import java.util.Iterator;
import javax.naming.Context;
import weblogic.jndi.Environment;
import weblogic.management.MBeanHome;
import weblogic.management.runtime.ServerRuntimeMBean;
public class serverRuntimeInfo {
public static void main(String[] args) {
MBeanHome home = null;
//domain variables
String url = "t3://localhost:7001";
String serverName = "Server1";
String username = "weblogic";
String password = "weblogic";
ServerRuntimeMBean serverRuntime = null;
Set mbeanSet = null;
Iterator mbeanIterator = null;
//Setting the initial context
try {
Environment env = new Environment();
env.setProviderUrl(url);
env.setSecurityPrincipal(username);
env.setSecurityCredentials(password);
Context ctx = env.getInitialContext();
// Getting the Administration MBeanHome.
home = (MBeanHome) ctx.lookup(MBeanHome.ADMIN_JNDI_NAME);
System.out.println("Got the Admin MBeanHome: " + home );
} catch (Exception e) {
System.out.println("Exception caught: " + e);
}
// Using the getMBeansByType method to get the set of
//ServerRuntime mbeans.
try {
mbeanSet = home.getMBeansByType("ServerRuntime");
mbeanIterator = mbeanSet.iterator();
// Comparing the name of the server in each ServerRutime
// MBean to the value specified by serverName
while(mbeanIterator.hasNext()) {
serverRuntime = (ServerRuntimeMBean)mbeanIterator.next();
if(serverRuntime.getName().equals(serverName)) {
System.out.println("Found the serverRuntimembean: " +
serverRuntime + " for: " + serverName);
System.out.println("Current state: " +
serverRuntime.getState() );
System.out.println("Stopping the server ...");
serverRuntime.shutdown();
System.out.println("Current state: " +
serverRuntime.getState() );
}
}
} catch (Exception e) {
System.out.println("Caught exception: " + e);
}
}
}
Using the Administration MBeanHome and getMBean()
Instead of retrieving a list of all MBeans and then filtering the list to find the ServerRuntimeMBean for a specific server, this example uses the MBean naming conventions to construct the WebLogicObjectName for the ServerRuntimeMBean on a server instance named Server1. For information about constructing a WebLogicObjectName, refer to Using WebLogicObjectNames for WebLogic Server MBeans.
To make sure that you supply the correct object name, use the weblogic.Admin GET command. For example, the following command returns the object name and list of attributes of the ServerRuntimeMBean for a server instance that runs on a host computer named MyHost:
java weblogic.Admin -url http://MyHost:7001 -username weblogic
-password weblogic GET -pretty -type ServerRuntime
For more information about using the weblogic.Admin utility to find information about MBeans, refer to "MBean Management Command Reference" in the WebLogic Server Administration Guide.
In Listing 4-4, weblogic is the username and password for a user who has permission to view and modify MBean attributes, Server1 is the name of the WebLogic Server instance for which you want to view and change status, and mihirDomain is the name of the WebLogic Server administration domain. For information about permissions to modify MBeans, refer to "Protecting System Administration Operations" in WebLogic Server Administration Guide.
Listing 4-4 Using the Administration MBeanHome and getMBean()
import java.util.Set;
import java.util.Iterator;
import javax.naming.Context;
import weblogic.jndi.Environment;
import weblogic.management.MBeanHome;
import weblogic.management.runtime.ServerRuntimeMBean;
import weblogic.management.WebLogicObjectName;
public class serverRuntimeInfo2 {
public static void main(String[] args) {
MBeanHome home = null;
//domain variables
String url = "t3://localhost:7001";
String serverName = "Server1";
String username = "weblogic";
String password = "weblogic";
String domain = "examples";
ServerRuntimeMBean serverRuntime = null;
//setting the initial context
try {
Environment env = new Environment();
env.setProviderUrl(url);
env.setSecurityPrincipal(username);
env.setSecurityCredentials(password);
Context ctx = env.getInitialContext();
home = (MBeanHome) ctx.lookup(MBeanHome.ADMIN_JNDI_NAME);
System.out.println("Got Admin MBeanHome from the Admin server: "
+ home);
} catch (Exception e) {
System.out.println("Exception caught: " + e);
}
try {
WebLogicObjectName objName = new WebLogicObjectName(serverName,
"ServerRuntime",home.getDomainName(),serverName);
System.out.println("Created WebLogicObjectName: " + objName);
serverRuntime = (ServerRuntimeMBean)home.getMBean(objName);
System.out.println("Got the serverRuntime using the adminHome: " +
serverRuntime );
System.out.println("Current state: " + serverRuntime.getState() );
System.out.println("Stopping the server ...");
//changing the state to SHUTDOWN
serverRuntime.shutdown();
System.out.println("Current state: " + serverRuntime.getState() );
} catch(Exception e) {
System.out.println("Exception: " + e);
}
}
}
Using the MBeanServer Interface
The example in this section uses a standard JMX approach for interacting with MBeans. It uses the Administration MBeanHome interface to retrieve the javax.management.MBeanServer interface and then uses MBeanServer to retrieve the value of the ListenPort attribute of the ServerRuntimeMBean for a server instance named Server1.
In the following example, weblogic is the username and password for a user who has permission to view and modify MBean attributes and mihirDomain is the name of the WebLogic Server administration domain. For information about permissions to modify MBeans, refer to "Protecting System Administration Operations" in WebLogic Server Administration Guide.
Listing 4-5 Using the Administration MBeanHome and getMBean()
import java.util.Set;
import java.util.Iterator;
import javax.naming.Context;
import javax.management.MBeanServer;
import weblogic.jndi.Environment;
import weblogic.management.MBeanHome;
import weblogic.management.WebLogicObjectName;
public class serverRuntimeInfo3 {
public static void main(String[] args) {
MBeanHome home = null;
//domain variables
String url = "t3://localhost:7001";
String serverName = "Server1";
String username = "weblogic";
String password = "weblogic";
Object attributeValue = null;
MBeanServer homeServer = null;
//setting the initial context
try {
Environment env = new Environment();
env.setProviderUrl(url);
env.setSecurityPrincipal(username);
env.setSecurityCredentials(password);
Context ctx = env.getInitialContext();
// Getting the Administration MBeanHome.
home = (MBeanHome) ctx.lookup(MBeanHome.ADMIN_JNDI_NAME);
System.out.println("Got Admin MBeanHome from the Admin server: " +
home);
} catch (Exception e) {
System.out.println("Exception caught: " + e);
}
try {
// Creating the mbean object name.
WebLogicObjectName objName = new WebLogicObjectName(serverName,
"ServerRuntime",home.getDomainName(),serverName);
System.out.println("Created WebLogicObjectName: " + objName);
//Retrieving the MBeanServer interface
homeServer = home.getMBeanServer();
//Retrieving the ListenPort attribute of ServerRuntimeMBean
attributeValue = homeServer.getAttribute(objName, "ListenPort");
System.out.println("ListenPort for " + serverName + " is:" +
attributeValue);
} catch(Exception e) {
System.out.println("Exception: " + e);
}
}
}
Example: Viewing Runtime Information About Clusters
The example in this section retrieves the number and names of WebLogic Server instances currently running in a cluster. It uses weblogic.management.runtime.ClusterRuntimeMBean, which provides information about a single Managed Server's view of the members of a WebLogic cluster.
Only Managed Servers host instances of ClusterRuntimeMBean, and you must retrieve the ClusterRuntimeMBean instance from a Managed Server that is actively participating in a cluster.
To make sure that it retrieves a ClusterRuntimeMBean from an active Managed Server that is in a cluster, this example does the following:
In the example, weblogic is the username and password for a user who has permission to view and modify MBean attributes. For information about permissions to modify MBeans, refer to "Protecting System Administration Operations" in WebLogic Server Administration Guide.
Listing 4-6 Retrieving a List of Servers Running in a Cluster
import java.util.Set;
import java.util.Iterator;
import java.rmi.RemoteException;
import javax.naming.Context;
import weblogic.jndi.Environment;
import weblogic.management.MBeanHome;
import javax.management.ObjectName;
import weblogic.management.WebLogicMBean;
import weblogic.management.runtime.ClusterRuntimeMBean;
import weblogic.management.WebLogicObjectName;
import weblogic.management.MBeanHome;
public class getRunningServersInCluster {
public static void main(String[] args) {
MBeanHome home = null;
//domain variables
String url = "t3://localhost:7001"; //url of the Administration Server
/* If you have more than one cluster in your domain, define a list of
* all the servers in the cluster. You compare the servers in the domain
* with this list to determine which servers are in a specific cluster.
*/
String server1 = "cs1"; // name of server in the cluster
String server2 = "cs2"; // name of server in the cluster
String username = "weblogic";
String password = "weblogic";
ClusterRuntimeMBean clusterRuntime = null;
Set mbeanSet = null;
Iterator mbeanIterator = null;
String name = "";
String[] aliveServerArray = null;
//Setting the initial context
try {
Environment env = new Environment();
env.setProviderUrl(url);
env.setSecurityPrincipal(username);
env.setSecurityCredentials(password);
Context ctx = env.getInitialContext();
// Getting the Administration MBeanHome.
home = (MBeanHome) ctx.lookup(MBeanHome.ADMIN_JNDI_NAME);
// Retrieving a list of ClusterRuntime MBeans in the domain.
mbeanSet = home.getMBeansByType("ClusterRuntime");
mbeanIterator = mbeanSet.iterator();
while(mbeanIterator.hasNext()) {
// Retrieving one ClusterRuntime MBean from the list.
clusterRuntime = (ClusterRuntimeMBean)mbeanIterator.next();
// Getting the name of the ClusterRuntime MBean.
name = clusterRuntime.getName();
// Determining if the current ClusterRuntimeMBean belongs to a
// server in the cluster of interest.
if(name.equals(server1) || name.equals(server2) ) {
// Using the current ClusterRuntimeMBean to retrieve the
// number of servers in the cluster.
System.out.println("\nNumber of active servers in the
cluster: " + clusterRuntime.getAliveServerCount());
// Retrieving the names of servers in the cluster.
aliveServerArray = clusterRuntime.getServerNames();
break;
}
}
} catch (Exception e) {
System.out.println("Caught exception: " + e);
}
if(aliveServerArray == null) {
System.out.println("\nThere are no running servers in the cluster");
System.exit(1);
}
System.out.println("\nThe running servers in the cluster are: ");
for (int i=0; i < aliveServerArray.length; i++) {
System.out.println("server " + i + " : " + aliveServerArray[i]);
}
}
}
Viewing Runtime Information for EJBs
For each EJB that you deploy on a server instance, WebLogic Server instantiates MBean types from the weblogic.management.runtime package (see Table 4-1). For more information about the MBeans in the weblogic.management.runtime package, refer to the WebLogic Server Javadoc.
WebLogic Server provides an additional, abstract interface, EJBRuntimeMBean, which contains methods that the other EJB runtime MBeans use.
EJB runtime MBeans are instantiated within a hierarchy. For example:
EntityEJBRuntimeMBean is always the parent of the EJBCacheRuntimeMBean, EJBTransactionRuntimeMBean, and EJBPoolRuntimeMBean MBeans. The fourth child MBean, EJBLockingRuntimeMBean, is only created if the entity bean uses an exclusive concurrency strategy (which is configured in the weblogic-ejb-jar.xml deployment descriptor).
Depending on the type of runtime data that you retrieve, you typically also need to retrieve the name of any parent MBeans to provide context for the data. For example, if you retrieve the value of EJBTransactionRuntimeMBean.TransactionsRolledBackTotalCount, you also retrieve the name of the parent EJBEntityRuntimeMBean to determine which entity bean the value comes from.
Figure 4-1 illustrates the hierarchical relationships.
Figure 4-1 Hierarchy of EJB Runtime MBeans
Example: Retrieving Runtime Information for All Stateful and Stateless EJBs
To retrieve runtime information for all EJBs deployed in a domain, the example in Listing 4-7 does the following:
If you want to retrieve runtime information only for the EJBs that are deployed on a specific server instance, you can connect to the specific server instance and retrieve the local MBeanHome interface. For more information, refer to Example: Retrieving a Local MBeanHome from an Internal Client.
Listing 4-7 Viewing Runtime Information for EJBs
import java.util.Iterator;
import java.util.Set;
import javax.management.InstanceNotFoundException;
import javax.naming.Context;
import javax.naming.InitialContext;
import weblogic.management.MBeanHome;
import weblogic.management.WebLogicObjectName;
import weblogic.management.configuration.ApplicationMBean;
import weblogic.management.configuration.EJBComponentMBean;
import weblogic.management.configuration.ServerMBean;
import weblogic.management.runtime.EJBComponentRuntimeMBean;
import weblogic.management.runtime.EJBPoolRuntimeMBean;
import weblogic.management.runtime.EJBRuntimeMBean;
import weblogic.management.runtime.EJBTransactionRuntimeMBean;
import weblogic.management.runtime.StatelessEJBRuntimeMBean;
import weblogic.jndi.Environment;
public final class EJBMonitor {
private String url = "t3://localhost:7001";
private String user = "weblogic";
private String password = "weblogic";
private MBeanHome mBeanHome; // admin
public EJBMonitor() throws Exception {
Environment env = new Environment();
env.setProviderUrl(url);
env.setSecurityPrincipal(user);
env.setSecurityCredentials(password);
Context ctx = env.getInitialContext();
mBeanHome = (MBeanHome)ctx.lookup(MBeanHome.ADMIN_JNDI_NAME);
}
public void displayStatelessEJBPoolIdleCount()
throws Exception
{
int idleCount = 0;
String type = "StatelessEJBRuntime";
Set beans = mBeanHome.getMBeansByType(type);
System.out.println("Printing Stateless Session pool idle count:");
for(Iterator it=beans.iterator();it.hasNext();) {
StatelessEJBRuntimeMBean rt = (StatelessEJBRuntimeMBean)it.next();
displayEJBInfo(rt);
EJBPoolRuntimeMBean pool = rt.getPoolRuntime();
idleCount = pool.getIdleBeansCount();
}
System.out.println("Pool Idle Bean Count: "+ idleCount +"\n");
}
public void displayStatefulEJBTransactionRollbackPercentages()
throws Exception
{
String type = "StatefulEJBRuntime";
Set beans = mBeanHome.getMBeansByType(type);
System.out.println("Printing Stateful transaction rollback
percentages:");
for(Iterator it=beans.iterator();it.hasNext();) {
EJBRuntimeMBean rt = (EJBRuntimeMBean)it.next();
displayEJBInfo(rt);
EJBTransactionRuntimeMBean trans = rt.getTransactionRuntime();
String rollbackPercentage = "0";
long rollbackCount = trans.getTransactionsRolledBackTotalCount();
if(rollbackCount > 0) {
long totalTransactions = rollbackCount +
trans.getTransactionsCommittedTotalCount();
rollbackPercentage =
""+(float)rollbackCount/totalTransactions*100;
}
System.out.println("Transaction rollback percentage: "+
rollbackPercentage +"\n");
}
}
private void displayEJBInfo(EJBRuntimeMBean rt) throws Exception {
System.out.println("EJB Name: "+rt.getEJBName());
EJBComponentRuntimeMBean compRTMBean =
EJBComponentMBean compMBean = compRTMBean.getEJBComponent();
ApplicationMBean appMBean = (ApplicationMBean)compMBean.getParent();
System.out.println("Application Name: "+appMBean.getName());
System.out.println("Component Name: "+compMBean.getName());
WebLogicObjectName objName = rt.getObjectName();
System.out.println("Server Name: "+objName.getLocation());
}
public static void main(String[] argv) throws Exception {
EJBMonitor m = new EJBMonitor();
m.displayStatelessEJBPoolIdleCount();
m.displayStatefulEJBTransactionRollbackPercentages();
}
}
![]() |
![]() |
![]() |
![]() |
||
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |