Version 5

    Searchable title : EJB Loadbalancing Policies

     

    When running a clustered appliation in JBoss AS remote clients will automatically loadbalance themselves across the cluster as follows:

     

    When an EJB is deployed on a specific node, a dynamic proxy is generated and serialized, then bound into the local JNDI tree with a list of the nodes which currently comprise the cluster.  Obviously, when you deploy an EJB to an entire cluster (whether through "farm deployment" or other means) this process is repeated on each node in the cluster.

     

    To connect a fat client to a cluster the client must include the JBoss client-side JARS (found in the $JBOSS_HOME/client directory, select the ones you need or just jbossall-client.jar) in the fat client classpath.  The fat client needs to connect to the JNDI tree to get the "initial context".  This can be done in two ways as described here: AccessEJBsRemotely

     

    JBoss uses dynamic proxies (introduced in JDK 1.3) to eliminate the need for EJBC (ejb-compile) to generate stubs & skeletons for remoteability.  The dynamic proxy will have the specified (default or overridden) load-balancing policy "baked" into it so that when the fat client downloads the proxy the behavior is invisible. 

     

    DEFAULT LB-policies

    • HOME : RoundRobin

    • SLSB : RoundRobin

    • SFSB : FirstAvailable

    • EB   : FirstAvailable

     

    Override default values using these attributes in the EJB's jboss.xml descriptor file :

    • home-load-balance-policy

    • bean-load-balance-policy

     

    Other options include:

    • FirstAvailableIdenticalAllProxies (all EJBs for this client should "select" same target node)

    • RandomRobin

     

    Keep in mind that FirstAvailable and FirstAvailableIdenticalAllProxies are both "sticky" (essentially you are load-balancing clients/connections instead of requests) while the other policies are load-balancing individual requests across the nodes of the cluster.

     

    You can even plug in your OWN load-balancing policy declaratively by specifying it in this file!

     

    Each time the fat client invokes methods on the remote EJB the server has the opportunity to "piggyback" any changes to the cluster composition back to the dynamic proxy on the fat client (e.g. a node has been added or a node has been removed from the cluster definition). 

     

    More information available in Chapter 16 of our documentation (http://docs.jboss.org/jbossas/jboss4guide/r4/html/cluster.chapt.html)

     

    For those of you who are a bit curious, you can see "under the hood" to see exactly what is happening. 

    Download our source code (http://prdownloads.sourceforge.net/jboss/jboss-4.0.4RC1-src.tar.gz?download) and look at the following files:

    • d:\jboss\4.0.2\src\server\src\main\org\jboss\metadata\ClusterConfigMetaData.java

    • d:\jboss\4.0.2\src\cluster\src\main\org\jboss\proxy\ejb\ProxyFactoryHA.java

    • d:\jboss\4.0.2\src\cluster\src\main\org\jboss\ha\framework\interfaces\LoadBalancePolicy.java

    • d:\jboss\4.0.2\src\cluster\src\main\org\jboss\ha\framework\interfaces\FirstAvailable.java

    • d:\jboss\4.0.2\src\cluster\src\main\org\jboss\ha\framework\interfaces\FirstAvailableIdenticalAllProxies.java

    • d:\jboss\4.0.2\src\cluster\src\main\org\jboss\ha\framework\interfaces\RandomRobin.java

    • d:\jboss\4.0.2\src\cluster\src\main\org\jboss\ha\framework\interfaces\RoundRobin.java

     

     

    Globally overriding the default Load Balance Policy:

     

     

    For the load balance policies, the defaults are hard-coded and not centrally configurable, the easiest way to centrally configure the Load Balance Policy is through property substitution in the jboss.xml

     

    • For EJB2:

     

    <clustered>true</clustered> <cluster-config> <home-load-balance-policy>${my.sfsb.home.loadbalance.policy:org.jboss.ha.framework.interfaces.RoundRobin}</home-load-balance-policy> <bean-load-balance-policy>${my.sfsb.bean.loadbalance.policy:org.jboss.ha.framework.interfaces.FirstAvailable}</bean-load-balance-policy> </cluster-config>

     

    • For EJB3:

     

    <clustered>true</clustered> <cluster-config><partition-name>${jboss.partition.name:DefaultPartition}</partition-name> <load-balance-policy>${my.ejb3.sfsb.loadbalance.policy:org.jboss.ha.framework.interfaces.FirstAvailable}</load-balance-policy> </cluster-config>

     

    When starting JBoss, pass the values for the properties to override via -D. For example:

     

    run -c all -Dmy.sfsb.home.loadbalance.policy=org.jboss.ha.framework.interfaces.FirstAvailable

     

    EJB3 only supports XML configuration of clustering beginning with the RC9 release, so before that this won't work.

     

    Regarding partition name, the default partition name is configurable at start up via the -g switch (e.g. ./run.sh -c all -g MyPartition). EJB2 will use whatever is passed to -g as the default value for cluster-config/partition-name, which is why you don't have to specify it in the above example. EJB3 does not support that yet, so you have to specify it in XML. Beginning in 4.2.0, EJB3 will also default to whatever is passed to -g.

     

     

     

     

    Referenced by: