Showing posts with label Monitoring. Show all posts
Showing posts with label Monitoring. Show all posts

Saturday, December 26, 2009

MBean vs MXBean

MBean vs MXBean

Manageable Resources are identified and instrumented as java objects, it could be Managed Bean(MBean) or advanced/extended/simplified Managed Bean(MXBean).

Managed Bean

MBeans can be any of java objects, in which we can store/retrive Serializable/Externalizable objects using methods. Based on the design pattern used in objects, we can differentiate either Standard(static) or Dynamic. Recommended to maintain Interface class name suffixed with MBean
Standard MBeans
Java objects that conform to certain design patterns derived from the JavaBeans component model.
  • fields are declared with access modifier as private, setter and getter methods with access modifier public, is called Attributes
  • Additionally, methods declared with access modifier public to do special operations, is called operations

public interface HelloWorldMBean {
//attributes
public String getName();
public void setName();

//operations
public void clearHelloCount();
public String sayHello(String name);
}
Dynamic MBeans
javax.management.DynamicMBean interface that offers more flexibility at runtime.
  • Object getAttribute(String attribute)
    Obtain the value of a specific attribute of the Dynamic MBean.
  • AttributeList getAttributes(String[] attributes)
    Get the values of several attributes of the Dynamic MBean.
  • MBeanInfo getMBeanInfo()
    Provides the exposed attributes and actions of the Dynamic MBean using an MBeanInfo object.
  • Object invoke(String actionName, Object[] params, String[] signature)
    Allows an action to be invoked on the Dynamic MBean.
  • void setAttribute(Attribute attribute)
    Set the value of a specific attribute of the Dynamic MBean.
  • AttributeList setAttributes(AttributeList attributes)
    Sets the values of several attributes of the Dynamic MBean.
Sample:
//Registering MBean in server
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
ObjectName name = new ObjectName("com.example:type=HelloWorld");
Object mbean = new HelloWorld();
mbs.registerMBean(mbean, name);

//Retrieving MBean from server
HelloWorldMBean hwBean = JMX.newMBeanProxy(mbs, name, HelloWorldMBean.class);
hwBean.sayHello("JMX");

MXBean

MXBeans reference only a pre-defined set of types. Recommended to maintain Interface class name suffixed with MXBean. Implements interface of Dynamic or cusotmized JavaBean. Using MXBean, We can facilitate any client to use MBeans. Additional details required to use MXBean

  • In interface, Class name has to be suffixed with MXBean or annotation @MXBean has to be used.
  • In implementation class, annotation ConstructorProperties used to reconstruct the MXBean in retrievel time. Possible attribute names required to get from MBeanServer for this ObjectName has to be listed in constructor argument.
  • No Model-Class implementation not required to retrive pre-defined types.
  • CompositeData used to reconstruct Complex Objects.

However, in implementation class, annotation used to specify the attribute details in ConstructorProperties. And, In discovering and accessing code has the differences. Instead of directly retrieving the cusomer defined Bean, javax.management.openmbean.CompositeDataSupport has used to retrieve the data.



Sample:

public HelloWorld implements HelloWorldMXBean{
private String name;
@ConstructorProperties({"name"})
public HelloWorld(String name)
{this.name=name;
}
}

...

//Registering MXBean in server
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
ObjectName name = new ObjectName("com.example:type=HelloWorld");
Object mbean = new HelloWorld();
mbs.registerMBean(mbean, name);

//Retrieving MXBean from server
//Predefined type retrieval
 String nameAttribute=mbs.getAttribute(name, "name");
 
//using CompositeData for complex java objects
CompositeData userdefinedBean = (CompositeData) mbs.getAttribute(name, "ComplexJavaObject");
String name = (String) hwBean.get("name");

//using proxy
HelloWorldMXBean proxy = JMX.newMXBeanProxy(mbs, name, HelloWorldMXBean.class);

Refer:
In java.net, What is an MXBean ? blog by Eamonn McManus.

Java Management Extensions (JMX)

Java Management Extensions (JMX)

Application is running for a long time suddenly started performing not good. We do not have any clue at this time. Idea started early 1998, there should be a way to interact the application from a local/remote vM to monitor and maintain the resource. For example, HeapSize almost reached maxSize and full collector not yet triggered, in this time we could make a request for GC from our application. This may not be the right solution, however, we can avoid OOM Exception.

In clustered environment, application may share the same configuration. User may update property in one node, which has to be replicated in other environment in flexible way. We cannot use tightly coupled model, since in failover scenario this will cause more pain to us. JMX Remote API, solves this problem by Change notification mechanism.

JSR details:

  • JSR 3 - Java Management Extensions
  • JSR 160 - JMX Remote API
  • JSR 255 - JMX 2.0


Typical uses of the JMX technology include:

  • Consulting and changing application configuration
  • Accumulating statistics about application behavior and making them available
  • Notifying of state changes and erroneous conditions.

The resources are instrumented as JAVA Object, known as Managed Beans(MBean). MBeans and its services sare registered in a Object Server, known as MBean Server or JMX agents. JMX Connectors helps agent to connect remote VM.

  • enables Java applications to be managed without heavy investment
  • provides a standard way to manage Java technology-based applications, systems, and networks
  • can be used for out-of-the-box management of the Java VM
  • provides a scalable, dynamic management architecture
  • leverages existing standard Java technologies
  • integrates easily with existing management solutions and emerging technologies. JMX solutions can use lookup and discovery services and protocols such as JiniTM network technology and the Service Location Protocol (SLP).

Examples TechNote

Wednesday, December 23, 2009

TCP Monitoring

TCP Monitoring

Network programming may be direct low level TCP communication or high level like RMI, HTTP, Web services, and etc., we may need to debug the message what and how it delivers in TCP layer. This will help us to debug and dig to chase the right tail. Sometime, we may see data incorrectly in receiving end due to network issues and we will start correcting our implementaion in server and client.

tcpmon is an open-source utility to Monitor A TCP Connection. In which, we can see the TCP layer data goes out of the box and also the response comes from the server. I have started using this tool to understand the structure of the SOAP message and HTTP headers. Specifically while going with encryption like MTOM/XOP, this tool helped lot.

If you want to count or see the content get transmitted while using browser to connect web application also, this tool helps lot.

We have to place this tool in between the server and client. For instance, I have to connect to application http://www.javafundu.com:7001/checkTCP. Here are the steps


  1. Double click the downloaded tcpmon.jar from tcpmon.dev.java.net
    or run command java -jar tcpmon.jar

  2. In Create a New TCP Monitor connection, set Local port=8080, Server Name=javafundu.com, and Server Port=7001

  3. Click on Add Monitor. Now TCP monitoring started

  4. Start the browser, and type the URL as http://localhost:8080/checkTCP

  5. Now Request from browser and response of the same also captured in TCPMon and get displayed.




Recent Posts

Unix Commands | List all My Posts

Texts

This blog intended to share the knowledge and contribute to JAVA Community such a way that by providing samples and pointing right documents/webpages. We try to give our knowledege level best and no guarantee can be claimed on truth. Copyright and Terms of Policy refer blogspot.com