YOUR FEEDBACK
Adobe Flex 2 - Answering Tough Questions About Enterprise Development
A Correct Person wrote: Denis Roebrt commented on the 21 Aug 2006 "Tough Que...
SOA World Conference
Virtualization Conference
$50 Savings Expire May 23, 2008... – Register Today!

2007 West
GOLD SPONSORS:
Active Endpoints
Your SOA Needs BPEL for Orchestration
BEA
Virtualized SOA: Adaptive Infrastructure for Demanding Applications
Nexaweb
Overcoming Bandwidth Challenges with Nexaweb
TIBCO
What is Service Virtualization?
SILVER SPONSORS:
WSO2
Using Web Services Technologies and FOSS Solutions
Click For 2007 East
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

Digg This!

  • 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
    3rd International Virtualization Conference & Expo: Themes & Topics
    From Application Virtualization to Xen, a round-up of the virtualization themes & topics being discussed in NYC June 23-24, 2008 by the world-class speaker faculty at the 3rd International Virtualization Conference & Expo being held by SYS-CON Events in The Roosevelt Hotel, in midtown
    Microsoft To Keynote 4th International Virtualization Conference & Expo
    Mike Neil is general manager for virtualization strategy in the Windows Server Division at Microsoft. Mike is focused on the delivery of the Windows virtualization technology, including Windows Server 2008 Hyper-V, Microsoft Hyper-V Server and Virtual PC 2007. Mike also directs the tec
    Virtualization Meets DaaS - Desktop-as-a-Service
    After a $1.5 million angel round, Desktone, which was started in 2006 by Eric Pulier, who also started SOA Software, US Interactive and IVT, picked up $17 million in first-round funding about a year ago from Highland Capital Partners, SoftBank Capital, Citrix Systems and the China-base
    Engelbart's Usability Dilemma: Efficiency vs Ease-of-Use
    The mouse was the original idea of Doug Engelbart who was the head of the Augmentation Research Center (ARC) at Stanford Research Institute. Engelbart's philosophy is best embodied, in my opinion, in the design of another device that he invented, the five-finger keyboard - with keys li
    Web 2.0 Is Fundamentally About Empowering People
    'Unlocking content to be remixed into new business value' is the driver of Web 2.0 in the enterprise, says Rod Smith, IBM VP of Emerging Internet Technologies, in this Exclusive Q&A with Jeremy Geelan on the occasion of IBM's release of a new technology created by IBM researchers, code
    Why Do 'Cool Kids' Choose Ruby or PHP to Build Websites Instead of Java?
    Here is a question that I have been pondering on and off for quite a while: Why do 'cool kids' choose Ruby or PHP to build websites instead of Java? I have to admit that I do not have an answer. Why do I even care? Because I am a Java developer. Like many Java developers, I get along w
    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

    MOST READ THIS WEEK
    ADS BY GOOGLE
    BREAKING NEWS FROM THE WIRES
    AmberPoint Extends SOA Governance to Apache ServiceMix, BEA AquaLogic Service Bus 3.0, BEA WebLogic Integration, Cisco ACE XML Gateway, JBoss Enterprise Application Platform and Oracle Fusion
    AmberPoint announced today that it has extended the reach of its runtime SOA governance