Mastering Enterprise Javabeans the Bestselling Classic Is Back—And Covers the New EJB 2.1 Specification!
Total Page:16
File Type:pdf, Size:1020Kb
576828 Cover 10/26/04 9:37 AM Page 1 Programming Languages/Java $45.00 USA/$64.99 CAN/£29.99 UK Mastering Enterprise JavaBeans The bestselling classic is back—and covers the new EJB 2.1 specification! Building on the overwhelming success Serving as the ultimate resource that Ed Roman is an independent of his previous two editions, renowned boasts the most up-to-date information consultant, a leading authority author Ed Roman has returned—along on EJB, this edition begins with the on EJB, and the author of the Mastering with Enterprise JavaBeans (EJB) gurus fundamentals of building an EJB system first two bestselling editions of Gerald Brose and Rima Patel Sriganesh— and gradually moves on to a discussion Mastering Enterprise JavaBeans to offer you the inside scoop on the of programming with EJB. You’ll learn (Wiley). EJB 2.1 specification and related about advanced EJB concepts and best enhancements in J2EE™ 1.4. Continuing practices that you won’t find anywhere Rima Patel Sriganesh is a member the tradition of providing you with an else, such as transactions, persistence, of Technical Staff in the unparalleled EJB tutorial, this expert clustering, integration, performance Technology Outreach group at Enterprise team of authors has revised more than monitoring, security, and choosing an Sun Microsystems, Inc., and is 30 percent of the book and added new EJB server. Along the way, you’ll get a coauthor of Developing Java chapters that explore the dramatic in-depth coverage of: Web Services (Wiley). ™ changes in the way EJB is now used for • EJB security mechanisms building applications. Gerald Brose is a Security • Best practices for EJB application Software Architect at Xtradyne development, deployment, and Technologies. He is an expert in testing EJB and CORBA and the author of Java Programming with JavaBeans • Tips for selecting appropriate Web CORBA, Third Edition (Wiley). frameworks and EJB containers • EJB integration with back-end enterprise information systems Third Edition using J2EE Connector technology The companion Web site includes sample code, updates to the book, • Performance optimizations for and links to useful software used various types of EJB in the book. ™ Third Edition Wiley Technology Publishing Timely. Practical. Reliable. Visit our companion Web site at www.wiley.com/compbooks/roman Also visit www.TheServerSide.com Roman Ed Roman Sriganesh ISBN: 0-7645-7682-8 Brose Rima Patel Sriganesh *85555-IJDJEi ,!7IA7G4-fhgicj!:p;o;p;K;K Gerald Brose 01_576828 ffirs.qxd 11/3/04 11:36 AM Page i Mastering Enterprise JavaBeans™ Third Edition Ed Roman Rima Patel Sriganesh Gerald Brose 01_576828 ffirs.qxd 11/3/04 11:36 AM Page ii Mastering Enterprise JavaBeans™, Third Edition Published by Wiley Publishing, Inc. 10475 Crosspoint Boulevard Indianapolis, IN 46256 www.wiley.com Copyright © 2005 by Ed Roman, Gerald Brose, and Rima Patel Sriganesh Published by Wiley Publishing, Inc., Indianapolis, Indiana Published simultaneously in Canada ISBN: 0-7645-7682-8 Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1 3B/RX/RR/QU/IN 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 Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Legal Department, Wiley Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4355, e-mail: [email protected]. Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with respect to the accuracy or completeness of the contents of this work and specif- ically disclaim all warranties, including without limitation warranties of fitness for a particular purpose. No warranty may be created or extended by sales or promotional materials. The advice and strategies contained herein may not be suitable for every situation. This work is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other profes- sional services. If professional assistance is required, the services of a competent professional per- son should be sought. Neither the publisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Website is referred to in this work as a citation and/or a potential source of further information does not mean that the author or the publisher endorses the information the organization or Website may provide or recommendations it may make. Further, readers should be aware that Internet Websites listed in this work may have changed or disap- peared between when this work was written and when it is read. IN NO EVENT SHALL THE AUTHOR OR PUBLISHER BE LIABLE TO ANY PERSON FOR ANY INCIDENTAL, INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, INCLUDING WITHOUT LIMITATION TO, LOSS OF PROFITS, LOSS OF DATA, BUSINESS INTERRUPTION OR ANY AND ALL OTHER SIMILAR DAMAGES OR LOSS, EVEN IF AUTHOR OR PUBLISHER OR THEIR SUP- PLIERS OR THEIR AGENTS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. For general information on our other products and services or to obtain technical support, please contact our Customer Care Department within the U.S. at (800) 762-2974, outside the U.S. at (317) 572-3993 or fax (317) 572-4002. Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books. Library of Congress Control Number: 2004114268 Trademarks: Wiley, the Wiley Publishing logo and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and other countries, and may not be used without written permission. Enterprise JavaBeans is a trademark of Sun Microsystems, Inc. in the U.S. or other countries. All other trademarks are the property of their respective owners. Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book. 01_576828 ffirs.qxd 11/3/04 11:36 AM Page iii To my wonderful wife, Christine, and to our boys, Johannes and Julius. Rima wishes to dedicate this book to her dearest and loving husband Sriganesh, and her most wonderful parents. 01_576828 ffirs.qxd 11/3/04 11:36 AM Page iv Credits Acquisitions Editor Project Coordinator Robert M. Elliott Erin Smith Development Editor Graphics and Production Specialists Sydney Jones Sean Decker Kelly Emkow Technical Editor Jennifer Heleine Floyd Marinescu Quality Control Technician Copy Editor Brian H. Walls Michael Koch Proofreading and Indexing Editorial Manager TECHBOOKS Production Services Kathryn Malm Bourgoine Vice President & Executive Group Publisher Richard Swadley Vice President and Publisher Joseph B. Wikert iv 02_576828 ftoc.qxd 11/3/04 11:36 AM Page v Contents Acknowledgments xvi Introduction xvii Part One Overview 1 Chapter 1 Overview 3 The Motivation for Enterprise JavaBeans 4 Component Architectures 7 Service-Oriented Architectures 8 Divide and Conquer to the Extreme with Reusable Services 9 Introducing Enterprise JavaBeans 11 Why Java? 12 EJB as a Business Tier Component 13 The EJB Ecosystem 15 The Bean Provider 16 The Application Assembler 16 The EJB Deployer 17 The System Administrator 17 The Container and Server Provider 18 The Tool Vendors 18 Summary of Roles 19 The Java 2 Platform, Enterprise Edition (J2EE) 21 The J2EE Technologies 22 Summary 26 Chapter 2 EJB Fundamentals 27 Enterprise Beans 27 Types of Beans 28 Distributed Objects: The Foundation for EJB 30 Distributed Objects and Middleware 32 v 02_576828 ftoc.qxd 11/3/04 11:36 AM Page vi vi Contents Explicit Middleware 33 Implicit Middleware 34 What Constitutes an Enterprise Bean? 36 The Enterprise Bean Class 36 The EJB Object 37 The Home Object 42 The Local Interfaces 44 Deployment Descriptors 48 Vendor-Specific Files 49 Ejb-jar File 49 Summary of Terms 50 Summary 52 Chapter 3 Writing Your First Bean 53 How to Develop an EJB Component 54 The Remote Interface 55 The Local Interface 56 The Home Interface 57 The Local Home Interface 59 The Bean Class 61 The Deployment Descriptor 64 The Vendor-Specific Files 65 The Ejb-jar File 65 Deploying the Bean 66 The Optional EJB Client JAR File 67 Understanding How to Call Beans 68 Looking up a Home Object 68 Running the System 72 The Server-Side Output 73 The Client-Side Output 73 Implementing Component Interfaces 73 A Solution 74 Summary 75 Part Two The Triad of Beans 77 Chapter 4 Introduction to Session Beans 79 Session Bean Lifetime 79 Session Bean Subtypes 80 Stateful Session Beans 80 Stateless Session Beans 81 Special Characteristics of Stateful Session Beans 83 Achieving the Effect of Pooling with Stateful Beans 83 The Rules Governing Conversational State 84 Activation and Passivation Callbacks 85 Method Implementation Summary 88 A Simple Stateful Session Bean 88 Life Cycle Diagrams for Session Beans 98 Summary 102 02_576828 ftoc.qxd 11/3/04 11:36 AM Page vii Contents vii Chapter 5 Writing Session Bean Web Services 103 Web Services Concepts 103 Web Services Standards 106 XML Artifacts and Platform Independence 109 Implementing a Web Service 110 The JAX-RPC Service Endpoint Interface 111 WSDL and the XML/Java Mapping