| By Vadim Rosenberg, Robert Patrick | Article Rating: |
|
| February 26, 2004 12:00 AM EST | Reads: |
19,381 |
This article is the fourth in a series of articles on BEA WebLogic Server administration and management for developers (WLDJ, Vol. 2, issues 10–12). The first installment focused on administration concepts and terminology, and the graphical tools for packaging an application and setting up and configuring a WebLogic Server domain.
In the second article, we focused on application deployment, runtime management, and the monitoring facilities available with WebLogic Server that did not require knowledge of JMX. The last article discussed the basic concepts and terminology of JMX and the WebLogic Server 8.1 JMX infrastructure, as well as showing you how to use JMX-specific tools that come with WebLogic Server 8.1.
In this article, we'll show you the basics of how to write custom Java applications that use JMX to configure, administer, and manage BEA WebLogic Server 8.1–based applications.
JMX Programming Fundamentals
When writing a Java
application that uses BEA WebLogic Server's JMX capabilities, the first thing
you need to do is decide whether to use the standard JMX MBeanServer interface
or WebLogic Server's strongly typed interface. As we discussed in our last
article, the MBeanServer interface allows you to write Java applications that
manage any JMX-compliant application through its weakly typed, Java
reflection–style interface. While this interface is perfect for tool vendors
that want to work with a wide variety of applications and discover functionality
at runtime, it can be more tedious to write and debug when the JMX program is
intended to manage a well-known JMX-compliant application. BEA WebLogic Server
8.1 provides a strongly typed interface that is simpler to use and provides
better compile-time checking of your code.
Using the MBeanServer Interface
The basic steps
to writing a JMX program that uses the MBeanServer interface are:
- Obtain a reference to the MBeanServer implementation
- Determine the MBean(s) of interest
- Determine the MBean's attributes and/or operations of interest
- Invoke the appropriate methods on the MBeanServer to perform the action
Managed servers also have their own MBeanServer. Through these MBeanServers you can access local configuration and runtime MBeans. Currently, the MBeanServers on the managed servers are only accessible through the WebLogic-specific MBeanHome interface discussed in the next section.
BEA WebLogic Server's MBeanServer supports transparent remote access capabilities through the normal WebLogic RMI mechanisms so your JMX program does not need to concern itself with whether the MBeanServer is in the local or remote process. If you intend to change MBean attributes or invoke operations that modify the domain, you will need to authenticate your application to WebLogic Server with sufficient permissions to do so. Although the JNDI authentication mechanism is deprecated in favor of JAAS-style authentication, we show the older authentication mechanism for brevity (see Listing 1)
To get information about an MBean, you first need to know its object name. JMX uses the javax.management.ObjectName class to represent an MBean's object name. If you don't know what MBeans are available, use one of the MBeanServer's query methods to get a list of the matching MBeans. For example, use the queryNames() method with null arguments to return a java.util.Set containing the ObjectName objects for all registered MBeans, as shown here:
Set mbeansSet = mbeanServer.queryNames(null,
null);
Iterator mbeans = mbeansSet.iterator();
while (mbeans.hasNext())
{
ObjectName mbeanName = (ObjectName)mbeans.next();
...
}
Once you determine the object name for the MBean of interest, you get detailed information about the MBean's attributes and operations by using the MBeanServer's getMBeanInfo() method (see Listing 2).
Finally, you invoke the appropriate method on the MBeanServer to get or set the attribute or to invoke the operation (see Listing 3).
If you know the target JMX application you want to manage, the relevant MBean object names and their attributes and operations of interest, it is possible to make the JMX programming more straightforward if you are willing to give up some of the flexibility. Listing 4 shows a simple JMX program to get the default execute queue's configured number of threads for the domain's admin server.
Notice that we use the fact that there is an MBean of type AdminServer in the default domain to get the domain name and the server name from the related MBeans. With BEA WebLogic Server 8.1, the default domain name is always weblogic so we could have skipped calling getDefaultDomain() and simply used weblogic as the domain name (or omitted it entirely since it is the default) when creating the object name for the admin server MBean.
Using the Strongly Typed WebLogic JMX
Interface
BEA WebLogic Server 8.1 also provides an MBeanHome
interface that gives you access to the strongly typed interface. You obtain
references to a server's MBeanHome implementation by looking it up in JNDI. As
was the case with the MBeanServers, administrative MBeans must be accessed
through the admin server's special MBeanHome, known as the Administration
MBeanHome. To obtain a reference to the Administration MBeanHome implementation,
do a JNDI lookup on the admin server using the JNDI name
weblogic.management.adminhome. This JNDI name is defined as the ADMIN_JNDI_NAME
constant on the MBeanHome interface to help insulate your program from any
further JNDI changes.
All servers, including the admin server, also have local MBeanHome implementations that provide access to local configuration and runtime MBeans. These local MBeanHome implementations are accessible by performing a JNDI lookup directly against the server of interest using the JNDI names weblogic.management.home.localhome or weblogic.management.home.<server_name>, where <server_name> is the name of the WebLogic Server instance. The MBeanHome interface defines two additional constants, LOCAL_JNDI_NAME and JNDI_NAME, that you should use in place of weblogic.management.home.localhome and weblogic.management.home, respectively. The admin server also has references to all managed servers' local MBeanHome implementations through the weblogic.management.home.<server_name> JNDI names.
To get a reference to the Administration MBeanHome, use code that looks very similar to the code shown to obtain the MBeanServer:
MBeanHome mbeanHome = null;
try {
... // Same as
earlier example
mbeanHome
=(MBeanHome)
ctx.lookup("weblogic.management.adminhome");
}
catch
(NamingException ne) { ... }
The MBeanHome interface provides a wide variety of methods to create MBeans and get different types of MBeans. For example, the getAllMBeans() methods return the type-safe stubs for all MBeans in the specified domain, for which you could then use Java reflection to determine the set of attributes and operations each MBean supports. Of course, you typically choose to use the strongly typed interface because you already know the types of MBeans that you need to manipulate. The getAdminMBean() methods allow you to get a type-safe reference to an administration MBean:
String domainName =
mbeanHome.getDomainName();
ServerMBean myserver =
(ServerMBean)
mbeanHome.getAdminMBean("myserver", "Server", domainName);
Once you have the type-safe reference, you can access the attributes and operation directly:
int listenPort = myserver.getListenPort();
Listing 5 shows the strongly typed interface version of the same program contained in Listing 4. Notice that we are using the getAdminMbean(name, type, domain) method to locate the MBean references of interest. This interface tends to be much simpler than the JMX standard interface where we had to use ObjectName representations of the MBean's object name. However, it is still a little confusing when you need to supply more than the Name and Type attributes to uniquely identity the MBean because you have to tack on the extra attribute name-value pairs, delimited by commas, to the value of the name attribute. For example, the following code snippet from Listing 5 shows the form of the name argument where we also have to specify the Server attribute to uniquely locate the ExecuteQueueMBean of interest:
String defaultExecuteQueueName
=
"weblogic.kernel.Default,Server=" + adminServerName;
ExecuteQueueMBean
defaultExecuteQueueMBean =
(ExecuteQueueMBean)
mbeanHome.getAdminMBean(defaultExecuteQueueName,
"ExecuteQueue",
domainName);
We hope you can see that the type-safe version of the program is simpler and provides better compile-time checking. If you need to write JMX programs to automate the management of your BEA WebLogic Server–based applications, we recommend using the type-safe interface in most circumstances. If you are building JMX management tools to work with a variety of JMX-compliant application, then using the JMX MBeanServer interface will make your job easier.
Summary
In this article, we showed you the basics of the
two different JMX programming interfaces that are available to build JMX
management programs for BEA WebLogic Server 8.1. The JMX standard MBeanServer
interface provides a loosely typed, reflection-style interface that allows tool
vendors to write tools that discover MBeans and their attributes and operations
at runtime. The strongly typed WebLogic JMX MBeanHome interface provides a
simpler interface for building JMX management programs to perform predefined
tasks with a WebLogic Server–based application.
The next article in this series will dive into the more advanced Java APIs for building custom JMX programs that use JMX notification with monitors and timers. Our final installment will discuss creating custom MBeans and extending the Admin Console to display them.
Published February 26, 2004 Reads 19,381
Copyright © 2004 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Vadim Rosenberg
Vadim Rosenberg is the product marketing manager for BEA WebLogic Server. Before joining BEA two years ago, Vadim had spent 13 years in business software engineering, most recently at Compaq Computers (Tandem Division) developing a fault-tolerant and highly scalable J2EE framework.
More Stories By Robert Patrick
Robert Patrick is a director of technology in BEA's CTO Office and coauthor of the book Mastering BEA WebLogic Server: Best Practices for Building and Deploying J2EE Applications. Robert has spent his career helping customers design, build, and deploy high performance, fault-tolerant, mission-critical distributed systems using BEA Tuxedo and BEA WebLogic Server.
- Oracle To Keynote Cloud Computing Expo
- The Economics of Cloud Computing Analyzed
- The Difference Between Web Hosting and Cloud Computing
- GovIT Expo Highlights Cloud Computing
- Cloud Computing Best Practices
- Gang of Four Creates Cloud BI Stack
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- Citrix Aims To Cripple VMware’s Cloud Designs
- Product Evaluation: JBoss TCO Calculator
- Platform as a Service Journal Launched on Ulitzer
- An Introduction to Abbot
- Oracle To Keynote Cloud Computing Expo
- Will Ulitzer Dominate News Content on The Web? -Gartner
- REA Is Where RIA Becomes the Norm
- The Economics of Cloud Computing Analyzed
- Software AG Named "Gold Sponsor" of SOA World Conference & Expo 2009 East
- The Difference Between Web Hosting and Cloud Computing
- GovIT Expo Highlights Cloud Computing
- Cloud Computing Best Practices
- Gang of Four Creates Cloud BI Stack
- Java vs C++ "Shootout" Revisited
- Where Are RIA Technologies Headed in 2008?
- Configuring Eclipse for Remote Debugging a WebLogic Java Application
- Migrating a JBoss EJB Application to WebLogic
- XA Transactions
- An Introduction to Abbot
- WebLogic Tutorial: "Integrating Apache Poi in WebLogic Server"
- Eclipse "Pollinate" Project to Integrate with Apache Beehive
- Failover and Recovery of Enterprise Applications - Part 1
- Cover Story: A Practical Solution to Internationalization of a J2EE Web App

































