Accessing WebLogic Server MBeans with JMX 4-3
where milliseconds is a java.lang.Long object that contains the number of milliseconds that your JMX client waits for the invocation of an MBean-server
method to return. If a method does not return by the end of the timeout period, the client moves to its next set of instructions. By default, a client waits indefinitely
for a method to return; if the MBean server is unable to complete an invocation, the JMX client will hang indefinitely.
3.
Connect to the WebLogic MBean server by invoking the JMXConnector.getMBeanServerConnection
method. The method returns an object of type
javax.management.MBeanServerConnection .
The MBeanServerConnection object is your connection to the WebLogic MBean server. You can use it for local and remote connections. See
MBeanServerConnection in the J2SE 6.0 API Specification at
http:download.oracle.comjavase6docsapijavaxmanagement MBeanServerConnection.html
.
4.
Oracle recommends that when your client finishes its work, close the connection to the MBean server by invoking the JMXConnector.close method.
4.2.1 Example: Connecting to the Domain Runtime MBean Server
Note the following about the code in Example 4–1
:
■
The class uses global variables, connection and connector, to represent the connection to the MBean server. The initConnection method, which assigns
the value to the connection and connector variables, should be called only once per class instance to establish a single connection that can be reused within
the class.
■
The initConnection method takes the username and password along with the servers listen address and listen port as arguments that are passed when the
class is instantiated. Oracle recommends this approach because it prevents your code from containing unencrypted user credentials. The String objects that
contain the arguments will be destroyed and removed from memory by the JVMs garbage collection routine.
■
Because the client sets the jmx.remote.x.request.waiting.timeout environment parameter to 10000, all of its invocations of MBean server methods
will time out if the method does not return within 10000 milliseconds of being invoked.
■
When the class finishes its work, it invokes JMXConnector.close to close the connection to the MBean server. See JMXConnector in the J2SE 6.0 API
Specification at http:download.oracle.comjavase6docsapijavaxmanagement
remoteJMXConnector.html .
Example 4–1 Connecting to the Domain Runtime MBean Server
public class MyConnection { private static MBeanServerConnection connection;
private static JMXConnector connector; private static final ObjectName service;
Initialize connection to the Domain Runtime MBean Server.
4-4 Developing Custom Management Utilities With JMX for Oracle WebLogic Server
public static void initConnectionString hostname, String portString, String username, String password throws IOException,
MalformedURLException {
String protocol = t3; Integer portInteger = Integer.valueOfportString;
int port = portInteger.intValue; String jndiroot = jndi;
String mserver = weblogic.management.mbeanservers.domainruntime; JMXServiceURL serviceURL = new JMXServiceURLprotocol, hostname, port,
jndiroot + mserver;
Hashtable h = new Hashtable; h.putContext.SECURITY_PRINCIPAL, username;
h.putContext.SECURITY_CREDENTIALS, password; h.putJMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES,
weblogic.management.remote; h.putjmx.remote.x.request.waiting.timeout, new Long10000;
connector = JMXConnectorFactory.connectserviceURL, h; connection = connector.getMBeanServerConnection;
} public static void mainString[] args throws Exception {
String hostname = args[0]; String portString = args[1];
String username = args[2]; String password = args[3];
MyConnection c= new MyConnection; initConnectionhostname, portString, username, password;
... connector.close;
} }
4.2.2 Best Practices: Choosing an MBean Server