Home > Articles

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Clustering Enterprise JavaBeans

As illustrated in Figure 25.21, in a nonclustered WebLogic Server environment, a client such as a servlet or JSP uses the Home interface of the EJBHome object to locate and create an instance of the bean (EJBObject). After receiving a reference to the bean instance, the client is then able to invoke the methods of the EJBObject via the bean's Remote interface.

As illustrated in Figure 25.22, in a WebLogic cluster environment, the load-balancing and failover mechanisms for EJBs, as well as RMI objects, are implemented using replica-aware stubs for the Home and Remote interfaces. These replica-aware stubs are generated through the compilation process of a cluster-aware EJB, as defined in the weblogic-ejb-jar.xml file. The weblogic.ejbc utility passes the cluster-aware interfaces through the rmic compiler, which then generates the replica-aware stubs for the respective EJB.

Figure 25.21Figure 25.21 The Home and Remote interfaces of an EJB in a nonclustered WebLogic Server environment.

Figure 25.22Figure 25.22 Creating and interacting with a clustered EJB.

NOTE

Replica-aware stubs for the RMI object are created explicitly using the command-line options to the rmic utility.

The following section describes the replica-aware stubs for the Home and Remote interfaces in more detail.

The Replica-Aware Home and Remote Stubs

When you deploy a clustered EJB to a clustered WebLogic Server, the replica-aware Home stub of the bean is bound to the server's local JNDI tree. The clustered server then updates the clusterwide JNDI tree by sending a JNDI announcement to other server members of the WebLogic cluster via a multicast broadcast message.

NOTE

Stateless session, stateful session, and entity EJBs can have replica-aware Home stubs.

When clients connect to a WebLogic cluster and look up a clustered EJB, they obtain a replica-aware stub for that EJBHome object. However, instead of representing a single EJBHome object, this replica-aware stub contains the following:

  • The logic required to locate the replicas (EJBHome objects) on any clustered server instance on which the EJB was deployed

  • The load-balancing logic for distributing the workload among those clustered server instances on which the replicas are located

  • The failover logic, to transparently retry the call to another replica, if the stub intercepts an exception

TIP

To ensure scalability and high availability for your clustered EJBs, you should deploy them homogeneously to your WebLogic cluster.

When a client invokes the create() method, the replica-aware Home stub routes the call to one of the EJBHome object replicas it is aware of via the replica handler. The replica then creates an instance of the bean and returns a replica-aware Remote (EJBObject) stub, which contains the logic to locate replicas (EJBObject objects) on any clustered server instance on which the EJB is deployed. However, unlike EJBHome objects, which can all use replica-aware Home stubs, only stateful and stateless session EJBs can leverage replica-aware Remote stubs.

The Load-Balancing and Failover Algorithms Employed by the Replica-Aware Stubs

Because all Home stubs are replica aware, the replica handler can provide the following load-balancing and failover algorithms for routing calls to the EJBHome object:

  • Round-robin load balancing—Is the default load-balancing strategy for replica-aware stubs if no algorithm is specified. This is a simple and predictable load-balancing strategy in which the replica handler cycles through a list of clustered server instances that host the target replicas for a specific stub.

  • Weight-based load balancing—Improves the round-robin strategy by allowing you to factor a pre-assigned weight between 1 and 100 to each clustered server. This weight value determines what proportion of the overall workload a server will bear relative to other servers. The higher the weight value, the more likely a server will be chosen by the replica handler. For example, you can assign a higher weight value to those servers that have a greater processing capability.

  • Random load balancing—Distributes replica requests randomly among targeted server hosts. This approach should be used only if the other load-balancing strategies do not meet your requirements and you have a homogeneous WebLogic cluster environment.

  • CAUTION

    The random load balancing algorithm does not guarantee an even distribution scheme across your servers because it is totally statistically based.

  • Parameter-based routing—Is a Call Router class created by a developer and associated with a replica-aware stub to provide custom parameter-based routing of your replica requests. The Call Router class is called before the replica request is invoked and passed the parameters of the request. The Call Router examines these parameters and returns the name of the server where the replica request should be made based on its programmed routing logic. See the BEA WebLogic Server documentation for more information on how to implement a Call Router class.

You can configure a cluster to use one of the preceding algorithms, with the exception of the parameter-based routing mechanism, using the Administration Console:

  1. From the left pane of the Administration Console, select the Clusters node.

  2. Select your cluster.

  3. On the Configuration, General tab, shown in Figure 25.23, perform the following tasks:

    • Default Load Algorithm—Select your load-balancing algorithm from the drop-down list.

    • Service Age Threshold—Enter a value that specifies the number of seconds by which the age of two conflicting services must differ before one is considered older than the other.

  4. Click Apply to save your changes.

Figure 25.23Figure 25.23 Selecting a load-balancing algorithm for EJB replicas.

The algorithm you select is maintained within the replica-aware stubs obtained for the clustered EJB objects.

WebLogic Server does not always perform load balancing for an EJB method call because it is more efficient to use a replica that is collocated with the Remote stub itself. This prevents additional overhead for calling a replica located on a remote server.

The Load-Balancing and Failover Mechanisms of Stateless Session Beans

By default, a stateless session bean's Home stub provides load balancing and failover for its method invocations to any clustered server where the bean is deployed. Because stateless session beans hold no state on behalf of their clients, stateless beans of the same type are identical in a WebLogic cluster. For this reason, a replica-aware Remote stub can also load-balance its method invocations to any clustered server where the bean is deployed.

By default, the Remote stub supports automatic failover of only those methods that occur after a method completes or if the method fails to connect to a server. If a method incurs a failure before it completes, the Remote stub does not automatically fail over the method invocation from one replica to another. Preventing automatic failover in this manner ensures a recovered method call is never able to duplicate persistent information, which could have been modified when the method initially failed.

If your stateless session bean contains idempotent methods, however, you can force the replica handler to retry a failed call without knowing whether the method actually completed on the failed server. Idempotent methods are those methods that typically do not persist their results and can be called repeatedly with the same arguments to produce the same result, irrespective of the location of the bean.

You can specify that a stateless session bean contains idempotent methods via the following clustering-descriptors in the bean's deployment descriptors:

  • At the bean level, you can set the stateless-bean-methods-are-idempotent parameter to true. This ensures automatic failover for all the methods of the bean.

  • At the method level, you can use the idempotent-methods deployment property.

NOTE

You do not need to specify the methods on the stateless session bean EJBHome object and read-only entity beans to be idempotent because they are automatically set to be idempotent by WebLogic Server.

The Load-Balancing and Failover Mechanisms of Stateful Session Beans

By default, a stateful session bean's Home stub provides load balancing and failover for its method invocations to any clustered server where the bean is deployed.

When the bean instance is created, however, the bean's hosting WebLogic Server selects a secondary WebLogic Server instance to host the replicated state of the EJB using the same rules as defined in replication groups for the HTTP Session state. The primary WebLogic Server hosts the actual instance of the bean. To minimize resource overhead, the secondary server stores only the replicated state of the primary bean for failover purposes; the secondary server does not create an instance of the bean. Replication groups were discussed earlier in this chapter.

The client receives a Remote stub that maintains a list of the primary and secondary server locations of the bean's state. However, all method calls to the bean are "pinned" to the primary state location; there is no load balancing of remote method calls. As a client modifies the primary state, the EJB container replicates the state from the primary to the secondary servers in the WebLogic cluster. This replication scheme is similar to that used by the HTTP session state and occurs immediately after the transaction commits or after each method invocation. To replicate the state of a stateful session EJB, ensure the bean is homogeneously deployed within the WebLogic cluster.

If the primary state location should fail, the secondary WebLogic Server creates a new bean instance using the replicated state, thus forming a new primary server location for the bean's method calls. The replica handler redirects subsequent requests to the bean to this new primary state. At the same time, a WebLogic Server is selected to hold the bean's replicated state (secondary), and the client's replica-aware Remote stub is updated with the new primary and secondary bean locations.

The Load-Balancing and Failover Mechanisms of Entity Beans

By default, an entity bean's Home stub provides load balancing and failover for its method invocations to any clustered server where the bean is deployed.

The load balancing and failover capabilities of the Remote stub depend on the read-write and read-only cache strategy employed by the entity bean as follows:

  • For read-write entity bean caches, the bean's remote method calls are "pinned" to the instance of the bean and cannot be load-balanced to other servers. Method calls fail over only if the request is made outside a transaction.

  • For read-only entity bean caches, each remote method call is load-balanced. However, failover does not automatically occur in the event of a recoverable call failure.

NOTE

The read-only cache exists locally on every server that accesses the bean.

  • + Share This
  • 🔖 Save To Your Account