How to Perform dynamic load balancing in Glassfish application server?

GlassFish is a fully Java EE 5-compliant application server with enterprise-ready features available under two OSI-approved licenses. Among many other enterprise-level features, GlassFish provides a very good self-management functionality extendable using the Java Management eXtension (JMX) standard.The GlassFish application server provides good facilities for cluster management and load balancing. Still, sometimes we need to have more fine-grained control over how our cluster nodes are loaded with requests. One such condition can happen in a shared environment when we are using the processing power to host several applications, ranging from databases to batch job processing to application servers serving requests that come from different customers. Such conditions can lead us to change the load routed toward each GlassFish instance during different time slices. We can change the routed load manually, but having the ability to change it automatically based on defined rules is something very desirable. JMX refers to several JSRs that allow software vendors and developers to have a common technology for exposing management and monitoring capabilities of their Java-based applications.JMX has three important components:

MBean: MBeans are probe-level objects of the JMX specification. There are four kinds of MBeans for different use cases; the most popular MBean type is Dynamic. MBeanServer: This is the access point for MBeans. All MBeans should be registered with an MBeanServer. It is the access point that allows a variety of operations such as invoking MBeans’ operations, changing attributes of MBeans, event listener registration, and so on.

Connectors and Adapters: These allow different types of management consoles to create a connection to an MBeanServer from outside of the host JVM. Popular connectors are RMI and the JMX Messaging Protocol (JMXMP; defined by JSR 160). Popular adapters are the SNMP adapter defined in Java Platform Profiling Architecture (JSR 163), and the web (HTML/HTTP) adapter. Using JMX, we guarantee that any other software that knows JMX can interact with our MBeans in order to monitor specific attributes or change some others that are exposed by our MBeans, or to listen for different types of JMX events published by our MBeans.AMX is present to interact with GlassFish application server MBeans, free of the difficulties discussed above. AMX is a set of APIs that expose management MBeans as client-side dynamic proxies that implement AMX interfaces. These AMX interfaces are mostly included in com.sun.appserv.management.config and some of its sub-packages. We can use AMX APIs on the client side in order to manage almost all JSR 77-approved Java EE objects like servers, applications, resources, etc., without knowing anything about JMX. However, all of these EE objects are also manageable through plain JMX.

Using AMX we can:Change application server configurations–create resources, delete resources, enable or disable, etc. Manage servers, node agents, clusters, etc. Receive notifications for almost any event happening in the application server and react accordingly. Monitor the state of many objects that are hosted inside the application server. These include EJBs, web applications, enterprise applications, connection pools, etc. When we install GlassFish in the cluster or enterprise profile, we create a domain of GlassFish application servers that initially has only one member. This first member is the Domain Administration Server (DAS) and it is a single point of management and administration for every cluster or instance that we may add to this domain. All related configuration of every domain object (such as instances, clusters, connection pools, etc.) is stored in DAS and can be changed directly using DAS. AMX allows us to connect to DAS and perform every required operation on application server MBeans in order to change the configuration of mentioned domain objects. There are some helper classes included in com.sun.appserv.management.helper that further ease working with AMX APIs