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