Saturday, October 27, 2007

 

Standard MBeans and Documentation

This blog-post assumes that you are a Java Programmer and have used or at least have knowledge of JMX technology. If word JMX is not ringing the bell, and if you are a Java programmer, I strongly suggest you get to know this technology and I'm sure you won't regret that you did. Go here for more info.

JMX technology in Java ecosystem has been playing tremendous role in making the Java components more manageable and maintainable. If you are developing a Java component and are exposing the configuration/management through set of attributes/operations, most probably you would be using

Back to point. If you are using standard MBeans with an interface defining all attributes/operations and an implementation class to provide the functionality, I'm sure you would have noticed that attributes/operations on the JMX agents are not being displayed but just empty.

One would have put in enough effort to document the MBeans interface with all nitty-gritty details but it is would be of no use if it is not being displayed in the JMX agent view.  This is due to fact that JMX  agents are accessing your interfaces at runtime, by then Java compiler would have removed all javadocs and changed the parameter names for operations. However StandardMBeans provide set of hooks, which can show the appropriate documentation on the agent view.

Documentation hooks works by set of getDescription/getParameter methods on StandardMBean class before constructing the MBeanInfo object for your MBean.  Where you can override those method to provide the documentation appropriate for your MBean, which would displayed on the agent view. However it is required to copy the documentation from MBean interface javadocs and programmatically return it. It is laborious and unmaintainable.  Solution being, generate that class which can generate the documentation automatically from the MBean interface javadocs.

To my surprise I didn't find any tool/utility which can generate this java file automatically from the MBean interface and provides the documentation at runtime. So I decided to implement my own and release to public. It is MBeanDoclet.

MBeanDoclet automatically generates a java source file for set of MBeans and provides the same javadoc documentation at runtime. So if you modified the javadoc, it is automatically updated in JMX Agents.

Example worth ten-thousand words. So here is one. For ex., if we have a MBean named TestMBean with some nicely written documentation

package com.brsanthu.mbeandoclet;

/**
 * MBean created to demonstrate the MBean Doclet usage.
 */
public interface TestMBean {
   
    /**
     * This is standard mbean attribute, which does nothing
     * but used only to demonstrate the MBeanDoclet usage.
     */
    public int getSampleAttribute();
   
    /**
     * This is standard mbean operation, which does nothing
     * but used only to demonstrate the MBeanDoclet usage.
     */
    public void sampleOperation();
}
Java2html

Running the MBeanDoclet, generates the following file.

////////////////////////////////////////////////////////////////////////////
//
// CAUTION: This source file is automatically generated by MBeanDoclet.
//          Any modifications would be overwritten nexttime it runs.
//
////////////////////////////////////////////////////////////////////////////

package com.brsanthu.mbeandoclet;

import javax.management.MBeanAttributeInfo;
import javax.management.MBeanInfo;
import javax.management.MBeanOperationInfo;
import javax.management.MBeanParameterInfo;
import javax.management.NotCompliantMBeanException;
import javax.management.StandardMBean;

public class TestMBeanJavadoc extends StandardMBean {

    public TestMBeanJavadoc(Class mbeanInterface) throws NotCompliantMBeanException {
        super(mbeanInterface);
    }

    public TestMBeanJavadoc(Object implementation, Class mbeanInterface)
                    throws NotCompliantMBeanException {
        super(implementation, mbeanInterface);
    }

    @Override
    protected String getDescription(MBeanInfo info) {
    String description = "";

        description = "MBean created to demonstrate the MBean Doclet usage.";

        return description;
    }

    @Override
    protected String getDescription(MBeanAttributeInfo info) {

        String description = "";

        if (false) {
            //This is always false stmt is added to make easier
            //to generate following blocks of else condition.
     

        } else if (info.getName().equals("SampleAttribute")) {
            description = "This is standard mbean attribute, which does nothing" +
 " but used only to demonstrate the MBeanDoclet usage.";


        } else {
          description = super.getDescription(info);
        }

        return description;
    }

    @Override
    protected String getDescription(MBeanOperationInfo info) {
        String description = "";

        if (false) {
            //This is always false statement is added just to make it easier
            //to generate following multiple blocks of else conditions.


        } else if (info.getName().equals("sampleOperation")) {
            description = "This is standard mbean operation, which does nothing" +
 " but used only to demonstrate the MBeanDoclet usage.";


        } else {
          description = super.getDescription(info);
        }

        return description;
    }
}
Java2html

This automatic generated has added lots of value-add to the MBean we have been developing.

If this doclet can be of any useful, you can download here. If you have any questions/issues, mail be brsanthu[at]yahoo[dot]com.


Sunday, October 21, 2007

 

Poor Comcast Customer Service

A 70 year old woman from Bristow, VA took extreme measure to vent out the frustration of poor service from Comcast and went on to Hammer down the Comcat's office equipments. Well, I don't support her action of hammering down, Cable company should learn something from this to improve the service.

Read more here.


Saturday, October 06, 2007

 

Best of Adnan Sami

Do you like Adnan Sami's pop songs? Here is the You Tube play list of all his good songs. Enjoy!


Wednesday, October 03, 2007

 

Java Substring Caveat

It is one of the commonly used function in the java String class but many people don't realize the caveat associated with it.

When you call the substring on a String object, the implementation do NOT create a new char array that is backing it but shares it.  Most of the cases this does work fine except in scenarios where you have a very large original String and want to keep reference to a part of that String.

For ex.,

        String originalString = "Very large string to demo the caveat with substring";
        String newString = originalString.substring(42);
Java2html

Programmer would assume that newString has occupies memory to store 9 characters but truth is that it is 51 characters.  This is due to the fact that when substring is called, Java String library didn't actually create a new character array of required length but used the same one created by originalString.  This isn't an issue if you continue to keep the strong reference to the originalString as newString still does share same memory space but otherwise newString is using more memory than it should.

Is there a simple solution? There is.

String newString = new String(originalString.substring(42));
Java2html
 
This instructs Java library to create a new character array of just required length and initialize.
 
Happy (Java) coding!

Friday, September 28, 2007

 

Windows File Association Webservice

Microsoft should not be providing a feature if it's not working. Some features they implement and force users to use are just mediocre.  One of them is their File association webservice. It's a future that pops up the following window whenever user tries to open a file by double-clicking, for which Windows has no information about.

Windows File Association Webservice

Worst part is that when user selects it and click ok, it goes to a page on Microsoft site which displays this useless page.

image

Funny that website doesn't have information for well-known extensions like xml, msi (Microsoft Installer), C (C Programming).

Fortunately, it's possible to disable this and let Windows show the good old "Select a Program" list as described here. Linked Microsoft page describes to add a registry key, which novice users may find little confusing. Download this registry file, double click, and Click Yes when Windows prompts with below message. You are all set to go.

image


 

Adding timestamp to command output in Unix

Haven't you had the same irritating expression when you are trying to troubleshoot the performance problem in Unix and find out that vmstat command (and bunch of other commands) doesn't print the timestamp, leaving you in lurch to guess the probable time to which a line in vmstat maps to?

Same goes for JDK garbage collection log files (however JRockit provides a command line option to log timestamp along).

Well, there is simple way to do this using awk. Add the below variable and function addts (stands for "add timestamp") snippet to your .bash_profile (assuming you use bash or whichever place appropriate for your shell) and you have it handy whenever you want it.

export addtscmd='{now=strftime("[%b %d %Y %I:%M:%S %p] "); print now $0}'
function addts { awk "${addtscmd}"; }

Here is an example of using vmstat before and after the timestamp.

vmstat without timestamp

[lbs@lsctlnx48 lbs]$ vmstat  1 5
procs                      memory      swap          io     system         cpu
 r  b   swpd   free   buff  cache   si   so    bi    bo   in    cs us sy id wa
 0  0      0  77396 173080 848712    0    0     5     3    3     3  2  1  2  2
 0  0      0  77396 173080 848712    0    0     0     0  178    94  0  0 100  0
 0  0      0  77396 173080 848712    0    0     0     0  115   106  0  0 100  0
 0  0      0  77396 173080 848712    0    0     0     0  138   115  0  0 100  0
 0  0      0  77396 173080 848712    0    0     0     0  105    95  0  0 100  0

vmstat with timestamp

[lbs@lsctlnx48 lbs]$ vmstat 1 5 | addts
[Sep 28 2007 05:26:12 PM] procs                      memory      swap          io     system         cpu
[Sep 28 2007 05:26:12 PM]  r  b   swpd   free   buff  cache   si   so    bi    bo   in    cs us sy id wa
[Sep 28 2007 05:26:12 PM]  2  0      0  77380 173080 848712    0    0     5     3    3     3  2  1  2  2
[Sep 28 2007 05:26:13 PM]  0  0      0  77380 173080 848712    0    0     0     0  190   105  0  0 100  0
[Sep 28 2007 05:26:14 PM]  0  0      0  77380 173080 848712    0    0     0     0  159   113  0  0 100  0
[Sep 28 2007 05:26:15 PM]  0  0      0  77380 173080 848712    0    0     0     0  105   105  0  0 100  0
[Sep 28 2007 05:26:17 PM]  0  0      0  77380 173080 848712    0    0     0    30  111   124  0  0 94  6

The above command addts can be used with any tool output including tail -f. So it's as easy as it sounds to timestamp all garbage collection log files :)


Monday, September 24, 2007

 

Round Robin List

 

In one of the projects I needed a simple Round Robin list, which would just return the next object in round robin fashion.

Even though it's trivial to come up with one, surprised to find out that I didn't find any hits when I searched for one.

This is what I came up with after half-hour. Use it as you wish.  Download here.

package org.brsanthu.misc;

import java.util.ArrayList;
import java.util.Collection;

/**
 * Simple extension to <code>ArrayList</code> with round robin functionality.
 * <p>
 * Just adds one method {@link #getNext()} which would return the next object
 * in the list and the method starts over at first element after reaching the
 * end of list.
 * <p>
 * This class it NOT designed for multi-threaded access. So necessary synchronization
 * is required before accessing {@link #getNext()} method.
 *
 * @since Sep 24th 2007
 * @author Santhosh Kumar
 */
public class RoundRobinList extends ArrayList {
   
    private static final long serialVersionUID = 1L;
   
    /**
     * Last used index. Initially it would be -1, so that
     * first element returned is 0th element of the list.
     */
    private int lastIndex = -1;
   
    /**
     * Constructs a new round robin list with default capacity.
     */
    public RoundRobinList() {
        super();
    }

    /**
     * Constructs a new round robin list and pre-populates with the
     * objects found in given list.
     * <p>
     * For more information about usage of the c, see the <code>ArrayList</code> constructor
     * page.
     *
     * @param c - collection of objects to be added by default to the new list.
     */
    public RoundRobinList(Collection c) {
        super(c);
    }

    /**
     * Creates the new round robin list with specified capacity.
     *
     * @param initialCapacity - initial capacity.
     */
    public RoundRobinList(int initialCapacity) {
        super(initialCapacity);
    }

    /**
     * Returns the next object in the list.
     * <p>
     * First invocation of this method after creating the list, always returns
     * the 0th element. Starts over the 0th element after reaching the end of
     * list.
     * <p>
     * If there are no objects in the list, returns <code>null</code>.
     *
     * @return - returns the next object in the list. <code>null</code> if list is
     *          empty.
     *
     */
    public Object getNext() {
       
        if (isEmpty()) {
            return null;
        }
       
        lastIndex++;
        if (lastIndex >= size()) {
            lastIndex = 0;
        }
       
        return get(lastIndex);
    }
}
Java2html

Wednesday, September 06, 2006

 

Canon Rebel XTi (400D)

Successor to immensely popular Canon entry DSLR Rebel XT (350D) called Rebel XTi (400D) has been announced by Canon.

I'm saddened by this little as I had just bought the 350D and would have waited for this day if I knew about this release.

In fact, Canon's 400D has not made as much as news as Nikon's D80 and I have no idea why.

Check it out if you are planning for a new DSLR.


 

Weeklyshot.org Invite

JD has sent out an invite to Weeklyshot.org, which I had been waiting for, since quite sometime.  Thank you so much JD. Current theme is Weight but I don't have any shots signifying that. So would keep watching for new themes or see if I can get one shot to match the current title.


This page is powered by Blogger. Isn't yours?