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. Figure 25.21. The Home and Remote interfaces of an EJB in a nonclustered WebLogic Server environment.
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.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 StubsWhen 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:
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 StubsBecause 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:
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:
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 BeansBy 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:
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 BeansBy 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 BeansBy 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 :
Note The read-only cache exists locally on every server that accesses the bean. |