Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Mastering Enterprise JavaBeans™ and the Java 2 Platform, Enterprise Edition - Roman E

..pdf
Скачиваний:
41
Добавлен:
24.05.2014
Размер:
6.28 Mб
Скачать

A B O U T T H E A U T H O R

d Roman is one of the world’s leading authorities on high-end middleware tech- Enologies. 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 application 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). Via on-site training courses, The Middleware Company educates developers and managers on the latest server-side technologies. They also aid in the development of custom middleware solutions. This includes making an application server purchase decision (EJB/COM+), integration paths for migrating legacy systems, and working with Internet-based e-commerce deployments.

xxii

Go back to the first page for a quick link to buy this book online!

I N T R O D U C T I O N

his book is a tutorial on Enterprise JavaBeans (EJB). It’s about EJB concepts, Tmethodology, and development. You’ll see many, many examples of Enterprise JavaBeans throughout this book, giving you a practical understanding of the subject. This book is also about Java 2, Enterprise Edition (J2EE), a software platform for developing robust enterprise applications, of which EJB is an essential component. By reading this book, you will acquire a deep understand-

ing of EJB and J2EE.

Make no mistake about it—what you are about to read is not an easy subject. EJB and J2EE incorporate concepts from a wealth of areas, including distributed computing, databases, security, component-driven software, and more. Combining them together is a magnificent stride forward for the Java community, but with that goes a myriad of concepts to learn and understand. This book will teach you the concepts and techniques for authoring reusable components in Java, and it will do so from the ground up. You only need to understand Java in order to understand this book.

While you’re reading this book, you may want to download the appropriate specifications, such as the EJB and J2EE specifications, available on the Sun Microsystems Web site. See the book’s accompanying Web site for links to these specifications, as they complement this book nicely.

Technologies Covered in This Book

The Java 2 Platform, Enterprise Edition (J2EE) is a sophisticated suite of enterprise 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 advanced 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.

xxiii

Go back to the first page for a quick link to buy this book online!

xxiv M A S T E R I N G E N T E R P R I S E J A V A B E A N S

■■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 Chapter 15.

■■Java Remote Method Invocation (RMI), covered in Appendix A.

■■The Java Naming and Directory Interface (JNDI), covered in Appendix B.

■■The Extensible Markup Language (XML), an ancillary technology that is used in J2EE, covered in Appendix C.

Technologies Not Covered in This Book

This book does not cover several enterprise Java technologies. For one, we do not cover the Java Message Service (JMS). JMS allows for asynchronous distributed object communications. Unfortunately, the current EJB specification (revision 1.1) does not include support for JMS. Sun Microsystems is promising that EJB 2.0 will include JMS support.

We also do not cover Java Server Pages (JSPs). JSPs enhance your enterprise deployment with a Web-based presentation layer. The closest technology to JSPs that we cover are Java servlets in Part IV (JSPs are compiled into servlets at runtime).

Finally, we do not cover the JavaMail API in this book. JavaMail is part of the Java 2 Platform, Enterprise Edition architecture, and is useful for performing mail operations in Java. JavaMail is useful in e-commerce deployments for sending a confirmation e-mail when purchasing goods online. See the book’s accompanying Web site for links to JavaMail resources.

Organization of the Book

The text is organized into the following five parts.

Part I begins with a tour of enterprise computing. We’ll talk about components, distributed frameworks, multitier deployments, and the various competing

Go back to the first page for a quick link to buy this book online!

Introduction xxv

architectures out there, including the Java 2, Enterprise Edition (J2EE) platform. We’ll have a look at where J2EE fits into the grand scheme of things, and we’ll explore the role that EJB plays within the J2EE platform. We’ll also 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 persistent). 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 analysis 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 buildingblocks 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 learned to EJB programming. Depending on your background, you may not need to read all of the appendices. I encourage all readers to review the latter half of each appendix, so that you understand how each technology is related to EJB. Appendix D moves on to cover what’s new in the EJB 1.1 specification, and Appendix E is a guide for making an EJB product purchase decision. Finally, Appendix F is a quick reference for programmers to use during EJB development. It includes diagrams illustrating what’s really going on in an EJB system, a guide to the core EJB API, and a transaction reference.

Go back to the first page for a quick link to buy this book online!

xxvi M A S T E R I N G E N T E R P R I S E J A V A B E A N S

Illustrations in the Text

Almost all of the illustrations in this book are written in the Unified Modeling Language (UML). UML is the de facto standard methodology for illustrating software engineering concepts in an unambiguous way. If you don’t know UML, pick up a copy of The Unified Modeling Language’s Users Guide, which illustrates how to effectively use UML in your everyday software. UML is a highly important achievement in Object-Oriented Methodology. It’s a common mechanism for engineers to communicate and design, and it forces you to abstract your object model and object prior to implementation. I cannot stress its use enough.

Examples Used

While writing this book, I asked some developer friends what they hate most in technical books. Other than obvious things, such as source code not working, the biggest complaint I found is that the books do not go deep enough. Specifically, many people feel that the canonical “Hello, World!” examples, while they may have their merit, are insufficient for revealing the intricacies of the technology being explained.

In this book, I have tried to keep the examples as robust and relevant as possible. The examples start out simple, so that first-time users as well as veterans will have simple templates to use to write their code. But as each chapter progresses, 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 Remote 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,

Go back to the first page for a quick link to buy this book online!

Introduction xxvii

adhering to the EJB specification, and contains nothing specific to WebLogic. With minor changes, you should be able to run this book’s code on the application server of your choice, assuming it fully implements the EJB specification. The one major step that will change between application servers is the deployment step. Be sure to consult with your vendor’s documentation for details on this.

In addition to the code, you’ll also find on the CD-ROM:

■■An evaluation copy of the BEA WebLogic EJB application server

■■An evaluation copy of the BEA WebLogic Commerce Server 1.7.1

■■ObjectSpaceJGL 3.1

■■Complete ready-to-use sample code

The Accompanying Web Site

This book would not be complete without a way to keep you in touch after the book was published. In addition to the CD-ROM, there is a Web site available for resources related to this book. There you’ll find:

■■Error corrections from the text

■■Links to EJB resources

■■Any updates to the source code examples

Before reading on, I would go to this Web site immediately to get any changes or updates that have happened since the book was first published. The Web site is at:

■■ www.wiley.com/compbooks/roman

Feedback

When you begin your EJB programming, I’m sure you’ll have many experiences to share with other readers as well. Feel free to e-mail me examples, case studies, horror stories, or tips that you’ve found helpful in your experiences, and I’ll post them on the Web site.

Acknowledgments

This book has been over a year in the making, and I am proud to say it is the finest work I have produced in my life. What made the book a reality, however, were the many people that aided in its development.

Go back to the first page for a quick link to buy this book online!

xxviii M A S T E R I N G E N T E R P R I S E J A V A B E A N S

First, hats off to my review panel, including Anne Thomas, Rickard ÖBerg, Mike Perham, Doug Hibberd, Simon North, William Lee, Roger Sessions, Mike Roman, Charles Erickson, the folks at Net.Quotient, and anyone else I may have left off. You have simply been awesome, and I couldn’t have done it without you.

Thanks to my love, Youn Hi, who endured the rough times when I was holding down a job and writing this book simultaneously, and lived through the hardship when I quit my job to work on this book full-time, giving the book the attention it deserved.

Thanks to my friends: Jonah Probell, Luke Benes, Henry Tran, Mike Uzquiano, DJ Piglet, Todd Snyder, Bryan Vandrovec, Maurice Garfinkel, Katie, Adit, James Kao, Lawrence Eng, José Gonzales, Dave Frank, Charles Erickson, Ahmed Gheith, Shawn Smith, Bindu and Richard Rao, Jian Song, Will Ballard, Doug Hibberd, Sean Hoffman, Daan DeBrouckere, Charles Erickson, Mike Perham, Alex Bentley, Jeff Ragusa, Sammy Wu, TU97.5, Scott Merriam, Galvin, Jeremy Deutsch, and V.

Thanks to the great folks over at John Wiley & Sons publishing. They have been absolutely outstanding throughout this book’s evolution. In particular, I’d like to thank Bob Elliott, Brian Snapp, and Emilie Herman for their incredible efforts.

Go back to the first page for a quick link to buy this book online!

P A R T ONE

Overview

n Part I, we introduce the server-side development platform that is the Java 2

IPlatform, Enterprise Edition (J2EE), of which the Enterprise JavaBeans (EJB) component architecture is a vital piece. J2EE is a conglomeration of concepts, programming standards, and innovations—all written in the Java programming language. With J2EE, you can rapidly construct distributed, scalable, reliable, and portable secure server-side deployments.

Chapter 1 begins by exploring the need for a server-side development platform such as J2EE. You’ll see the rich needs of server-side computing, such as scalability, high availability, resource management, and security. You’ll also see the need for a rapid application development component architecture such as EJB and COM+. We’ll wrap up by surveying Sun Microsystems’ J2EE, a complete server-side development platform.

Chapter 2 moves on to the Enterprise JavaBeans component model. We’ll take a look at how EJB empowers heterogeneous vendors to collaborate to solve a business problem, and we’ll study the roles of each party in an EJB deployment. We’ll also look at the different functional software modules in an EJB deployment and how they relate.

1

Go back to the first page for a quick link to buy this book online!

C H A P T E R1

Server-side Component

Architectures

nterprise JavaBeans (EJB) is a server-side component architecture that enables Eand simplifies the process of building enterprise-class distributed object applications in Java. By using EJB, you can write scalable, reliable, and secure applications without writing your own complex distributed object framework. EJB is about rapid application development for the server side; you can quickly and easily construct server-side components in Java by leveraging a prewritten distributed infrastructure provided by the industry. EJB is designed to support application portability and reusability across any vendor’s enterprise middleware

services.

If you are new to enterprise computing, these concepts will be made very clear shortly. EJB is a complicated subject and thus deserves a thorough explanation. In this chapter, we’ll discusses the main concepts surrounding Enterprise JavaBeans. This starts with a discussion about what’s involved in writing enterprise software and why a prepackaged distributed object architecture such as Enterprise JavaBeans simplifies your life. From this discussion, we’ll have a greater insight into why a server-side component architecture makes sense, as well as a feature list of what we’d like to see when we choose an architecture for developing server-side distributed object applications.

We’ll then examine several endeavors by the industry to address these enterprise needs. The highlight of this discussion—as well as this book—is Sun’s Java 2 Platform, Enterprise Edition (J2EE). J2EE is a collection of enterprise technologies, of which EJB is an integral part. By understanding and using J2EE properly, you can build portable, object-oriented, enterprise-class applications in Java.

3

Go back to the first page for a quick link to buy this book online!

4

 

M A S T E R I N G E N T E R P R I S E J A V A B E A N S

The Need for a Server-Side Component Architecture

To understand the value EJB brings to the table, we first must examine the needs that developers commonly have when authoring and deploying components in a server-side environment. As we uncover the issues surrounding server-side development, we’ll begin to see the need for a standardized architecture such as EJB.

Software Components

We begin our discussion with a look at software components. A software component is code that implements a set of well-defined interfaces. It is a manageable, discrete chunk of logic. Components are not entire applications—they cannot run alone. Rather, they can be used as puzzle pieces to solve some larger problem.

The idea of software components is very powerful. A company can purchase a well-defined module that solves a problem and combine it with other components to solve larger problems. For example, consider a software component that computes the price of goods. We’ll call this a pricing component. You hand the pricing component information about a set of products, and it figures out the total price of the order.

The pricing problem can get quite hairy. For example, let’s assume we’re ordering computer parts, such as memory and hard drives. The pricing component figures out the correct price based on a set of pricing rules such as:

Base prices of a single memory upgrade or a single hard disk

Quantity discounts that a customer receives for ordering more than 10 memory modules

Bundling discounts that the customer receives for ordering both memory and a hard disk

Preferred customer discounts that you can give to big-name customers

Locale discounts depending on where the customer lives

Overhead costs such as shipping and taxes

These pricing rules are in no way unique to ordering computer parts. Other industries, such as health care, appliances, airline tickets, and others need the same pricing functionality. Obviously, it would be a huge waste of resources if each company that needed complex pricing had to write its own sophisticated pricing engine. Thus, it makes sense that a vendor provides a generic pricing component that can be reused over and over again for different customers. For example:

Go back to the first page for a quick link to buy this book online!