Tài liệu Mastering JavaBeans and the Java 2 Platform Enterprise Edition - Pdf 84

Wiley Computer Publishing
John Wiley & Sons, Inc.
NEW YORK • CHICHESTER • WEINHEIM • BRISBANE • SINGAPORE • TORONTO
Mastering Enterprise
JavaBeans

and the Java

2 Platform,
Enterprise Edition
Ed Roman
Buy this book/CD-ROM set
online at Amazon.com
Publisher: Robert Ipsen
Editor: Robert M. Elliott
Managing Editor: Brian Snapp
Electronic Products, Associate Editor: Mike Sosa
Text Design & Composition: Rob Mauhar
Designations used by companies to distinguish their products are often claimed as trade-
marks. In all instances where John Wiley & Sons, Inc., is aware of a claim, the product names
appear in initial capital or all capital letters. Readers, however, should contact the appro-
priate companies for more complete information regarding trademarks and registration.
Sun, Sun Microsystems, the Sun Logo, Enterprise JavaBeans, Java, and JNDI are trademarks
or registered trademarks of Sun Microsystems, Inc. in the United States and other countries.
This book is printed on acid-free paper.
Copyright © 1999 by Ed Roman. All rights reserved.
Published by John Wiley & Sons, Inc.
Published simultaneously in Canada.
No part of this publication may be reproduced, stored in a retrieval system or transmitted
in any form or by any means, electronic, mechanical, photocopying, recording, scanning or
otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright

—William W. Lee, Chief Technology Officer, The Theory Center
“Enterprise JavaBeans and the J2EE are among the most important technologies
in enterprise computing. Organizations that are exploring or implementing mission-
critical, Web-based, and distributed systems should understand the role that the En-
terprise Java platform can play. Ed Roman has done an excellent job of taking this
complex subject and explaining it in a clear and practical manner. I recommend this
book to anyone who wants to increase their knowledge and expertise in building
robust, ‘real-world’ computing systems.”
—Doug Hibberd, Chief Technology Officer, iMARK.COM
“This book explains all fundamentals of EJB wrapped up in an easy to follow set of
examples. It is easy enough for the beginner and covers enough for more experi-
enced users to like it. It also provides the reader with a guide to what you should
consider when buying an EJB server, as well as a brief look into the future and what’s
coming next in this exciting new technology.”
—Rickard ÖBerg, Software Architect, DreamBean
“This book starts off innocently enough with the idea of explaining Enterprise
JavaBeans. However, by the end, you realize that Ed Roman has effectively un-
wrapped the onion that is today’s multi-tier architecture and shown how J2EE can
revolutionize how systems are architected. I highly recommend this book to any-
one who wishes to keep up with the latest in Java technology and internet systems
architecture.”
—Mike Perham, Senior Web Developer, living.com
ADVANCE PRAISE
Go back to the first page for a quick link to buy this book online!
To my family:
Mom, Dad, and Mike
Go back to the first page for a quick link to buy this book online!
CONTENTS
v
Preface xix

2
EE Technologies
29
Enterprise JavaBeans (EJB)
31
Remote Method Invocation (RMI) and RMI-IIOP
32
Java Naming and Directory Interface (JNDI)
33
Java Database Connectivity (JDBC)
34
Java Transaction API (JTA) and Java Transaction Service (JTS)
35
Java Messaging Service (JMS)
36
Java Servlets and Java Server Pages (JSPs)
37
Java IDL
38
JavaMail
38
Connectors
39
The Extensible Markup Language (XML)
40
Summary 40
Chapter 2 Enterprise JavaBeans Overview 41
Who’s Who in Enterprise JavaBeans 42
Go back to the first page for a quick link to buy this book online!
vi

Remote Accessibility and Location Transparency
64
Glue-Code and Bean Installation Tools
65
Specialized Container Features
65
Summary 67
Part II: Developing Beans 69
Chapter 3 Introduction to Session Beans 71
What Constitutes an Enterprise Bean? 71
The Enterprise Bean Class
72
The EJB Object
73
The Home Object
77
Deployment Descriptors
78
Bean-Specific Properties
81
Ejb-jar File
81
Summary of Terms
82
What Is a Session Bean? 83
Session Bean Lifetime
83
Conversational versus Nonconversational Session Beans
84
All Session Beans’ Methods Are Serialized

97
Only One Way to Initialize Stateless Sessions
98
Containers Can Pool and Reuse Stateless Sessions
98
EJB Object Decoupling
99
Writing a “Hello, World!” Stateless Session Bean 99
Constructing the “Hello, World!” Remote Interface
100
Implementing the “Hello, World!” Bean
101
Constructing the “Hello, World!” Home Interface
104
Writing the Deployment Descriptor
105
The Environment Properties
107
The Ejb-jar File
108
Deploying the Bean
110
Writing Client Code for Our Stateless Bean
111
Running the System
112
Summary 114
Chapter 5 The Basics of Stateful Session Beans 115
Characteristics of Stateful Session Beans 115
Achieving the Effect of Pooling with Stateful Beans

143
viii
MASTERING ENTERPRISE JAVABEANS
Go back to the first page for a quick link to buy this book online!
Specifying Fazuul in EJB
145
Making the Game Data-Driven through Environment Properties
145
Implementing Fazuul
146
Running the Client 170
Summary 174
Chapter 7 Introduction to Entity Beans 175
Persistence Concepts 176
Java Object Serialization
176
Object-Relational Mapping
176
Object Databases
178
What Is an Entity Bean? 179
Files Included with Entity Beans
181
Features of Entity Beans 182
Entity Beans Are Long-Lived
183
Entity Beans Survive Failures
183
Entity Bean Instances Are a View into a Database
183

Bean-Managed Persistence Example: A Bank Account 211
Account.java
212
AccountHome.java
213
AccountPK.java
214
AccountBean.java
214
AccountException.java
227
Client.java
228
ix
Contents
Go back to the first page for a quick link to buy this book online!
The Deployment Descriptor
230
Environment Properties
231
Setting Up the Database
231
Running the Client Program 232
Server-Side Output
232
Client-Side Output
234
Summary 234
Chapter 9 Writing Container-Managed Persistent Entity Beans 235
Container-Managed Fields 235

Resolving Your EJB Debugging Problems 258
Summary 260
Part III: Advanced Enterprise JavaBeans Concepts 261
Chapter 10 Transactions 263
The Motivation for Transactions 264
Atomic Operations
264
Network or Machine Failure
265
Multiple Users Sharing Data
266
The Benefits of Transactions 266
The ACID Properties
268
Transactional Models 270
Flat Transactions
270
x
MASTERING ENTERPRISE JAVABEANS
Go back to the first page for a quick link to buy this book online!
Nested Transactions
272
Other Transactional Models
274
Enlisting in Transactions with Enterprise JavaBeans 274
Transaction Models Supported
274
Underlying Transaction System Abstraction
274
Declarative and Programmatic Transactions

The Java Transaction API (JTA)
294
Declarative versus Programmatic Transactions Example
297
How to Control Transactions from Client Code
298
Designing Transactional Conversations in EJB 299
Summary 302
Chapter 11 CORBA and RMI-IIOP 303
What Is CORBA? 303
CORBA as the Basis for EJB
304
Why Should I Care about CORBA? 304
Benefits of Using CORBA
305
Drawbacks of CORBA
305
Understanding How CORBA Works 306
Object Request Brokers
306
Object Implementations and Object References
308
Object Adapters
308
Repositories
309
OMG’s Interface Definition Language 310
OMG IDL Maps to Concrete Languages
312
Static Invocations

RMI-IIOP Client with a CORBA Object Implementation
326
CORBA Client with an RMI-IIOP Object Implementation
328
The RMI-IIOP API 329
javax.rmi
330
Bootstrapping with RMI over IIOP
331
The Big Picture: CORBA and EJB Together 332
Benefits of CORBA/EJB Interoperability
333
CORBA/EJB Interoperability Scenarios
334
Example Code
335
CORBA EJB Client Example
336
Summary 341
Part IV: J2EE in the Real World: A Sophisticated
E-Commerce Deployment 343
Chapter 12 J2EE in the Real World: Designing an E-Commerce
Object Model 345
A Preview of the Final Product 346
Scoping the Technical Requirements 348
Object Model for the Business Logic Tier
350
Object Model for the Presentation Tier
355
Summary 360

OrderBean.java
389
OrderHome.java
396
OrderPK.java
397
OrderException.java
398
The Deployment Descriptor
398
Summary 400
Chapter 14 J2EE in the Real World: Implementing Our E-Commerce
Session Beans 401
The Quote Line Item Stateful Session Bean 403
QuoteLineItem.java
403
QuoteLineItemBean.java
404
QuoteLineItemHome.java
408
The Deployment Descriptor
408
The Quote Stateful Session Bean 410
Quote.java
411
QuoteBean.java
413
QuoteHome.java
425
QuoteException.java

Enterprise JavaBeans 451
The Role of Servlets in an EJB Deployment 451
Alternatives to Servlets
452
Implementing Our Servlets 453
The Login Servlet
454
The Web Storefront Servlet
461
The Online Catalog Servlet
464
The Product Base Servlet
470
The Product Detail Servlet
473
The Quote Viewing Servlet
476
The Purchasing Servlet
486
The Servlet Properties
492
Running the Complete E-Commerce System 495
Starting the Business Logic Tier
495
Starting the Presentation Tier
495
Starting the Thin Client
495
Optimizations and Design Strategies 496
Unique Primary Key Generation

516
How Objects Are Serialized
516
What Should You Make Transient?
518
Uses of Object Serialization
519
xiv
MASTERING ENTERPRISE JAVABEANS
Go back to the first page for a quick link to buy this book online!
How RMI Simulates Pass by Reference
519
Exporting Your Remote Objects
522
A Simple Example 523
The IFlip Interface
523
The Flip Class
524
The Client
526
Compiling the Program
528
Running the Program
528
Dealing with Remote Exceptions 528
RMI’s Remote Exception limitations
529
RMI Tips 531
Advanced RMI 531

577
Programming with JNDI 577
Setting Up the Required Software
577
The JNDI Packages
578
Basic JNDI Steps
578
Exploring the javax.naming Package
580
Example: An Interactive Browser
582
Advanced JNDI: Combining JNDI with JDBC 593
What Is a JDBC DataSource?
594
Storing Java Objects in Directories
595
xv
Contents
Go back to the first page for a quick link to buy this book online!
Implementing Our JNDI-JDBC Code
596
Advanced JNDI: Combining JNDI with EJB 601
JNDI and EJB Home Objects
601
Other Uses of JNDI
602
Advanced JNDI: Combining JNDI with Java RMI 602
IFlip.java
605

624
XML Summary
629
XML and EJB 629
XML Deployment Descriptors
629
XML as an On-The-Wire Data Format
630
Summary 632
Appendix D Understanding EJB 1.1 635
Portable Deployment Descriptors 635
One Deployment Descriptor Per Ejb-Jar File
636
EJB
1
.
1
XML Deployment Descriptor Example
636
Entity Bean Support Mandated 639
RMI-IIOP API Standardized 639
Everything JNDI 640
How to Look Up Home Objects from Client Code
641
How to Look Up Home Objects from within a Bean
642
How to Look Up Resource Factories
645
How to Look Up Environment Properties
648

.
1
System
661
New Home Handles 664
Other Important Changes in EJB 1.1 664
For More Information 666
Beyond EJB 1.1 666
Summary 667
Appendix E Making a Purchase Decision 669
EJB Specification Compliance 669
Entity Bean Support 670
Persistence 671
Bean-Managed Persistence
671
Container-Managed Persistence
671
Choosing a CORBA-based versus an RMI-based
EJB Product 672
Usage of RMI-IIOP API 672
Accessibility of JTA via JNDI 673
Protocol Issues 673
Integrated Tier Support 673
In-Memory Data Cache 674
Scalability 674
High Availability 675
Security 675
IDE Integration 675
Intelligent Load Balancing 676
Stateless Transparent Fail-over 676

694
EJBMetaData
695
EJBObject
696
EnterpriseBean
697
EntityBean
697
EntityContext
701
FinderException
701
Handle
701
HomeHandle
702
NoSuchEntityException
702
ObjectNotFoundException
702
RemoveException
703
SessionBean
703
SessionContext
704
SessionSynchronization
705
Transaction Reference 706

way of thinking. He said something that baffled and confused me at first, but
would eventually result in a complete paradigm shift and career move for me.
What did Court say? Nothing profound. But simply, “You know Ed, this stuff is
really what Enterprise JavaBeans is for.”
At first, I had no idea what he was talking about. Enterprise JavaBeans? What’s
that? Something like regular JavaBeans? I had no idea. Eventually, Court managed
xx
MASTERING ENTERPRISE JAVABEANS
Go back to the first page for a quick link to buy this book online!
to explain to me what EJB was. And once he explained it, I knew that Trilogy
had to do a 180-degree turn, or Trilogy would lose their competitive advantage.
You see, EJB is a specification for a server-side component marketplace. EJB
enables you to purchase off-the-shelf components from one vendor, combine
them with components from another vendor, and run those components in an
application server written by yet a third vendor. This means companies could
collaborate on the server-side. EJB enables you to buy, rather than build, ele-
ments of server-side applications.
The EJB value proposition had strong ramifications for Trilogy. EJB represented
a way for Trilogy to get out of this middleware business, and concentrate on their
e-commerce strategic efforts. This would mean discarding the backbone com-
pletely in favor of a third party vendor’s architecture. Not only would this re-
duce Trilogy’s maintenance costs, but it would solidify their software suite, since
their middleware would now be written by professionals who had been in the
business for twenty years. This proposition would eventually lead to an entirely
new business unit forming at Trilogy.
So I decided to start researching EJB and pushing for Trilogy to adopt it. I went
to the Sun Microsystems Web page and downloaded the EJB 1.0 specification
in PDF form and printed it out. Back then, the specification was about a third
of the size it’s grown to today.
Understanding the specification turned out to be much more challenging than

this book is complemented by a brand-new enterprise bean. This is not a book
with a single code example that flows for the entire text, because that would
give you a very narrow view of the kinds of domain models you can build with
EJB. So prepare yourself, because together we will develop thirteen enterprise
beans over the course of this book. We’ll also write other small modules, such
as servlet code, JNDI code, RMI code, XML code, and more, to give you a taste
for the Java 2 Platform, Enterprise Edition (J2EE) suite.
My hope is that I can save you time and energy, and aid you in designing well-
crafted server-side deployments. But this is merely the beginning. The EJB
marketplace is just getting started, and there’s a whole lot more work ahead to
do. I encourage you to take an active role in the middleware industry, and to
work with me taking EJB to the next level. Feel free to e-mail me your experi-
ences, tips, and design strategies, and I’ll post them on the book’s accompany-
ing Web site to share with others. Our goal is to increase our knowledge of EJB
as a community, and together we can do it.
Sincerely,
Ed Roman
On an airplane home from the PLoP (Pattern Languages of Programming)
conference, 1999
xxii
E
d Roman is one of the world’s leading authorities on high-end middleware tech-
nologies. He has been actively involved with Sun Microsystems’ enterprise Java
solutions from their inception, and has designed, built, and deployed a variety
of enterprise applications, including architecting and developing complete appli-
cation server products. He routinely devotes a significant amount of time towards
influencing and refining Sun’s enterprise specifications, is a regular contributor
to middleware interest mailing lists, and regularly speaks at middleware-related
conferences.
Ed is the CEO of The Middleware Company (www.middleware-company.com).

Technologies Covered in This Book
The Java 2 Platform, Enterprise Edition (J2EE) is a sophisticated suite of en-
terprise APIs that enable you to write robust, scalable, and multiuser secure
deployments. J2EE is huge, and it spawns a multitude of concepts. The major
parts of the J2EE platform that we cover are everything you need to begin ad-
vanced programming with EJB. This means you only need to approach this book
with understanding of the Java language because we will teach you everything
you need beyond that.
We cover the following J2EE technologies:
■■
Enterprise JavaBeans (EJB) version 1.0, found throughout the book.
xxiv
MASTERING ENTERPRISE JAVABEANS
Go back to the first page for a quick link to buy this book online!
■■
The latest information about programming with the new EJB version 1.1,
covered in Appendix D.
■■
How to use Java Database Connectivity (JDBC) with enterprise beans,
covered in Chapter 8.
■■
The Java Transaction API (JTA), covered in Chapter 10, with a real-world
example in Chapter 14.
■■
CORBA and RMI-IIOP, covered in Chapter 11.
■■
Servlets and EJB, covered as part of a large e-commerce example in Chap-
ter 15.
■■
Java Remote Method Invocation (RMI), covered in Appendix A.

take a whirlwind tour of EJB, which serves as a great overview for people in
a hurry. While Part I is essential information to EJB newcomers, veterans will
also find nuggets of useful knowledge as well. There are two chapters in Part I.
Part II devotes exclusive attention to programming with EJB. We’ll see how to
write both kinds of enterprise beans: session beans and entity beans. We’ll
cover the basics of writing each type of bean, including extensive examples.
We’ll see both types of session beans (stateful and stateless), as well as both
types of entity beans (bean-managed persistent and container-managed per-
sistent). There are seven chapters in Part II.
Part III covers advanced concepts that are related to EJB and J2EE. We’ll learn
the fundamentals of transactions and understand why they are necessary for
a robust deployment. We’ll also take a look at the Common Object Request
Broker Architecture (CORBA) and study how it related to EJB and the J2EE
platform. There are two chapters in Part IV.
Part IV shows how to use EJB and the J2EE platform in the real world. We’ll
develop an extensive e-commerce deployment that illustrates how to build
an e-commerce Web site using the J2EE platform. We’ll begin with an analy-
sis of our deployment’s requirements, and from there we’ll design a set of
enterprise beans and Java servlets that fulfill those requirements. We’ll then
implement each of these components and show you how to put it all together
to make the deployment go live. By the time you’re done reading Part IV, you
should have a firm grasp on how EJB and the J2EE platform can be used to
solve real-world problems. There are four chapters in Part IV.
The Appendices plunge into the concepts and APIs that form the building-
blocks for the J2EE platform. EJB is put aside to introduce Java Remote
Method Invocation (RMI), the Java Naming and Directory Interface (JNDI),
and the Extensible Markup Language (XML). Each appendix is devoted to
one of these technologies. Within each appendix, we first introduce the
technology’s core concepts, quickly moving from basic concepts to advanced
development. Each appendix concludes by relating the knowledge you’ve just

the examples become more complex, exercising a significant portion of each
API that’s introduced. I’ve also tried to develop useful utilities from the examples
that you can use in practice. For example, Appendix A (which covers Java Re-
mote Method Invocation) introduces a simplified message queue that’s based
on RMI. Appendix B (which covers the Java Naming and Directory Interface)
walks you through a browser that you can use to interact with different kinds
of directory structures. Part IV (multiple chapters illustrating how to deploy an
e-commerce solution with the J2EE platform) has several reusable enterprise
beans that you can extend for your own purposes.
The goal of these efforts is to give you a breadth of understanding beyond just
“Hello, World!” despite the newness of EJB. I hope you find this to be the case.
The Included CD-ROM
On the accompanying CD-ROM, you will find all the source code you see in this
book. The code comes complete with makefiles, ready to build and run with the
BEA WebLogic server. Note that the code has been built from the ground-up,


Nhờ tải bản gốc
Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status