6. High Level View Client Object EJB Object B I Z I N T E R F A C E S e r v i c e s EJB Server DB ? ? ? How does a client get a reference to a different machine/JVM? How does the client communicate with the bean? How does the server step into client-bean call? Java references hold bits that don’t means anything outside the Currently running JVM.
15. Food for thought How does a local method call, passes an object reference? By Value: by copying the bits in the reference variable. The object Itself is never passed. How does it work with remote method call? That reference would Actually mean nothing on the other heap. An object copy is shipped, not the reference copy. Actually sends Remote Object’s stub.
16.
17. Digging deep in to it Exploring the components and the architecture…
18.
19. How does it all happen? EJB S e r v i c e s The bean is deployed, and the server instantiates the Bean Home object and registers it with JNDI. The client does a JNDI lookup on the Home, using the registered name. The JNDI sends back a stub to the Remote Home object. The client asks the Home for a reference to the Component interface, by calling create() The services kick in and the Bean is created. The EJBObject is created and stub returned to the client. The client can call the business method on the bean The client can get rid of Home stub if he doesn’t want access to more Beans of this type. Home Stub create() EJBObject EJBObject stub
37. State chart: stateful EJB EJB EJB 1. constructor 2. setSessionContext 3. ejbCreate() does not exist method ready passivated Timeout. ejbRemove() not called ejbPassivate() ejbActivate() ejbRemove() or timeout Bean throws system exception (uncheked, uncaught) cannot be brought out of passivation, just to kill it.
38.
39.
40.
41.
42. State chart: stateless EJB EJB 1. constructor 2. setSessionContext 3. ejbCreate() does not exist method ready ejbRemove() or timeout Bean throws system exception (uncheked, uncaught)
55. The Flow tx EJB updateCustomer() Client calls a business method Container intercepts the call, Starts a transaction before getting the bean Container tells the db to lock the row Container loads the bean with the entity state from the db Bean runs multiple business methods in the same transaction Container ends the transaction after updating the new state in bean which might have been cached on behalf of the entity Container asks the db to release the lock some Sk 1 Address Name ID some Sk 1 Address Name ID
56.
57. Container callbacks EntityBean set Entity Context(EntityContext ec) ejbActivate() ejbPassivate() ejbRemove() unsetEntityContext() ejLoad() ejbStore() SessionBean set Session Context(…) ejbActivate() ejbPassivate() ejbRemove() When bean is reactivated, deserialization Container gives the bean a reference to its context When bean is taken out of the pool to service a client’s buz method call When bean is about to be serialized To reduce pool size When bean is about to return to pool, following a transaction Delete entity from database Container calls when Wants to reduce size of pool When bean has be refreshed with data from persistent storage When container is about to update the db Both EntityContext and SessionContext extend EJBContext, but EJBContext adds One method getPrimaryKey(). This is different from EJBObject’s getPrimary key as This is only exposed to the entity beans and not to both Session bean and Entity bean
58. Container callbacks ExampleHome create() findByPrimaryKey(..) doAll() ExampleBean Your Home interface EntityBean interface For every create() in the home There must be two methods in the bean ejbCreate() and ejbPostCreate() EntityBean set Entity Context(EntityContext ec) ejbActivate() ejbPassivate() ejbRemove() unsetEntityContext() ejbLoad() ejbStore() set Entity Context(EntityContext ec) ejbActivate() ejbPassivate() ejbRemove() unsetEntityContext() ejbLoad() ejbStore() ejbCreate() ejbPostCreate() ejbFindByPrimaryKey(String key) ejbHomeDoAll() //business methods from //component interface Bean also has the Virtual Persistent fields, which are for values that map to db. They represent entity’s persistent state. They exist as abstract getters and setters
59. State chart: Entity bean EJB EJB EJB 1. constructor 2. setEntityContext does not exist pooled method ready Timeout. ejbRemove() not called ejbPassivate() ejbActivate() unsetEntityContext() Bean throws system exception (uncheked, uncaught) ejbFind<method>() ejbSelect<method>() ejbHome<method>() ejbFind<method>() ejbHome<method> Biz method (Component interface) ejbSelect<method>() From business method
In Older days, each app server vendor had its own proprietary API. You learn it then work with it. If you need a new feature, its totally vendor dependent.
If you are an object and you have a reference to another object, that object must be in the same heap with you.
JRMP is Java Remote Method Protocol and is only Java to Java. Here you can be sure that the object that you get is the one you asked for.
RMI-IIOP compatible arguments are : Serializable, primitive types, Remote, or array or a collections of any of those. Application exceptions or compiler checked exceptions extends Exception class and runtime exceptions are a subclass of RuntimeExceptions.
isIdentical(): Stateless Session Bean: True, if both references come from same Home, even if stubs are referring to two different Remote EJB Objects Stateful Session Bean: False no matter what, for any two unique stubs, even if for same home Entity Beans: True if the stubs refer to two entities with the same Primary Key. How are the three remove() methods different?
When ejbPassivate() completes, every non-transient instance variable must be a reference to one of the following: A serializable object A null value A bean’s remote component or home interface, even if the stub class is not serializable A bean’s local component or home interface, even if the stub class is not serializable A SessionContext object, even if its not serializable A bean’s special JNDI context,or any of its sub-context The UserTransaction interface A resource manager connection factory (like DataSource)
Is different from Stateless Session bean which calls the ejbActivate() and ejbPassivate(), and hence has to serialize and save the state.