ISBN-10:
0201604469
ISBN-13:
9780201604467
Pub. Date:
05/03/1999
Publisher:
Addison-Wesley
Enterprise JavaBeans: Developing Component-Based Distributed Applications

Enterprise JavaBeans: Developing Component-Based Distributed Applications

Paperback

Current price is , Original price is $39.95. You

Temporarily Out of Stock Online

Please check back later for updated availability.

This item is available online through Marketplace sellers.

Product Details

ISBN-13: 9780201604467
Publisher: Addison-Wesley
Publication date: 05/03/1999
Pages: 324
Product dimensions: 7.37(w) x 9.18(h) x 0.68(d)

Read an Excerpt

PREFACE:

This book discusses Enterprise JavaBeans (EJB). It assumes that you have basic knowledge of Java, but does not assume detailed knowledge of distributed computing. The primary goal of this book is to provide a relatively short and easy-to-read document that will get you up to speed quickly on how to develop EJB. Its secondary goal is to serve as a good companion text for a short course on EJB development.

Chapter 1 provides an overview of EJB—that is, what it is and where it fits into the world of distributed computing. If you're looking for technical details, you won't find them in this chapter. Nevertheless, this discussion provides some background that you can use to convince your manager that EJB is a good thing.

Chapter 2 offers an overview of the EJB architecture. It describes the various components that make up an EJB system and covers the major classes and interfaces used in the development of an EJB bean.

Chapter 3 is the first chapter that contains coding examples. It presents a "hello, world!" EJB system. The "hello world" example was chosen for two reasons. First, in coming to terms with a new technology, a simple example is useful in helping you to figure out the core components of the system. Second, the main intent in this chapter is to highlight the steps you need to perform to write and deploy an EJB bean; in this case, a simpler bean provides fewer distractions from this discussion.

Two types of EJB beans exist: Session beans and Entity beans. Chapter 4 covers Session beans. It discusses the differences between stateful and stateless beans, and provides examples of each.

Chapter 5 discusses Entitybeans. It describes the differences between bean-managed and container-managed entity beans, providing an example of each.

In Chapter 6, the issue is writing client programs that use EJB beans. It includes quite a few examples, including those demonstrating

  • How to obtain and use handles to beans
  • How to call a bean from a servlet
  • How to call a bean from another bean
  • How to call a bean from an applet
  • How to use client-side transaction management
  • How to obtain and examine EJB metadata

Chapter 7 focuses on the process of packaging and deploying EJB beans. It includes a detailed discussion of deployment descriptors and the values they contain. It also examines access control lists and shows how to use them to set up permissions on an EJB server. In addition, Chapter 7 includes an example that shows how a bean can retrieve and test a client's identity.

Chapter 8 contains a set of tips for constructing distributed systems—or any other type of system, for that matter. The goal of this chapter is to provide you with some concrete rules of thumb that you can use in your own development process and to help you avoid making a few common mistakes.

Chapter 9 includes a relatively nontrivial example program (a time management system) and a sort of "implementation diary" in which the author describes the process used to create the example. This process involves assessing the options available, analyzing the tradeoffs among them, and making the final choices.

In Chapter 10—the "wrap-up" chapter—currently available EJB servers are discussed. Currently, relatively few implementations are available, so we also discuss EJB servers that are not available as of this writing, but should be out soon. This chapter concludes with a discussion of the future of the EJB specification; fortune-telling is a risky business, so the predictions generally stick to issues that are certain to be included in future versions of the EJB standard.

As noted earlier, the goal of this book is to provide you with a quick but thorough introduction to EJB—quick, so you can start using EJB soon, and thorough, so that you won't be flummoxed by real-world situations because only a few canned examples were involved. If you have any comments or suggestions for improvement, please send me an e-mail at tvalesky@patriot.net.

It is a fact of life that the EJB specification and its associated documents will undergo some evolution over the next few years. To accommodate these changes, we've made an online supplement to this book available. This supplement will track changes and new developments in the specification, include new examples that demonstrate new capabilities for EJB, and generally keep you up-to-date with what's going on in the EJB world. Be sure to stop by.

A note on Java versions is in order here. The examples in this book were developed in JDK 1.1, and the accompanying WebLogic software should be run under JDK 1.1 Throughout this book, notations have been made where things will change under Java 2 (formerly known as JDK 1.2). For late-breaking news, refer to his book's companion Web site.

A note about the formatting of the code examples: In the source code in this book, all actual new lines begin with a line number. In some cases, the lines were too long to be displayed correctly and have wrapped around to a new line. So, if you see a line in one of the examples like line 15 here:

15 System.out.println("this is a very very very long line"); 16 System.out.println("this is shorter");
rest assured that all is well with the actual code.

Acknowledgments

Thanks to the WebLogic folks, especially Bob Pasker and Sriram Srinivasan. Their support has been first-rate.

Thanks to Jonathan K. Weedon of Inprise Corporation for the CORBA client example.

Thanks to the folks on the EJB-INTEREST list, for many happy hours of conversation about EJB, and for serving as a sounding board for many of my ideas.

Thanks to the team at EJBHOME for developing a freeware EJB implementation. The word will spread faster if the price of entry is not into five figures.

Thanks to the reviewers of this manuscript, James D. Frentiss, Liane Acker, Gary McGraw, Ajit Sagar, J. Patrick Ravenal, Ethan Henry, and Jim Inscore, all of whom helped clarify and refine the manuscript.

Thanks to my teachers.

Thanks to Russ Marshall and the gang at BLS, for giving me my first programming job at a time when I had few qualifications other than enthusiasm, and for showing me the value of good requirements and thorough testing.

Thanks to the folks at Javasoft, for changing the world.

Thanks to David Kodama and Dana Gardner, my long-suffering editors at Advisor Publications, for keeping me honest.

Thanks to the staff at Addison Wesley Longman, especially Elizabeth Spainhour, Mary O'Brien, and Maureen Willard.

Thanks to Mom and Dad for putting me through college. Top o' the world, Ma!

—Tom Valesky

Table of Contents

Chapter 1 The Big Picture 1

Introduction 1
Transaction Processors 1
ACID Properties of Transactions 3
OLTP Versus OLAP 3
Two-Tier, Client-Server Architecture 4
Three-Tier Architecture 7
Sockets 8
RPCs 9
CORBA 9
RMI 10
OLE/DCOM 11
Message Queues 11
Distributed Transaction Processing 12
EJB's Role 13
Conclusion 16
Chapter 2 EJB's Architecture 17

Logical Architecture 17
Overview of EJB's Software Architecture 18
EJB Servers 19
EJB Containers 21
Enterprise Beans 26
A High-Level View of an EJB Conversation 41
Finding the Bean 42
Getting Access to a Bean 43
Calling the Bean's Methods 43
Getting Rid of the Bean 43
RMI Clients 43
CORBA Clients 44
Building and Deploying EJBs 45
Writing the EJB 45
Deploying the EJB 46
Connecting to the EJB 46
Roles in EJB 46
Enterprise Bean Provider 46
Deployer 47
Application Assembler 47
EJB Server Provider 47
EJB Container Provider 48
System Administrator 48
Chapter 3 Hello, EJB! 49

Requirements 49
Design 50
Implementation 51
Step 1: Create the Remote Interface for the Bean 51
Step 2: Create the Bean's Home Interface 54
Step 3: Create the Bean's Implementation Class 56
Step 4: Compile the Remote Interface, Home Interface, and Implementation Class 59
Step 5: Create a Session Descriptor 59
Step 6: Create a Manifest 62
Step 7: Create an ejb-jar File 63
Step 8: Deploy the ejb-jar File 63
Step 9: Write a Client 64
Step 10: Run the Client 67
What'sReally Going on Here? 68
Conclusion 70
Chapter 4 Writing EJB Session Beans 71

When to Use Session Beans 71
Constraints on Session Beans 71
Session Bean Life Cycle 73
Transactions and EJB 78
Stateful Session Bean Example 80
Requirements 80
Design 81
Implementation 83
Summing Up the Stateful Session Bean Example 91
Stateless Session Bean Example 91
Requirements 91
Design 92
Implementation 92
Deploying the Example 100
Conclusion 101
Chapter 5 Writing EJB Entity Beans 103

When to Use Entity Beans 103
Concurrent Use by Several Clients 104
Long Lifetime 104
Survival of Server Crashes 104
Direct Representation of Data in an Underlying Database 105
Bean-Managed Versus Container-Managed Persistence 105
Primary Keys 106
Entity Bean Life Cycle 106
Nonexistence 107
The Pooled State 108
The Ready State 108
Reentrant Instances 109
Example: Container-Managed Persistence 109
Requirements 109
Design 109
Implementation 110
Example: Bean-Managed Persistence 116
Requirements 116
Design 116
Implementation 117
Conclusion 124
Chapter 6 EJB Clients 125

An EJB Bean as a Client to Another Bean 125
The Home Interface 125
The Remote Interface 126
The EJB Client Bean 126
The Client 128
Serializing a Handle 129
The Client 129
Invoking the Client 134
Transactions in Clients 134
Authentication in Clients 137
Getting Metadata 139
A Servlet Client 143
HTML to Make a Call to the Servlet 146
Setting Up WebLogic Servlets 149
An Applet Client 149
The Applet Tag 152
CORBA Client Example 153
What to Look for in a CORBA-Compliant EJB
Implementation 156
HTTP Tunneling and SSL 156
Conclusion 157
Chapter 7 Deployment 159

The DeploymentDescriptor Class 159
The AccessControlEntry Class 161
Back to the DeploymentDescriptor Class (I) 162
The ControlDescriptor Class 163
"Run-as" Modes 164
Back to the DeploymentDescriptor Class (II) 165
The SessionDescriptor Class 165
The EntityDescriptor Class 167
Example Program 168
The Home Interface 168
The Remote Interface 168
The Bean Implementation Class 169
The Client 170
Using Roles at Runtime 173
The ReadDD Class 174
The Deployment Descriptor 177
Setting Up Access Control Lists 178
Container-Managed Finder Methods 178
Other Deployment Issues 179
Caching Issues 179
Persistent Storage 180
Properties 180
Other Administrative Issues 180
Conclusion 182
Chapter 8 Tips, Tricks, and Traps for Building Distributed and Other Systems 183

Expect Your Network Connections to Fail 183
Test Catastrophic Failure 184
Avoid Remote Method Invocations Where Possible 184
Treat Transactions and Database Connections as Precious Resources 184
Monitor the Granularity of Objects 185
Monitor the Granularity of Methods 185
Isolate Vendor-Specific Code 185
Avoid Making Entity Beans Reentrant 185
Observe Programming Restrictions on EJB Beans 186
Don't Implement the Bean's Remote Interface in Its Implementation Class 186
Use Relatively Small and Well-Defined Queries 187
Don't Keep Database Cursors Open 187
Minimize Transactions 187
Minimize Distributed Transactions 188
Avoid Indexing Your Tables 188
Remember That Memory Is Cheap 188
Build in Upward-Scalability 188
Wrap Entity Beans with Session Beans 189
Streamline Your Middleware Methods 189
Put Your Business Logic in the Middle Tier 189
Understand the Tradeoffs Between Isolation Level and Concurrency 189
Avoid Extensive Object Allocation and Deallocation 190
Prototype, Prototype, Prototype 190
Do Load Testing 190
Monitor the Size of Your User Base When Designing an Architecture 190
Separate Transaction-Processing Tables from Reporting Tables 191
If a Database Query Runs Slowly, Review Its Query Plan 191
Keep Joins Simple 191
Have a Database Administrator 192
Use Prepared Statements 192
Have Your Development Environment Mirror Your Production Environment 192
During Load Testing, Use a Sniffer to Monitor Network Traffic 192
Use the Facade Pattern for Interfacing with Legacy Systems 193
Use Patterns 193
Keep Network Topology in Mind 194
Design Security in from the Start 194
Work Closely with Network Personnel 194
Be Aware of Internal Politics 194
Be Aware of the Organizational Culture 195
Be Prepared for Requirements Changes 195
Build One Slice at a Time 196
Build the Difficult Components First 196
Talk to Your Users 196
Keep It Simple 197
Conduct Walkthroughs 197
Use Version Control 198
Use a Code Profiler 198
Establish Your Interfaces Early 198
Build Early and Often 199
Perform Regression Testing 199
Choose Appropriate Test Cases 200
Generate Test Cases While Implementing the Application 200
Automate Everything 200
Understand the Role of Testing 201
Chapter 9 A Nontrival Example 203

Requirements 203
Design 204
Relationships in EJB 204
Relationships in General 205
Detailed Design 205
Database Design 208
Detailed Design of the TimeTracker Class 209
Detailed Design of the Employee Class 211
Designing the TimeSheetHash Class 212
Designing the Client 213
Implementation 213
Building the Database 214
Setting Up the Access Control Lists 214
Implementing the Employee Bean 215
The Final Product 218
Employee Home Interface 218
Employee Remote Interface 219
Employee Implementation 220
Employee Primary Key Class 220
EmployeeInfo Class 221
MyIdentity Class 221
TimeSheetLine Class 222
TimeSheetHash Class 222
The Deployment Descriptor 223
Implementing the TimeTracker Bean 223
The Home Interface 225
The Remote Interface 225
Exceptions 226
Notes About the Implementation Class 227
The Deployment Descriptor 227
Implementing the Real Client 227
The Client Implementation Class 228
An Applet to Run the Client 228
Deployment Issues 229
Conclusion 229
Chapter 10 Implementations and Future Directions 231

EJB Implementations 231
WebLogic 231
EJBHome 232
Other EJB Vendors 232
Future Directions for EJB 236
Sun's EJB Roadmap 237
A Bit of Stargazing 238
Conclusion 238

Appendix A Source Code for Chapter 4 241
Home Interface for Bag Example 241
Remote Interface for the Bag Example 241
InventoryItem Class 242
ItemNotFoundException Exception 242
BagBean Implementation Class 242
BagBean Client 249
Bag2 Home Interface 251
Bag2 Remote Interface 251
Bag2 Bean Implementation 252
Bag2 Client 258
Appendix B Source Code for Chapter 5 261
Implementation of OrderBean (Container-Managed Persistence) 261
Client for Container-Managed Entity Bean 263
Implementation Bean for Bean-Managed Persistence 264
Client for the Bean-Managed Persistence Example 270
Appendix C Source Code for Chapter 9 273
Implementation of the Employee Entity Bean 273
Deployment Descriptor for the Employee Entity Bean 283
Implementation Class for the TimeTracker Session Bean 287
Deployment Descriptor for the TimeTracker Session Bean 293
TimeTracker Client Implementation 298
Glossary 305
Index 311

Preface

Preface This book discusses Enterprise JavaBeans (EJB). It assumes that you have basic knowledge of Java, but does not assume detailed knowledge of distributed computing. The primary goal of this book is to provide a relatively short and easy-to-read document that will get you up to speed quickly on how to develop EJB. Its secondary goal is to serve as a good companion text for a short course on EJB development. Chapter 1 provides an overview of EJB--that is, what it is and where it fits into the world of distributed computing. If you're looking for technical details, you won't find them in this chapter. Nevertheless, this discussion provides some background that you can use to convince your manager that EJB is a good thing. Chapter 2 offers an overview of the EJB architecture. It describes the various components that make up an EJB system and covers the major classes and interfaces used in the development of an EJB bean. Chapter 3 is the first chapter that contains coding examples. It presents a "hello, world!" EJB system. The "hello world" example was chosen for two reasons. First, in coming to terms with a new technology, a simple example is useful in helping you to figure out the core components of the system. Second, the main intent in this chapter is to highlight the steps you need to perform to write and deploy an EJB bean; in this case, a simpler bean provides fewer distractions from this discussion. Two types of EJB beans exist: Session beans and Entity beans. Chapter 4 covers Session beans. It discusses the differences between stateful and stateless beans, and provides examples of each. Chapter 5 discusses Entity beans. It describes the differences between bean-managed and container-managed entity beans, providing an example of each. In Chapter 6, the issue is writing client programs that use EJB beans. It includes quite a few examples, including those demonstrating How to obtain and use handles to beans How to call a bean from a servlet How to call a bean from another bean How to call a bean from an applet How to use client-side transaction management How to obtain and examine EJB metadata Chapter 7 focuses on the process of packaging and deploying EJB beans. It includes a detailed discussion of deployment descriptors and the values they contain. It also examines access control lists and shows how to use them to set up permissions on an EJB server. In addition, Chapter 7 includes an example that shows how a bean can retrieve and test a client's identity. Chapter 8 contains a set of tips for constructing distributed systems--or any other type of system, for that matter. The goal of this chapter is to provide you with some concrete rules of thumb that you can use in your own development process and to help you avoid making a few common mistakes. Chapter 9 includes a relatively nontrivial example program (a time management system) and a sort of "implementation diary" in which the author describes the process used to create the example. This process involves assessing the options available, analyzing the tradeoffs among them, and making the final choices. In Chapter 10--the "wrap-up" chapter--currently available EJB servers are discussed. Currently, relatively few implementations are available, so we also discuss EJB servers that are not available as of this writing, but should be out soon. This chapter concludes with a discussion of the future of the EJB specification; fortune-telling is a risky business, so the predictions generally stick to issues that are certain to be included in future versions of the EJB standard. As noted earlier, the goal of this book is to provide you with a quick but thorough introduction to EJB--quick, so you can start using EJB soon, and thorough, so that you won't be flummoxed by real-world situations because only a few canned examples were involved. If you have any comments or suggestions for improvement, please send me an e-mail at tvalesky@patriot.net. It is a fact of life that the EJB specification and its associated documents will undergo some evolution over the next few years. To accommodate these changes, we've made an online supplement to this book available. This supplement will track changes and new developments in the specification, include new examples that demonstrate new capabilities for EJB, and generally keep you up-to-date with what's going on in the EJB world. Be sure to stop by. A note on Java versions is in order here. The examples in this book were developed in JDK 1.1, and the accompanying WebLogic software should be run under JDK 1.1 Throughout this book, notations have been made where things will change under Java 2 (formerly known as JDK 1.2). For late-breaking news, refer to his book's companion Web site. A note about the formatting of the code examples: In the source code in this book, all actual new lines begin with a line number. In some cases, the lines were too long to be displayed correctly and have wrapped around to a new line. So, if you see a line in one of the examples like line 15 here: 15 System.out.println("this is a very very very long line"); 16 System.out.println("this is shorter"); rest assured that all is well with the actual code. Acknowledgments Thanks to the WebLogic folks, especially Bob Pasker and Sriram Srinivasan. Their support has been first-rate. Thanks to Jonathan K. Weedon of Inprise Corporation for the CORBA client example. Thanks to the folks on the EJB-INTEREST list, for many happy hours of conversation about EJB, and for serving as a sounding board for many of my ideas. Thanks to the team at EJBHOME for developing a freeware EJB implementation. The word will spread faster if the price of entry is not into five figures. Thanks to the reviewers of this manuscript, James D. Frentiss, Liane Acker, Gary McGraw, Ajit Sagar, J. Patrick Ravenal, Ethan Henry, and Jim Inscore, all of whom helped clarify and refine the manuscript. Thanks to my teachers. Thanks to Russ Marshall and the gang at BLS, for giving me my first programming job at a time when I had few qualifications other than enthusiasm, and for showing me the value of good requirements and thorough testing. Thanks to the folks at Javasoft, for changing the world. Thanks to David Kodama and Dana Gardner, my long-suffering editors at Advisor Publications, for keeping me honest. Thanks to the staff at Addison Wesley Longman, especially Elizabeth Spainhour, Mary O'Brien, and Maureen Willard. Thanks to Mom and Dad for putting me through college. Top o' the world, Ma! --Tom Valesky

Customer Reviews

Most Helpful Customer Reviews

See All Customer Reviews