This discussion is archived
3 Replies Latest reply: Jan 7, 2013 8:03 AM by Fabian RSS

Printing the Name and State of Servers

947163 Newbie
Currently Being Moderated
Hi,

Please can somebody help in executing the below mentioned code which will print the name and state of the servers.

Below Java file taken from the following location : http://docs.oracle.com/cd/E23943_01/apirefs.1111/e13951/core/index.html

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Hashtable;
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.naming.Context;

public class PrintServerState {

private static MBeanServerConnection connection;
private static JMXConnector connector;
private static final ObjectName service;

// Initializing the object name for DomainRuntimeServiceMBean
// so it can be used throughout the class.
static {
try {
service = new ObjectName(
"com.bea:Name=DomainRuntimeService,Type=weblogic.management.
mbeanservers.domainruntime.DomainRuntimeServiceMBean");
}catch (MalformedObjectNameException e) {
throw new AssertionError(e.getMessage());
}
}

/*
* Initialize connection to the Domain Runtime MBean Server
*/
public static void initConnection(String hostname, String portString,
String username, String password) throws IOException,
MalformedURLException {
String protocol = "t3";
Integer portInteger = Integer.valueOf(portString);
int port = portInteger.intValue();
String jndiroot = "/jndi/";
String mserver = "weblogic.management.mbeanservers.domainruntime";
JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname,
port, jndiroot + mserver);
Hashtable h = new Hashtable();
h.put(Context.SECURITY_PRINCIPAL, username);
h.put(Context.SECURITY_CREDENTIALS, password);
h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES,
"weblogic.management.remote");
connector = JMXConnectorFactory.connect(serviceURL, h);
connection = connector.getMBeanServerConnection();
}

/*
* Print an array of ServerRuntimeMBeans.
* This MBean is the root of the runtime MBean hierarchy, and
* each server in the domain hosts its own instance.
*/
public static ObjectName[] getServerRuntimes() throws Exception {
return (ObjectName[]) connection.getAttribute(service,
"ServerRuntimes");
}

/*
* Iterate through ServerRuntimeMBeans and get the name and state
*/
public void printNameAndState() throws Exception {
ObjectName[] serverRT = getServerRuntimes();
System.out.println("got server runtimes");
int length = (int) serverRT.length;
for (int i = 0; i < length; i++) {
String name = (String) connection.getAttribute(serverRT,
"Name");
String state = (String) connection.getAttribute(serverRT[i],
"State");
System.out.println("Server name: " + name + ". Server state: "
+ state);
}
}

public static void main(String[] args) throws Exception {
String hostname = args[0];
String portString = args[1];
String username = args[2];
String password = args[3];

PrintServerState s = new PrintServerState();
initConnection(hostname, portString, username, password);
s.printNameAndState();
connector.close();
}
}


When executing the above mentioned code getting the following error

Exception in thread "main" java.net.MalformedURLException: Bad IPv6 address: t3:
//localhost
at javax.management.remote.JMXServiceURL.validateHost(Unknown Source)
at javax.management.remote.JMXServiceURL.validateHost(Unknown Source)
at javax.management.remote.JMXServiceURL.validate(Unknown Source)
at javax.management.remote.JMXServiceURL.<init>(Unknown Source)
at PrintServerState.initConnection(PrintServerState.java:39)
at PrintServerState.main(PrintServerState.java:84)
Caused by: java.net.UnknownHostException: t3://localhost
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$1.lookupAllHostAddr(Unknown Source)
at java.net.InetAddress.getAddressesFromNameService(Unknown Source)
at java.net.InetAddress.getAllByName0(Unknown Source)
at java.net.InetAddress.getAllByName(Unknown Source)
at java.net.InetAddress.getAllByName(Unknown Source)
at java.net.InetAddress.getByName(Unknown Source)
... 6 more
  • 1. Re: Printing the Name and State of Servers
    Pierluigi Vernetto Pro
    Currently Being Moderated
    are you passing the 4 parameters in the command line:
    String hostname = args[0];
    String portString = args[1];
    String username = args[2];
    String password = args[3];

    ?

    better print them for debugging purposes
  • 2. Re: Printing the Name and State of Servers
    947163 Newbie
    Currently Being Moderated
    Yes i am passing the paramenters in command line
  • 3. Re: Printing the Name and State of Servers
    Fabian Pro
    Currently Being Moderated
    Hi,

    Copy the below code and save it as PrintServerState.java,
    I also have copied the code from
    http://docs.oracle.com/cd/E21764_01/web.1111/e13728/accesswls.htm#i1116377

    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.util.Hashtable;
    import javax.management.MBeanServerConnection;
    import javax.management.MalformedObjectNameException;
    import javax.management.ObjectName;
    import javax.management.remote.JMXConnector;
    import javax.management.remote.JMXConnectorFactory;
    import javax.management.remote.JMXServiceURL;
    import javax.naming.Context;
    import java.lang.*;

    public class PrintServerState {

    private static MBeanServerConnection connection;
    private static JMXConnector connector;
    private static final ObjectName service;

    // Initializing the object name for DomainRuntimeServiceMBean
    // so it can be used throughout the class.
    static {
    try {
    service = new ObjectName(
    "com.bea:Name=DomainRuntimeService,Type=weblogic.management.
    mbeanservers.domainruntime.DomainRuntimeServiceMBean");
    }catch (MalformedObjectNameException e) {
    throw new AssertionError(e.getMessage());
    }
    }

    /*
    * Initialize connection to the Domain Runtime MBean Server
    */
    public static void initConnection(String hostname, String portString,
    String username, String password) throws IOException,
    MalformedURLException {
    String protocol = "t3";
    Integer portInteger = Integer.valueOf(portString);
    int port = portInteger.intValue();
    String jndiroot = "/jndi/";
    String mserver = "weblogic.management.mbeanservers.domainruntime";
    JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname,
    port, jndiroot + mserver);
    Hashtable h = new Hashtable();
    h.put(Context.SECURITY_PRINCIPAL, username);
    h.put(Context.SECURITY_CREDENTIALS, password);
    h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES,
    "weblogic.management.remote");
    connector = JMXConnectorFactory.connect(serviceURL, h);
    connection = connector.getMBeanServerConnection();
    }

    /*
    * Print an array of ServerRuntimeMBeans.
    * This MBean is the root of the runtime MBean hierarchy, and
    * each server in the domain hosts its own instance.
    */
    public static ObjectName[] getServerRuntimes() throws Exception {
    return (ObjectName[]) connection.getAttribute(service,
    "ServerRuntimes");
    }

    /*
    * Iterate through ServerRuntimeMBeans and get the name and state
    */
    public void printNameAndState() throws Exception {
    ObjectName[] serverRT = getServerRuntimes();
    System.out.println("got server runtimes");
    int length = (int) serverRT.length;
    for (int i = 0; i < length; i++) {
    String name = (String) connection.getAttribute(serverRT,
    "Name");
    String state = (String) connection.getAttribute(serverRT[i],
    "State");
    System.out.println("Server name: " + name + ". Server state: "
    + state);
    }
    }

    public static void main(String[] args) throws Exception {
    String hostname = args[0];
    String portString = args[1];
    String username = args[2];
    String password = args[3];

    PrintServerState s = new PrintServerState();
    initConnection(hostname, portString, username, password);
    s.printNameAndState();
    connector.close();
    }
    }

    Now compile it as javac -d . PrintServerState.java

    Then Execute WL_HOME/weblogic92/server/bin/setWLSEnv.sh

    Now Execute ur class file generated as
    java -classpath .:WL_HOME/weblogic92/server/lib/wljmxclient.jar PrintServerState ipaddress port username password

    The output will be as follows

    Server name: Server3.   Server state: RUNNING
    Server name: Server2.   Server state: RUNNING
    Server name: Server1.   Server state: RUNNING
    Server name: Server4.   Server state: RUNNING

    Regards
    Fabian

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points