Head First EJB

Book description

What do Ford Financial, IBM, and Victoria's Secret have in common? Enterprise JavaBeans (EJB). As the industry standard for platform-independent reusable business components, EJB has just become Sun Microsystem's latest developer certification. Whether you want to be certifiable or just want to learn the technology inside and out, Head First EJB will get you there in the least painful way. And with the greatest understanding.You'll learn not just what the technology is, but more importantly, why it is, and what it is and isn't good for. You'll learn tricks and tips for EJB development, along with tricks and tips for passing this latest, very challenging Sun Certified Business Component Developer (SCBCD) exam. You'll learn how to think like a server. You'll learn how to think like a bean. And because this is a Head First book, you'll learn how to think about thinking.Co-author Kathy Sierra was one of Sun's first employees to teach brave, early adopter customers how to use EJB. She has the scars. But besides dragging you deep into EJB technology, Kathy and Bert will see you through your certification exam, if you decide to go for it. And nobody knows the certification like they do - they're co-developers of Sun's actual exam!As the second book in the Head First series, Head First EJB follows up the number one best-selling Java book in the US, Head First Java. Find out why reviewers are calling it a revolution in learning tough technical topics, and why Sun Chairman and CEO Scott McNealy says, "Java technology is everywhere...if you develop software and haven't learned Java, it's definitely time to dive in "Head First."And with Head First book, you don't even have to feel guilty about having fun while you're learning; it's all part of the learning theory. If the latest research in cognitive science, education, and neurobiology suggested that boring, dry, and excruciatingly painful was the best way to learn, we'd have done it. Thankfully, it's been shown that your brain has a sense of style, a sense of humour, and a darn good sense of what it likes and dislikes.In Head First EJB, you'll learn all about:

  • Component-based and role-based development
  • The architecture of EJB, distributed programming with RMI
  • Developing and Deploying an EJB application
  • The Client View of a Session and Entity bean
  • The Session Bean Lifecycle and Component Contract
  • The Entity bean Lifecycle and Component Contract
  • Container-managed Persistence (CMP)
  • Container-managed Relationships (CMR)
  • EJB-QL
  • Transactions
  • Security
  • EJB Exceptions
  • The Deployment Descriptor
  • The Enterprise Bean Environment in JNDI
  • Programming Restrictions and Portability
The book includes over 200 mock exam questions that match the tone, style, difficulty, and topics on the real SCBCD exam. See why Kathy and Bert are responsible for thousands of successful exam-passers--"The Sun certification exam was certainly no walk in the park, but Kathy's material allowed me to not only pass the exam, but Ace it!"--Mary Whetsel, Sr. Technology Specialist, Application Strategy and Integration, The St. Paul Companies"Kathy Sierra and Bert Bates are two of the few people in the world who can make complicated things seem damn simple, and as if that isn't enough, they can make boring things seem interesting."--Paul Wheaton, The Trail Boss, javaranch.com"Who better to write a Java study guide than Kathy Sierra, reigning queen of Java instruction? Kathy Sierra has done it again. Here is a study guide that almost guarantees you a certification!"--James Cubetta, Systems Engineer, SGI

Publisher resources

View/Submit Errata

Table of contents

  1. Head First EJB™
  2. Dedication
  3. A Note Regarding Supplemental Files
  4. Advance praise for Head First EJB™
  5. Praise for the Head First approach and Head First Java™
  6. Praise for the Head First approach
  7. Perpetrators of the Head First series (and this book)
  8. How to Use This Book: Intro
    1. Who is this book for?
      1. Who should probably back away from this book?
    2. We know what you’re thinking
    3. And we know what your brain is thinking
    4. Metacognition: thinking about thinking
    5. Here’s what WE did
    6. Here’s what YOU can do to bend your brain into submission
    7. What you need for this book
    8. Last-minute things you need to know
    9. About the SCBCD exam
    10. Technical Reviewers
    11. Other people to credit:
  9. 1. Intro to EJB: Welcome to EJB
    1. What is EJB all about?
    2. What does EJB really give me?
    3. No more vendor lock-in!
    4. How does it all work?
    5. Behind the scenes...
    6. Beans come in three flavors
    7. Session beans can be stateless or stateful
    8. Example: The Advice Guy bean
    9. Five things you do to build a bean:
    10. EJB Roles and Responsibilities
    11. Tutorial:
    12. Organize your project directory
      1. Organizing your terminal / command-line
    13. Compile the two interfaces and the bean class
    14. Start the server
    15. You’ll see something like this
    16. Start deploytool
    17. you’ll see something like this
    18. Make a new Application
    19. Name and save the new application
    20. What you’ll see after you create and name the application
    21. Now let’s make the new enterprise bean (the ejb-jar and the DD)
    22. Now we’re in the really cool New Enterprise Bean Wizard
    23. Create the new ejb-jar
    24. Add the three class files (including their package directory) to the JAR
    25. Confirm that you added ONLY the package directory and the class files
    26. Make it a Stateless Session bean
    27. Tell it which of the three class files in the JAR is the actual BEAN class
    28. Tell it which is the Home interface, and which is the Component interface
    29. Verify everything on this screen!
    30. You’re done, click Finish
    31. Meanwhile back on the main deploytool screen...
    32. Run your bean through the deploytool verifier
    33. Close your eyes and click OK
    34. Whew! No failed tests
    35. Time to Deploy
    36. Make it Return a Client Jar
    37. Give it a name, so clients can look it Up
    38. Watch the progress bars go up, then celebrate
    39. Now you’ll see the AdviceApp inside the server
    40. Now all we need is a client...
    41. Organizing your project directory for the client
      1. Run the client!
    42. Coffee Cram: Mock Exam
    43. Coffee Cram: Mock Exam Answers
  10. 2. Architectural Overview: EJB Architecture
    1. You remember this picture...
    2. Making a remote method call
    3. There’s a “helper” on the server as well...
    4. What about arguments and return values?
    5. What really gets passed when you pass an object to a remote method?
    6. Passing a Remote object through a remote method call
    7. What must the Remote object and the stub have in common?
    8. The client calls business methods on the stub through the Remote business interface
    9. How EJB uses RMI
    10. The Remote object is not the bean, it’s the bean’s bodyguard—the EJBObject
    11. The Component interface
    12. Who writes the class that really DOES implement the component interface? In other words, who makes the EJBObject class?
    13. Who creates what?
    14. The bean Home
    15. Architectural overview: Session beans
    16. Architectural overview: Entity beans
    17. Architectural overview: Creating a Stateful Session bean
    18. Architectural overview: Creating a Stateless Session bean
    19. Who creates the stateless session bean, and when?
    20. Stateless session beans are more scalable
    21. Architectural overview: Message-driven beans
  11. 3. The Client View: Exposing Yourself
    1. What the client really wants
    2. It all starts with the home interface
    3. How a client uses a session bean: create, use, and remove
    4. But first, we have to get a home interface reference
      1. In other words, we have to get the stub to the home object... the thing we use to call create(), so that we can get what we really want—the EJB object stub!
      2. What’s JNDI?
    5. Let’s take another look at the complete client code
    6. Just when you thought a simple cast would be enough...
    7. But NO. You have to narrow the object as well!
    8. OK, I’ll bite. Why can’t you just do a plain old cast?
    9. PortableRemoteObject.narrow()
    10. Writing the Remote home interface for a session bean
    11. Remote home interface examples for session beans
    12. But enough about the home... let’s talk about the EJB object. The component interface. The thing you REALLY want.
    13. Imagine what else you might want to do with your EJB object reference...
      1. Online shopping should not be rushed...
    14. Thankfully, we’ve got handles
    15. isIdentical?
      1. how to find out if two stubs refer to the same bean
    16. A bean’s client interfaces can be local
    17. Which methods make sense for the local client interfaces?
      1. Do we need handles with local interfaces?
      2. Do we need EJBMetaData with local interfaces?
      3. Do we need isIdentical() with local interfaces?
      4. Do we need primary key information with local interfaces?
      5. Do we need remove methods with local interfaces?
    18. When you think handle, think Remote
      1. Local clients don’t need handles!
    19. Who needs EJBMetaData when you’ve got reflection?
      1. Local clients don’t need EJBMetaData!
    20. Do you need isIdentical() when there’s equals()?
      1. Local clients still need isIdentical()
    21. Why so many remove methods?
      1. For Remote clients, two in the home, plus one in the component interface
      2. How can you use a remove that takes a handle when you don’t have a handle?
        1. Local clients don’t have handles, so local homes don’t have a remove() that takes a handle.
    22. Comparing Remote vs. Local interfaces
    23. Writing the local client interfaces
    24. You can have both a Remote and local client view for a bean, but you probably won’t.
    25. Exceptions in client interfaces: what the client might get
    26. Local client code
    27. What has to change inside the bean class?
    28. Arguments to Remote vs. local methods
    29. Coffee Cram: Mock Exam
    30. Coffee Cram: Mock Exam Answers
  12. 4. Session Bean Lifecycle: Being a Session Bean
    1. Exploring the session bean lifecycle
    2. You remember how it all works, right?
      1. Getting and using a stateful session bean
    3. There’s obviously more to the bean’s lifecycle than just creation and business methods...
    4. Container Callbacks, for the special moments in a bean’s life...
    5. Container Callbacks come from TWO places
    6. Implementing the container callbacks
    7. We have to look at the transitions
    8. Bean Creation: when an object becomes a bean
    9. Bean things you can do during creation
    10. Bean Use: what happens AFTER creation...
    11. Bean things you can do within business methods
    12. Passivation: a stateful bean’s chance at scalability...
    13. Your job for passivation: make your state passivatable!
    14. Bean things you can do in ejbActivate() and ejbPassivate()
    15. Bean Removal: when beans die
    16. Complaints about bean removal
    17. Bean things you can do in ejbRemove()
    18. Implementing the AdviceBean as a stateFUL bean
      1. Things you can add if the bean is stateFUL
    19. AdviceStatefulBean code
    20. AdviceStatefulBean CLIENT code
    21. Deploying a stateful bean
    22. Compared to stateful beans, stateless beans have a simple life
    23. Bean things you can do from stateless bean methods
    24. Writing a Session Bean: your job as Bean Provider
    25. SessionContext
      1. You need it more than it needs you
    26. Coffee Cram: Mock Exam
    27. Coffee Cram: Mock Exam Answers
  13. 5. Entity Bean Intro: Entities are Persistent
    1. What’s an entity bean?
    2. Entities vs. Entity Beans
    3. Entity beans from the client’s point of view
    4. A very simple Customer entity bean
    5. An entity bean’s client view
    6. Entity bean Remote component interface
    7. Entity bean Remote component interface
    8. Entity bean Remote home interface
      1. How it’s different from a session bean home interface:
    9. What does the client really want from an entity bean home?
    10. Entity bean Remote home interface
    11. When finders have a dark side...
    12. Home business methods to the rescue
    13. Session bean create() vs. entity bean create()
    14. Session bean remove() vs. entity bean remove()
    15. Entity/bean/instance death
    16. Entity bean client view
    17. Coffee Cram: Mock Exam
    18. Coffee Cram: Mock Exam Answers
  14. 6. Bean/Entity Synchronization: Being an Entity Bean
    1. The real power of entity beans is synchronization
    2. The only question is WHO does the work when it’s time to synchronize
    3. Container-managed vs. bean-managed persistence
    4. A brief history on the evolution of CMP 2.0
    5. The EntityBean interface adds three new container callbacks (including two just for synchronization)
    6. Even the methods that are the same, don’t behave the same
    7. But wait... there’s more! Entity beans have new home container callbacks, too
    8. Writing a CMP entity bean: make it abstract
    9. You put three kinds of things in your bean class:
    10. PLUS... (ok, that’s four things...)
    11. Virtual fields are NOT instance variables!
    12. Complete code for the CustomerBeanCMP class
    13. So how DID the client get a reference to the EJB object for #28?
    14. Bean things you can do during entity construction:
    15. Object identity: the primary key
    16. Bean things you can do during entity creation:
    17. Bean things you can do in home business methods
    18. Bean things you can do during activation and loading
    19. Bean things you can do during passivation and storing
    20. Coffee Cram: Mock Exam
    21. Coffee Cram: Mock Exam Answers
  15. 7. Entity Relationships: When Beans Relate
    1. Beanifying your movie database
    2. But we don’t want to think in TABLES We want to think in CLASSES
    3. We need relationships between the Movie bean and the Director bean
    4. Why should the Director be a bean? Why can’t it just be data?
    5. Relationships and multiplicity
    6. Multiplicity in Bean Classes
    7. Multiplicity affects return type!
    8. Defining virtual fields for persistent data fields and relationship fields
    9. Defining your “abstract persistence schema” (virtual fields aren’t enough)
      1. The Container needs these TWO things to know you have a CMP field “title”
    10. Persistent CMP fields in the DD
    11. Using relationships in your code
    12. Defining relationships in your abstract persistence schema (in the DD)
      1. Relationship definition for the Director-to-Movie relationship
      2. Director-to-Movie relationship
      3. Relationships can be one-way (unidirectional)
      4. The dark side of a unidirectional (one-way) relationship
      5. Cascade delete can propagate
      6. Director-to-Movie relationship
      7. Movie-to-Trailer relationship
      8. The abstract schema is in the <entity> element and the <ejb-relation> is in the <relationships> element
      9. MovieBean code with a CMR field
      10. The MovieBean’s home interface
    13. Mapping from abstract schema to a real database
      1. Writing your portable queries, for select and finder methods
      2. EJB-QL for the MovieBean
      3. Using an optional WHERE clause
      4. Navigating to another related bean
      5. Selecting a value rather than the whole bean
      6. EJB-QL SELECT
      7. EJB-QL SELECT — when to use Object ( )
      8. What does it MEAN to return an abstract schema type?
      9. SELECT and FROM are mandatory!
      10. Identifiers
      11. The WHERE clause
      12. The WHERE clause
      13. The problem with using Collection types
      14. Collections don’t bark()!
      15. The IN operator lets you say “For an individual element IN the Collection...”
      16. The BETWEEN expression
      17. The other “IN”
      18. The IS EMPTY comparison expression
      19. The IS NOT EMPTY comparison expression
      20. The LIKE expression
      21. The NOT LIKE expression
      22. Relationship assignments
      23. If the multiplicity of the relationship field is ONE, it’s a MOVE If the multiplicity is MANY, it’s a COPY
    14. Coffee Cram: Mock Exam
    15. Coffee Cram: Mock Exam Answers
  16. 8. Message-Driven Beans: Getting the Message
    1. Imagine this scenario...
    2. Too bad these guys aren’t message-driven beans
      1. Don’t they look a lot like stateless session beans?
    3. Message-driven bean class
    4. Writing a message-driven bean: your job as Bean Provider
    5. Notice something missing from the code?
      1. We never said what kind of messages we’re listening for, or where they might be coming from.
      2. Complete DD for a message-driven bean
    6. Topics and Queues
    7. Only ONE bean per pool gets a copy of a topic message
    8. With a queue, only one bean gets the message. Period.
    9. MessageDrivenContext
    10. MessageDrivenContext
    11. What if something goes wrong?
    12. Message acknowledgement
    13. That’s all well and good, but let’s go back and see how our earlier scenario ended...
    14. Think about it.
    15. Coffee Cram: Mock Exam
    16. Coffee Cram: Mock Exam Answers
  17. 9. EJB Transactions: The Atomic Age
    1. The ACID test
      1. Is your transaction safe?
    2. Distributed transactions: two-phase commit
    3. How it works in EJB
      1. Transactions can propagate through method calls
    4. Some transactions don’t propagate
      1. The caller’s transaction might be suspended
    5. How do I make (or get) a transaction?
      1. Two ways: code it or declare it
    6. Transaction-related methods are in two interfaces
    7. Making a BMT transaction
    8. Call stack of the checkOut() method
    9. Things you must NOT do with BMT
    10. What does it mean to suspend a transaction?
    11. The UserTransaction interface
    12. setRollbackOnly()
      1. The sound of a transaction’s death
    13. setRollbackOnly() lives in TWO interfaces
    14. getRollbackOnly()
      1. Because life’s too short for a bean to waste time
    15. BMT beans use getStatus() instead of getRollbackOnly()
    16. BMT can be a really BAD idea. BMT hurts bean reuse
    17. Container-managed transactions
    18. How attributes work
      1. Know your attributes
    19. Transaction attributes that require a transaction
    20. Transaction attributes that do not require a transaction
    21. These are the methods you MUST mark with an attribute (for a CMT bean)
    22. “Unspecified Transaction Context”
    23. Burn these in
    24. Marking transactions in the DD
    25. DD example for CMT
    26. More DD examples for CMT
    27. Summary of Bean-managed demarcation
    28. Entity beans have ejbLoad() to stay synchronized, even if the transaction rolls back.
    29. Session Synchronization
      1. because session beans don’t have ejbLoad and ejbStore
    30. SessionSynchronization “special moments”
    31. Coffee Cram: Mock Exam
    32. Coffee Cram: Mock Exam Answers
  18. 10. Exceptions in EJB: When beans go bad
    1. What can go wrong?
    2. Remember, Java exceptions can be checked or unchecked
    3. It’s all about expectations...
    4. In EJB, exceptions come in two flavors: application and system
    5. With an Application Exception, the Container will...
    6. With a System Exception, the Container will...
    7. Warning! RemoteException is checked, but not expected!
      1. A Remote entity bean home interface declares application exceptions and one system exception (RemoteException)
      2. A local entity bean home interface declares only application exceptions
    8. RemoteException goes to remote clients EJBException goes to local clients
    9. Bean Provider’s responsibilities
    10. The Container’s responsibilities
    11. The five standard EJB application exceptions
    12. The five standard application exceptions from the client’s point of view
    13. Common system exceptions
    14. Common system exceptions
    15. Scenarios: what do you think happens?
    16. Scenario Summary
    17. Coffee Cram: Mock Exam
    18. Coffee Cram: Mock Exam Answers
  19. 11. Security in EJB: Protect Your Secrets
    1. Imagine you’re writing a payroll application...
    2. What can you do?
    3. How to do security in EJB
    4. The Application Assembler’s job: access control
    5. Defining the roles
      1. The <security-role> element
    6. Defining the roles... a better way
      1. The <security-role> element with descriptions
    7. Defining the method permissions
      1. The <method-permission> element
    8. Defining the method permissions
      1. Three different ways to specify methods
    9. Method permissions interact with one another as a union!
    10. Watch out for <unchecked/>
    11. The Deployer’s job: mapping actual humans to abstract roles
    12. Principals and Roles, Users and Groups
    13. Class-level vs. instance-level security
      1. When declarative security is not enough, you might need programmatic security to restrict access to a specific instance of a bean.
    14. Using programmatic security to custom-tailor a method
    15. The problem with isCallerInRole()...
      1. the Bean Provider hard codes a role name, but how does he know what roles the App Assembler will use? And what if there’s more than one Bean Provider?
    16. Map declarative security roles to the programmer’s hard-coded (fake) roles
    17. Use <run-as> security identity to pretend someone else is calling...
    18. Security context propagation with <run-as>
    19. Coffee Cram: Mock Exam
    20. Coffee Cram: Mock Exam Answers
  20. 12. A Bean’s Environment: The Joy of Deployment
    1. A bean’s special place- java:comp/env
    2. But it’s not per bean instance... It’s per bean home
    3. It’s simple... if the programmer puts a made-up JNDI name in code, he has to announce that to the deployer in the DD.
    4. Bean Provider and Application Assembler responsibility for the Deployment Descriptor
    5. Deployer responsibility for the Deployment Descriptor
    6. Remembering who does what
    7. Now let’s look at the bean’s runtime environment
    8. Which APIs does EJB 2.0 guarantee?
    9. What MUST be in an ejb-jar?
    10. Structure of an ejb-jar
    11. Programming restrictions
    12. Coffee Cram: Mock Exam
    13. Coffee Cram: Mock Exam Answers
  21. A. Final Mock Exam
    1. Coffee Cram
    2. Coffee Cram: Master Mock Exam
    3. Coffee Cram: Master Mock Exam Answers
    4. This isn’t goodbye
    5. Cover Rough Drafts for the series
  22. B. Interface summary
  23. Index
  24. About the Authors
  25. Copyright

Product information

  • Title: Head First EJB
  • Author(s): Kathy Sierra, Bert Bates
  • Release date: October 2003
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9780596005719