YOUR FEEDBACK
Gregor Rosenauer wrote: well, not what's your take on this? Did I miss a second page of this article or...
AJAXWorld RIA Conference
Early Bird Savings Expire Friday Register Today and SAVE !..

2008 East
DIAMOND SPONSOR:
Data Direct
Frontiers in Data Access: The Coming Wave in Data Services
PLATINUM SPONSORS:
Red Hat
The Opening of Virtualization
Intel
Virtualization – Path to Predictive Enterprise
Green Hills
IT Security in a Hostile World
JBoss / freedom oss
Practical SOA Approach
GOLD SPONSORS:
Software AG
The Art & Science of SOA: How Governance Enables Adoption
PlateSpin
Effective Planning for Virtual Infrastructure Growth
Fujitsu
Automated Business Process Discovery & Virtualization Service
Ceedo
Workspace Virtualization
Click For 2007 West
Event Webcasts

2008 East
PLATINUM SPONSORS:
Appcelerator
Think Fast: Accelerate AJAX Development with Appcelerator
GOLD SPONSORS:
DreamFace Interactive
The Ultimate Framework for Creating Personalized Web 2.0 Mashups
ICEsoft
AJAX and Social Computing for the Enterprise
Kaazing
Enterprise Comet: Real–Time, Real–Time, or Real–Time Web 2.0?
Nexaweb
Now Playing: Desktop Apps in the Browser!
Sun
jMaki as an AJAX Mashup Framework
POWER PANELS:
The Business Value
of RIAs
What Lies Beyond AJAX?
KEYNOTES:
Douglas Crockford
Can We Fix the Web?
Anthony Franco
2008: The Year of the RIA
Click For 2007 Event Webcasts

SYS-CON.TV
TOP THREE LINKS YOU MUST CLICK ON


Application Management with WebLogic Server for Developers, part 4
The Basics of Writing Custom Java Applications using JMX

  • For the previous four parts of this series, and the sixth and final part, please see links at foot of article

    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
    When writing a JMX application for managing BEA WebLogic Server, the easiest way to obtain a reference to the MBeanServer implementation is to look it up from the admin server's JNDI tree using the JNDI name weblogic.management.server. Administration MBeans are accessible through the admin server's MBeanServer. All configuration changes for a domain must be made through the admin server's MBeanServer.

    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.

  • About 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.

    About 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.

    BEA WEBLOGIC LATEST STORIES
    Since its emergence, Web Service technology has gone a long way towards perfecting itself and finding its right application in the real world. With the maturity of the specifications, Web Service technology, with its power of interoperability, is now the major enabling technology of SO...
    Join Scott Guthrie as he discusses Microsoft’s commitment to web standards development, Rich Internet Applications and how Microsoft is contributing to help move the web forward. Join Adobe’s Kevin Lynch as he demonstrates how Flash and HTML come together to make the most engaging,...
    Virtualization has become a critical part of Enterprise IT strategy. Why and how has it become one of the most important change agents in our industry? To answer these questions I had the good fortune recently to be able to speak to a select group of top IT industry executives who join...
    Watching VMware stock and its market cap spike since it IPO'd must have had Red Hat positively pea green with envyWatching VMware stock and its market cap spike since it IPO'd must have had Red Hat positively pea green with envy - so green in fact that it's gonna try taking VMware on b...
    A standard from OASIS called Web Services for Remote Portlets (WSRP) is used so portlets can be decoupled from a portal. In part one (JDJ, Volume. 13, issue 3) of this article, we introduced the relevant standards and specifications and then demonstrated WSRP's capabilities by consumin...
    SYS-CON's upcoming '3rd International Virtualization Conference & Expo' faculty includes such distinguished speakers as: Al Aghili (Managed Methods), Alan Chhabra (Egenera), Andi Mann (Enterprise Management Associates), Andrew Conte (APC), Andy Astor (EnterpriseDB), Ariel Cohen (Xsigo ...
    SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS
    SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
    Click to Add our RSS Feeds to the Service of Your Choice:
    Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
    myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
    Publish Your Article! Please send it to editorial(at)sys-con.com!

    Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021

    SYS-CON FEATURED WHITEPAPERS

    ADS BY GOOGLE
    BREAKING NEWS FROM THE WIRES

    Autodesk, Inc. (NASDAQ:ADSK) today announced that its Autodesk LocationLogic platfo...