bea.com | products | dev2dev | support | askBEA
 Download Docs   Site Map   Glossary 
Search

Programming WebLogic Management Services with JMX

 Previous Next Contents Index View as PDF  

Accessing Runtime Information

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.

This topic provides examples for retrieving and modifying runtime information about WebLogic Server domains and server instances:

 


Determining the Active Domain and Servers

The Administration MBeanHome interface includes APIs that you can use to determine the name of the currently active domain and the name of all server instances that are currently active.

The example class in Listing 5-1 does the following:

  1. Retrieves the Administration MBeanHome interface.
  2. Uses MBeanHome.getActiveDomain().getName() to retrieve the name of the domain.
  3. Uses the getMBeansByType method to retrieve the set of all ServerRuntime MBeans in the domain.
  4. Iterates through the set and compares the names of the ServerRuntimeMBean instances with the name of the WebLogic Server instance. If the instance is active, it prints the name of the server.

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 the WebLogic Server Administration Guide.

The code in this example must run on the Administration Server.

Listing 5-1 Determining the Active Domain and Servers

import java.util.Set;
import java.util.Iterator;
import java.rmi.RemoteException;
import javax.naming.*;
import weblogic.jndi.Environment;
import weblogic.management.MBeanHome;
import javax.management.ObjectName;
import weblogic.management.WebLogicMBean;
import weblogic.management.configuration.ServerMBean;
import weblogic.management.runtime.ServerRuntimeMBean;
import weblogic.management.WebLogicObjectName;
import weblogic.management.MBeanHome;
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";
ServerRuntimeMBean serverRuntime = null;
int count = 0;
   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();
System.out.println("got the IC");
//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 Severs: ");
mbeanSet = home.getMBeansByType("ServerRuntime");
mbeanIterator = mbeanSet.iterator();
while(mbeanIterator.hasNext()) {
serverRuntime = (ServerRuntimeMBean)mbeanIterator.next();
//printing the names of active servers
if(serverRuntime.getState().equals("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: " + count);
}
}

Using weblogic.Admin to Determine Active Domains and Servers

While you can use the example code in Listing 5-1 to determine active domains and servers from a JMX application, you can use the weblogic.Admin utility to accomplish a similar task from the command line or a script.

The following command returns the name of the currently active domain, where peach hosts the domain's Administration Server and weblogic is the name and password of a user who has permission to view MBean attributes:

java weblogic.Admin -url peach: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="examplesDomain:Location=peach,Name=examplesDomain,ServerRuntime=peach,Type=DomainRuntime"{Name=examplesDomain}}

To see a list of all server instances that are currently active, you use ask the Administration Server to retrieve all ServerRuntime MBeans that are registered in its Administration MBeanHome interface. (Only active server instances register ServerRuntime MBeans with the Administration MBeanHome interface.)

You must specify the -adminurl argument to instruct the GET command to use the Administration Server's Administration MBeanHome interface:

java weblogic.Admin -adminurl peach:8001 -username weblogic -password weblogic GET -type ServerRuntime -property State

The command output includes the WebLogicObjectName of all ServerRuntime MBeans and the value of each State attribute:

---------------------------
MBeanName: "MedRec:Location=MedRecMS2,Name=MedRecMS2,Type=ServerRuntime"
State: RUNNING
---------------------------
MBeanName: "MedRec:Location=MedRecServer,Name=MedRecServer,Type=ServerRuntime"
State: RUNNING
---------------------------
MBeanName: "MedRec:Location=MedRecMS1,Name=MedRecMS1,Type=ServerRuntime"
State: RUNNING

 


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 change the lifecycle state of a server. (For information about server states, refer to "Server Lifecycle" in the Configuring and Managing WebLogic Server guide.)

This section provides examples of finding ServerRuntimeMBean and using it to change the state of a server instance. 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 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 Runtime MBeans that reside on the current WebLogic Server. If you invoke MBeanHome.getRuntimeMBean()on the Administration Server, it returns only the Runtime MBeans that are on the Administration Server.

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 the WebLogic Server Administration Guide.

Listing 5-2 Using a Local MBeanHome and getRuntimeMBean()

import java.util.Set;
import java.util.Iterator;
import java.rmi.RemoteException;
import javax.naming.*;
import weblogic.jndi.Environment;
import weblogic.management.MBeanHome;
import javax.management.ObjectName;
import weblogic.management.WebLogicMBean;
import weblogic.management.configuration.ServerMBean;
import weblogic.management.runtime.ServerRuntimeMBean;
import weblogic.management.WebLogicObjectName;
public class serverRuntimeInfo1 {
  public static void main(String[] args) {
    MBeanHome home = null;
//domain variables
String url = "t3://localhost:7003";
String serverName = "Server1";
   String username = "weblogic";
String password = "weblogic";
   ServerRuntimeMBean serverRuntime = null;
ServerRuntimeMBean serverRuntimeM = 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("weblogic.management.home." + serverName);
System.out.println("Got the MBeanHome: " + home + " for server: " +
      serverName);
} catch (Exception e) {
System.out.println("Exception caught: " + 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);
} catch (javax.management.InstanceNotFoundException e) {
System.out.println("Caught exception: " + e);
}
    System.out.println("Current state: " + serverRuntime.getState() );
System.out.println("Suspending the server ...");
serverRuntime.suspend();
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() );
}
}

Using the Administration MBeanHome and getMBeansByType()

Like the example in Listing 5-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.

This example class uses MBeanHome.getMBeansByType method to retrieve the set of all ServerRuntime MBeans in the domain. It then iterates through the set and compares the names of the ServerRuntimeMBean instances with the name of a WebLogic Server instance. When it finds a specific server instance, the class changes the state of the server to SHUTDOWN.

In the following example, 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 the WebLogic Server Administration Guide.

Listing 5-3 Using the Administration MBeanHome and getMBeansByType()

import java.util.Set;
import java.util.Iterator;
import java.rmi.RemoteException;
import javax.naming.*;
import weblogic.jndi.Environment;
import weblogic.management.MBeanHome;
import javax.management.ObjectName;
import weblogic.management.WebLogicMBean;
import weblogic.management.configuration.ServerMBean;
import weblogic.management.runtime.ServerRuntimeMBean;
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";
   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);
}
    /* Here we use the getMBeansByType method to get the set of ServerRuntime mbeans
* Then we iterate through the set. We retrieve the ServerRuntimeMbean we are
* interested in by comparing the name to the value of serverName.
*/
  try { 
mbeanSet = home.getMBeansByType("ServerRuntime");
mbeanIterator = mbeanSet.iterator();
while(mbeanIterator.hasNext()) {
serverRuntime = (ServerRuntimeMBean)mbeanIterator.next();
if(serverRuntime.getName().equals(serverName)) {
System.out.println("we have got the serverRuntimembean: " + serverRuntime +
" for: " + serverName);
System.out.println("Current state: " + serverRuntime.getState() );
System.out.println("Suspending the server ...");
System.out.println("Stopping the server ...");
//changing the state to SHUTDOWN
serverRuntime.shutdown();
System.out.println("Current state: " + serverRuntime.getState() );
} catch (javax.management.InstanceNotFoundException 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 WebLogicObjectNames for WebLogic Server MBeans.

To make sure that you supply the correct object name, you can 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 named Server1:

java weblogic.Admin -url http://Server1: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 Command Line Reference.

In Listing 5-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.

Listing 5-4 Using the Administration MBeanHome and getMBean()

import java.util.Set;
import java.util.Iterator;
import java.rmi.RemoteException;
import javax.naming.*;
import weblogic.jndi.Environment;
import weblogic.management.MBeanHome;
import javax.management.ObjectName;
import weblogic.management.WebLogicMBean;
import weblogic.management.configuration.ServerMBean;
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";
   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 Administration MBeanHome.
* Note: Looking up MBeanHome.ADMIN_JNDI_NAME returns the Administration
* MBeanHome interface. It provides access to all MBeans in the domain.
* Looking up "weblogic.management.home.<AdminServerName>" returns the
* local MBeanHome for the Administration Server. It provides
* to the Configuration and Runtime MBeans on the Administration Server.
*/
home = (MBeanHome) ctx.lookup(MBeanHome.ADMIN_JNDI_NAME);
System.out.println("Got the Admin MBeanHome: " + home + " from the
      Admin server");
} catch (Exception e) {
System.out.println("Exception caught: " + e);
}
  try {
     /* Creating the mbean object name.
* The serverName refers to the name of the Managed Server that hosts
* the ServerRuntimeMBean.
*/
String name = "mihirDomain:Location=" + serverName + ",Name=" +
      serverName + ",Type=ServerRuntime" ;
WebLogicObjectName objName = new WebLogicObjectName(name);
System.out.println("Created WebLogicObjectName: " + name);
    serverRuntime = (ServerRuntimeMBean)home.getMBean(objName);
System.out.println("Got the serverRuntime using the adminHome: " +
serverRuntime );
} catch(Exception e) {
System.out.println("Exception: " + e);
}
    System.out.println("Current state: " + serverRuntime.getState() );
System.out.println("Suspending the server ...");
serverRuntime.suspend();
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() );
  } 
}

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.

Listing 5-5 Using the Administration MBeanHome and getMBean()

import java.util.Set;
import java.util.Iterator;
import java.rmi.RemoteException;
import javax.naming.*;
import javax.management.ObjectName;
import javax.management.MBeanServer;
import weblogic.jndi.Environment;
import weblogic.management.MBeanHome;
import weblogic.management.WebLogicMBean;
import weblogic.management.runtime.ServerRuntimeMBean;
import weblogic.management.WebLogicObjectName;
public class serverRuntimeInfo3 {
  public static void main(String[] args) {
    MBeanHome home = null;
//domain variables
   String url = "t3://adminserver:7001";
String serverName = "Server1";
String username = "weblogic";
String password = "weblogic";
String ListenPort = "7001";
   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 Administration MBeanHome.
home = (MBeanHome) ctx.lookup(MBeanHome.ADMIN_JNDI_NAME);
System.out.println("Got the Admin MBeanHome: " + home + " from the
      Admin server");
} catch (Exception e) {
System.out.println("Exception caught: " + e);
}
  try {
     /* Creating the mbean object name.
* The serverName refers to the name of the Managed Server that hosts
* the ServerRuntimeMBean.
*/
String name = "mihirDomain:Location=" + serverName + ",Name=" +
      serverName + ",Type=ServerRuntime" ;
WebLogicObjectName objName = new WebLogicObjectName(name);
System.out.println("Created WebLogicObjectName: " + name);
    //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:

  1. Retrieves the Administration MBeanHome, which runs on the Administration Server and can provide access to all ClusterRuntimeMBeans in the domain.
  2. Retrieves all ClusterRuntimeMBeans and determines whether they belong to a specific domain.
  3. Finds one ClusterRuntimeMBean for a Managed Server in the domain of interest.
  4. Uses the ClusterRuntimeMBean APIs on the Managed Server to determine the number and name of active servers in the cluster.

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 the WebLogic Server Administration Guide.

Listing 5-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.*;
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);
} catch (Exception e) {
System.out.println("Exception caught: " + e);
}
// Retrieving a list of ClusterRuntime MBeans in the domain.
try {
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]);
}
}
}

 

Back to Top Previous Next