Ejb 2.0

1,286 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,286
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
99
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • 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.
  • Ejb 2.0

    1. 1. <ul><li>Enterprise Java Bean </li></ul><ul><ul><ul><ul><ul><li> </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li> BY: </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li> Sukesh Kumar </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li> </li></ul></ul></ul></ul></ul>
    2. 2. The Motivation… <ul><li>Component Based Development… </li></ul><ul><li>Instead of reusing Java Classes, you get to reuse a bigger chunk of functionality. </li></ul>
    3. 3. What does EJB give me? <ul><li>Lets you focus on the business logic and The underlying services: </li></ul><ul><ul><ul><li>Transaction Management </li></ul></ul></ul><ul><ul><ul><li>Security </li></ul></ul></ul><ul><ul><ul><li>Concurrency </li></ul></ul></ul><ul><ul><ul><li>Networking </li></ul></ul></ul><ul><ul><ul><li>Resource management </li></ul></ul></ul><ul><ul><ul><li>Persistence </li></ul></ul></ul><ul><ul><ul><li>Messaging </li></ul></ul></ul><ul><ul><ul><li>Deploy-time customization </li></ul></ul></ul><ul><li>to the EJB Server Vendor. </li></ul>
    4. 4. Portability <ul><li>Java </li></ul><ul><ul><li>Write once Run Anywhere </li></ul></ul><ul><ul><li>Stops the force to work on single OS </li></ul></ul><ul><li>EJB </li></ul><ul><ul><li>Write Once Deploy Anywhere </li></ul></ul><ul><ul><li>Stops being at the mercy of Application Server Vendor </li></ul></ul>
    5. 5. EJB Architecture It’s about infrastructure…
    6. 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.
    7. 7. The RMI Remote Method Invocation…
    8. 8. What is RMI? <ul><li>Access to Remote Objects </li></ul><ul><li>Java-to-Java only - i.e., from JVM </li></ul><ul><li>Your client gets to act like it’s making a remote method call, but what its doing is calling a method on a proxy object running on the same heap. </li></ul><ul><li>The proxy is called “stub” </li></ul>
    9. 9. Stubs <ul><li>Implement a given set of RMI interfaces, which are also implemented by a remote object. </li></ul><ul><li>lives on client </li></ul><ul><li>Have the same interface as the server object </li></ul><ul><li>handles all the low level networking sockets and streams. </li></ul>
    10. 10. Skeletons <ul><li>lives on server </li></ul><ul><li>receives requests from stub </li></ul><ul><li>talks to true remote object </li></ul><ul><li>delivers response to stub </li></ul>
    11. 11. RMI Layers TCP Java Virtual Machine Client Object Java Virtual Machine Stub Remote Object Skeleton Remote Reference Layer Transport Layer Remote Reference Layer Transport Layer
    12. 12. Remote Reference Layer <ul><li>Figures out which remote object is being referenced </li></ul><ul><li>Could span multiple virtual machines </li></ul><ul><li>Communicates via TCP/IP </li></ul>
    13. 13. Transport Layer <ul><li>Deals with communications </li></ul><ul><li>Connection management </li></ul><ul><li>Dispatching messages between stub and skeleton </li></ul>
    14. 14. RMI Flow Client Virtual Machine Client Server Virtual Machine Stub Remote Object Skeleton Registry Virtual Machine “ Team” Server 1 2 1. Server Creates Remote Object 2. Server Registers Remote Object 3. Client requests object from Registry 3 4. Registry returns remote reference (and stub gets created) 4 5. Client invokes stub method 5 6. Stub talks to skeleton 6 7. Skeleton invokes remote object method
    15. 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. 16. Algorithm <ul><li>Marshalling (Serialization) </li></ul><ul><li>Un-marshalling (De-Serialization) </li></ul><ul><li>Java.io.OutputStream and java.io.InputStream classes typically used by calling WriteObject on OutputStream and ReadObject on InputStream . </li></ul>
    17. 17. Digging deep in to it Exploring the components and the architecture…
    18. 18. EJB Components <ul><li>Component Interface </li></ul><ul><ul><li>Extends EJBObject </li></ul></ul><ul><ul><li>Business methods the client uses </li></ul></ul><ul><li>Home Object </li></ul><ul><ul><li>Extends EJBHome </li></ul></ul><ul><ul><li>Hands out references of the component interface </li></ul></ul><ul><ul><li>One deployed bean, one home </li></ul></ul><ul><li>Bean Class </li></ul><ul><ul><li>Let this be a secret for the time being. </li></ul></ul>
    19. 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
    20. 20. Who does what? <ul><li>Container </li></ul><ul><ul><li>Implements Component Interface </li></ul></ul><ul><ul><li>Creates stub to EJBObject </li></ul></ul><ul><ul><li>Implements Home Interface </li></ul></ul><ul><ul><li>Creates stub for it. </li></ul></ul><ul><li>You </li></ul><ul><ul><li>Create Component Interface </li></ul></ul><ul><ul><li>Create Bean Class </li></ul></ul><ul><ul><li>Create Home Interface </li></ul></ul>
    21. 21. Getting specific <ul><li>Session Beans </li></ul><ul><ul><ul><li>Client request share the Home, but never the bean </li></ul></ul></ul><ul><ul><ul><li>One Home but different EJBObject and Bean. </li></ul></ul></ul><ul><li>Entity Beans </li></ul><ul><ul><ul><li>Clients share the Home, and may share the bean. </li></ul></ul></ul><ul><ul><ul><li>If two clients are trying to access same ENTITY, then both have reference to same EJBObject. </li></ul></ul></ul>
    22. 22. Session Bean Creation <ul><li>Stateful: </li></ul><ul><ul><li>After the EJBObject creation, the bean is instantiated with call of create() . </li></ul></ul><ul><li>Stateless </li></ul><ul><ul><li>The bean creation is on discretion of Container. </li></ul></ul><ul><ul><li>The call of create() and actual creation of the bean are decoupled. </li></ul></ul><ul><ul><li>A single bean can handle multiple client as long as only one client at a time is in the middle of the business method invocation. </li></ul></ul>
    23. 23. <ul><li>Message Driven Beans </li></ul><ul><ul><li>Don’t have a client view. </li></ul></ul><ul><ul><li>Don’t have Home or EJBOjbect </li></ul></ul><ul><ul><ul><li>The client sends message to a JMS messaging </li></ul></ul></ul><ul><ul><ul><li>JMS delivers the message to the container </li></ul></ul></ul><ul><ul><ul><li>The Container gets a MDB out of the pool </li></ul></ul></ul><ul><ul><ul><li>The container delivers the message to the bean by calling the onMessage() MessageListener interface method. </li></ul></ul></ul>
    24. 24. The client view Bean exposed…
    25. 25. What client wants? <ul><li>A reference to Bean </li></ul><ul><li>What it can get is reference to its bodyguard, the EJBObject </li></ul><ul><li>To get even the EJBObject, it has to get the Home interface </li></ul><ul><li>So…. It all starts with the lookup to the HOME interface. </li></ul>
    26. 26. How does he get it? The process…
    27. 27. JNDI <ul><li>Java Naming and Directory Interface </li></ul><ul><li>Organizes things in to a virtual directory tree </li></ul><ul><li>Each level of tree is either another virtual directory (called a context) or an object </li></ul>
    28. 28. The Client code…
    29. 29. PortableRemoteObject <ul><li>It’s not pure RMI with JRMP </li></ul><ul><ul><li>Where you know that what you get out of lookup is polymorphically something that IS-A home interface. </li></ul></ul><ul><li>It’s RMI with RMI-IIOP </li></ul><ul><ul><li>You might get back an IIOP stub that isn’t castable to the home interface. </li></ul></ul><ul><ul><li>Narrow the object you got to the object you want. </li></ul></ul>
    30. 30. The home interface Stateless Session Bean Stateful Session Bean Rules: Declare a create() method that returns the component interface and throw CreateException and RemoteException <ul><li>Stateless Session beans have only one no-arg create and Stateful </li></ul><ul><li>can have multiple, overloaded create methods, throwing Exceptions </li></ul><ul><li>Can also throw other checked exceptions </li></ul><ul><li>The name of the create method must begin with create and arguments </li></ul><ul><li>Must be RMI-IIOP compliant </li></ul>
    31. 31. EJBHome methods <ul><li>EJBMetaData getEJBMetaData() </li></ul><ul><ul><li>Get reflection like information of the bean. </li></ul></ul><ul><li>HomeHandle getHomeHandle() </li></ul><ul><ul><li>Serialize the home so that you can use it later </li></ul></ul><ul><li>void remove (Handle h) </li></ul><ul><ul><li>Done with the session bean, send EJBObject handle </li></ul></ul><ul><li>void remove (Object key) </li></ul><ul><ul><li>Done with the Entity bean, delete row from DB. Pass Primary key. </li></ul></ul>
    32. 32. The Component Interface <ul><li>Rules </li></ul><ul><ul><li>Declare one or more business methods. </li></ul></ul><ul><ul><ul><li>Arguments and return types must be RMI-IIOP compatible. </li></ul></ul></ul><ul><ul><ul><li>You can have overloaded methods </li></ul></ul></ul><ul><ul><ul><li>Each method must declare a RemoteException </li></ul></ul></ul><ul><ul><ul><li>You can declare your own application exceptions but they must not be runtime exceptions </li></ul></ul></ul>
    33. 33. EJBObject Methods <ul><li>Object getPrimaryKey() </li></ul><ul><ul><li>Get the PK of an Entity bean </li></ul></ul><ul><li>EJBHome getEJBHome() </li></ul><ul><ul><li>Get the bean’s Home </li></ul></ul><ul><li>Handle getHandle() </li></ul><ul><ul><li>Save a reference to the EJBObject </li></ul></ul><ul><li>void remove() </li></ul><ul><ul><li>Tell the bean you are done with it (Session) </li></ul></ul><ul><li>boolean isIdentical() </li></ul><ul><ul><li>Compare two EJB object references to see if they reference the same bean. (See note) </li></ul></ul>
    34. 34. Session Beans Being a Session Bean…
    35. 35. Lifecycle <ul><li>Stateless </li></ul><ul><ul><li>Bean creation (when container wants) </li></ul></ul><ul><ul><li>Bean use (when client calls a method) </li></ul></ul><ul><ul><li>Bean removal (when container decides there are too many beans in the pool) </li></ul></ul><ul><li>Stateful </li></ul><ul><ul><li>Bean creation (when the client wants a bean) </li></ul></ul><ul><ul><li>Bean use (when the client calls a method) </li></ul></ul><ul><ul><li>Bean passivation (bean is put to sleep to conserve resources) </li></ul></ul><ul><ul><li>Bean activation (bean wakes up to service bus. method from the client) </li></ul></ul><ul><ul><li>Bean removal (when the client is finished with the bean, or the bean times out) </li></ul></ul>
    36. 36. Container callbacks <ul><li>Comes from two places </li></ul>ExampleHome create() ExampleBean setSessionContext(SessionContext sc) ejbActivate() ejbPassivate() ejbRemove() ejbCreate() printStuff() SessionBean setSessionContext(…) ejbActivate() ejbPassivate() ejbRemove() Your Home interface SessionBean interface For every create() in the home There must be a matching ejbCreate() in the bean Business method, not a container callback For Stateless session bean there will be exactly five container callbacks As has only one create(). Stateful beans will have arguments in create() to pass the client-specific state to be stored.
    37. 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. 38. Beanness <ul><li>A SessionContext Reference </li></ul><ul><ul><li>The bean’s special reference for getting info form container </li></ul></ul><ul><ul><ul><li>Get a reference to your home and EJB object </li></ul></ul></ul><ul><ul><ul><li>Get security information about client </li></ul></ul></ul><ul><ul><ul><li>Force transaction to rollback (CMT) </li></ul></ul></ul><ul><ul><ul><li>Find out if a transaction has already been set to rollback (CMT) </li></ul></ul></ul><ul><ul><ul><li>Get a transaction reference, and call methods on it (BMT) </li></ul></ul></ul><ul><li>A special JNDI context </li></ul><ul><ul><li>A particular node on a JNDI virtual directory tree </li></ul></ul><ul><ul><ul><li>A reference to another bean </li></ul></ul></ul><ul><ul><ul><li>A reference to a resource manager connection factory </li></ul></ul></ul><ul><ul><ul><li>Deploy time constants for the bean, set by deployer </li></ul></ul></ul><ul><ul><ul><li>A reference to administered object reference (JMS destination) </li></ul></ul></ul><ul><li>Access to </li></ul><ul><ul><ul><li>Another bean </li></ul></ul></ul><ul><ul><ul><li>A resource manager (like a database) </li></ul></ul></ul>
    39. 39. When can you do what? <ul><li>Use you SessionContext to: </li></ul><ul><ul><ul><li>Get a reference to your home </li></ul></ul></ul><ul><ul><ul><li>Get a reference to your EJB object </li></ul></ul></ul><ul><ul><ul><li>Get security information about the client </li></ul></ul></ul><ul><ul><ul><li>Force a transaction to rollback (CMT) </li></ul></ul></ul><ul><ul><ul><li>Find out if the transaction has already been set to rollback (CMT) </li></ul></ul></ul><ul><ul><ul><li>Get a transaction reference, and call method on it (BMT) </li></ul></ul></ul><ul><li>Access: </li></ul><ul><ul><ul><li>Your special JNDI environment </li></ul></ul></ul><ul><ul><ul><li>Another bean’s method </li></ul></ul></ul><ul><ul><ul><li>A resource manager (like a database) </li></ul></ul></ul>constructor setSessionContext() ejbCreate() ejbActivate() ejbPassivate() Business methods ejbRemove()
    40. 40. Passivation: When/Why/How? <ul><li>Client doesn’t call any methods for a while, so container calls ejbPassivate() on the bean </li></ul><ul><li>Then saves the bean to temporary storage (serialization) </li></ul><ul><li>Client calls a business method, so container activates the bean (deserialization) </li></ul><ul><li>You have to make sure that by the time ejbPassivate() completes, all the instance variable’s values are ready for passivation. (see note) </li></ul>
    41. 41. Implementing activate and passivate <ul><li>ejbPassivate() </li></ul><ul><ul><li>Make sure your instance variables are ready for passivation </li></ul></ul><ul><ul><ul><li>A JDBC connection is not Serializable, set it to null </li></ul></ul></ul><ul><li>ejbActivate() </li></ul><ul><ul><li>Reacquire the non-Serializable resources, or do whatever it takes to restore your state for use </li></ul></ul><ul><ul><ul><li>Get the JDBC connection back </li></ul></ul></ul>
    42. 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)
    43. 43. Rules for Home methods: bean class <ul><li>Every create method in home must have a matching ejbCreate in bean class </li></ul><ul><li>ejbCreate must be public and not final or static </li></ul><ul><li>You do not have to declare exceptions declared in home, unless you might actually throw it </li></ul><ul><li>You must NOT declare any application exceptions that were not in create of home </li></ul>
    44. 44. Rules for business methods :Component interface <ul><li>Names must not begin with ejb. </li></ul><ul><li>Arguments and Return types must be RMI-IIOP compliant. </li></ul><ul><li>You must not expose the local home or component interface of a bean through a Remote component interface method </li></ul>
    45. 45. Rules for business methods: bean class <ul><li>Business methods must be declared public and not static or final </li></ul><ul><li>Method names must not begin with ejb </li></ul><ul><li>Must not pass “this” as argument or return value </li></ul><ul><li>Arguments and return types must be RMI-IIOP type. </li></ul><ul><li>You do not have to declare exceptions declared, unless you might actually throw it </li></ul><ul><li>You must NOT declare any application exceptions that were not in component interface </li></ul>
    46. 46. Entity Beans The persistence…
    47. 47. Entity Beans: Why? <ul><li>Entity bean is a realization of something that already exists in persistent store. </li></ul><ul><li>By using it you take the advantage of </li></ul><ul><ul><li>all the Container’s services </li></ul></ul><ul><ul><li>Automatic synchronization of db and the bean </li></ul></ul><ul><ul><li>You don’t have to map back and forth in your code </li></ul></ul><ul><ul><li>You avoid all the SQL queries in your code </li></ul></ul>
    48. 48. What you have to do? <ul><li>Add </li></ul><ul><ul><li>Call create() </li></ul></ul><ul><ul><li>Container pulls the bean from the pool of this bean type </li></ul></ul><ul><ul><li>Container inserts new row in database </li></ul></ul><ul><ul><li>Get the EJB object stub after linking </li></ul></ul><ul><li>Update </li></ul><ul><ul><li>Call a finder </li></ul></ul><ul><ul><li>Container checks if the persistent store has the data </li></ul></ul><ul><ul><li>get the EJB object stub, if the data exists </li></ul></ul><ul><ul><li>Make changes </li></ul></ul>
    49. 49. The code
    50. 50. <ul><li>create() and findByPrimaryKey() returns only one component interface reference </li></ul><ul><li>There can be multiple entity finders, the client get a reference to each bean that matches the query </li></ul><ul><ul><li>That means you have to make remote method calls on each to get the data you want </li></ul></ul><ul><ul><ul><li>Home business methods can return something other than EJB object references. </li></ul></ul></ul><ul><ul><ul><li>Perfect if client wants entity data and not reference </li></ul></ul></ul><ul><ul><ul><li>You can write the getAll() kind of methods as Home business method </li></ul></ul></ul>
    51. 51. finders <ul><li>There can be one or more finder methods </li></ul><ul><li>Returns either the Remote component interface (single-entity finder) or a Collection (multiple-entity finder) </li></ul><ul><li>All finders must declare both the RemoteException and the FinderException. </li></ul>
    52. 52. ejbRemove() <ul><li>The Entity is deleted </li></ul><ul><li>The Component interface stub for the object is deleted </li></ul><ul><li>The Entity bean goes to the pool </li></ul>
    53. 53. ? Who loves Stale things? Do they matter?
    54. 54. Entity Bean Synchronization <ul><li>Container’s job is to make sure that the entity and the bean stay in sync. </li></ul><ul><li>Before the bean can run any business method, the bean has to be refreshed with the entity’s state </li></ul><ul><ul><li>Will the bean get stale between every business method call? </li></ul></ul><ul><ul><ul><li>When the client calls a business method, and that method starts a transaction, I tell the db to lock the entity. </li></ul></ul></ul>Still a Problem?
    55. 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. 56. Activation/Passivation <ul><li>Passivation: </li></ul><ul><ul><li>ejbPassivate(): called when bean has finished the business method to release the resources and send back to the pool. </li></ul></ul><ul><ul><li>Bean has no identity </li></ul></ul><ul><ul><li>Bean is not serialized and saved </li></ul></ul><ul><li>Activation: </li></ul><ul><ul><li>ejbActivate() :is called to service a business method </li></ul></ul>
    57. 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. 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. 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
    60. 60. A reference to EJB object <ul><li>Client calls a finder on the home </li></ul><ul><li>Client calls create on the home </li></ul><ul><li>Client calls a home business method that returns a reference to the bean’s component interface </li></ul>?
    61. 61. Why ejbPostCreate? <ul><li>Can I initialize my EJB object without the primary key or knowing the state changes? </li></ul><ul><li>ejbCreate() </li></ul><ul><ul><li>Put your entity initialization code, values for your persistent fields </li></ul></ul><ul><ul><li>Bean instance gets instantiated in setEntityContext(), here you initialize it, doesn’t have EJB object </li></ul></ul><ul><ul><li>Return type must be Primary Key </li></ul></ul><ul><li>ejbPostCreate() </li></ul><ul><ul><li>Finish your initialization code here </li></ul></ul><ul><ul><li>You have the EJB object reference </li></ul></ul><ul><ul><li>Accessing your Container Managed Relationships </li></ul></ul><ul><ul><li>Return type must be void </li></ul></ul>
    62. 62. finders <ul><li>Container implements </li></ul><ul><li>Your bean class must not even mention the finder methods </li></ul><ul><li>Container implements using the home interface and your deployment descriptor to figure out what to do </li></ul><ul><li>Container returns the EJB object, but the bean stays in the pool to avoid loading of the stale data </li></ul>
    63. 64. Message Driven Beans Asynchronous communication…
    64. 65. MDB: Overview <ul><li>Client (Producer) sends a message to the messaging service </li></ul><ul><li>Message is delivered to the Container. Client is doing other things </li></ul><ul><li>Container acknowledges the message to the services, and pulls a bean out of the pool to process the message. </li></ul><ul><li>Container invokes the beans onMessage() method, with the message as argument </li></ul><ul><li>The bean’s transaction commits, and is sent back to pool </li></ul><ul><ul><li>If the bean’s transaction in onMessage had rolled back, the Container would have told the messaging service to put the message back on the queue. </li></ul></ul>
    65. 66. <ul><li>MDBs are asynchronous </li></ul><ul><li>Multiple beans of same type can process messages concurrently </li></ul><ul><li>Container makes sure that each bean is thread safe </li></ul>
    66. 67. State chart: MDB EJB EJB 1. constructor 2. setMessageDrivenContext 3. ejbCreate() does not exist method ready ejbRemove() or timeout Bean throws system exception (uncheked, uncaught) onMessage()
    67. 68. MDB class
    68. 69. JMS destination <ul><li>What is JMS Destination? </li></ul><ul><li>Where did you specify the JMS destination in the code? </li></ul>
    69. 70. Flavors of Messaging <ul><li>Topics </li></ul><ul><ul><li>Uses Publish and Subscribe method. One bean per pool gets a copy. </li></ul></ul><ul><ul><li>Types are: </li></ul></ul><ul><ul><ul><li>Durable Subscription </li></ul></ul></ul><ul><ul><ul><ul><li>Consumer sees all the messages, even if offline </li></ul></ul></ul></ul><ul><ul><ul><li>Non-Durable Subscription </li></ul></ul></ul><ul><ul><ul><ul><li>Consumer must be online to view the message </li></ul></ul></ul></ul><ul><li>Queues </li></ul><ul><ul><li>FIFO. Only one bean gets the message. </li></ul></ul><ul><ul><ul><li>Producer sends the message for single consumer </li></ul></ul></ul>
    70. 71. What if something goes wrong? <ul><li>What if the bean getting the message dies or throws an exception? </li></ul><ul><li>Handled using </li></ul><ul><ul><li>acknowledgement: </li></ul></ul><ul><ul><ul><li>Container tells the Messaging service that everything is fine if message delivered </li></ul></ul></ul><ul><ul><ul><li>Tells to put message back in queue if something bad happens </li></ul></ul></ul>
    71. 72. How is it tracked? <ul><li>The transaction status </li></ul><ul><ul><li>Is tied to whether the transaction commits or rolls back. </li></ul></ul><ul><ul><li>You get this behavior for beans that uses CMT </li></ul></ul><ul><li>The method completion </li></ul><ul><ul><li>Is tied to whether the message completes successfully. </li></ul></ul><ul><ul><li>You get this behavior for beans that use BMT </li></ul></ul>
    72. 73. EJB Transactions The ACID test…
    73. 74. Exceptions in EJB Handle the bad…
    74. 75. Security in EJB The secrets…
    75. 77. Deployment Prepare the environment…

    ×