Using enterprise javabeans 2 0 2002 2002

1.1K 64 0
Using enterprise javabeans 2 0 2002 2002

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

• Table of Contents • Examples Special Edition Using Enterprise JavaBeans™ 2.0 By Chuck Cavaness, Brian Keeton Publisher : Que Pub Date : September 19, 2001 ISBN : 0-7897-2567-3 Pages : 648 Slots : 1 Special Edition Using Enterprise JavaBeans 2.0 starts with a description of how Enterprise JavaBeans fits into the big picture of J2EE development, then covers such topics as: Locating EJB's using JNDI Managing data with EJB Query Language Building JMS applications using the new Message-driven Bean Planning EJB applications using design patterns Later chapters describe advanced development topics including interoperability, horizontal services and clustering Throughout the book, the authors construct a component-based auction web site using the J2EE architecture as a practical example • Table of Contents • Examples Special Edition Using Enterprise JavaBeans™ 2.0 By Chuck Cavaness, Brian Keeton Publisher : Que Pub Date : September 19, 2001 ISBN : 0-7897-2567-3 Pages : 648 Slots : 1 Copyright About the Authors Acknowledgments Tell Us What You Think! Introduction This Book Is for You How This Book Is Organized Conventions Used in This Book Part I: Developing Enterprise JavaBeans Chapter 1 Introduction to Enterprise Applications Chapter 2 Setting the Stage–An Example Auction Site Chapter 3 EJB Concepts The Enterprise JavaBeans Architecture Component-Based Distributed Computing N-Tiered Architectures Why Use EJB? The Auction Example Overview of an English Auction Choosing the Use Cases to Implement Defining the Object Model What's Next? Grasping the Concepts Early What Is an Enterprise Bean? EJB Roles and Their Responsibilities Local Versus Remote EJB Clients Using RMI to Communicate with Enterprise JavaBeans Accessing an EJB Through Its Component Interface Locating Enterprise Beans Using the Home Interface Deciding Whether to Use a Local or Remote Client Creation and Removal of EJBs Passivation and Activation Object Pooling Handles The EJBMetaData Class EJB Server and Container Implementations Chapter 4 Java Naming and Directory Interface Chapter 5 Entity Beans Chapter 6 Bean-Managed Persistence Why Applications Need Naming and Directory Services Overview of the JNDI Architecture Selecting and Configuring a JNDI Provider The JNDI Environment Properties Setting the JNDI Environment Properties The Context and InitialContext Objects Getting the Environment for a Context Object Using the lookup Method to Locate JNDI Resources Locating EJB Objects Accessing an EJB's Environment Establishing Security Through the InitialContext JNDI and Clustering Troubleshooting What Is an Entity Bean? Declaring the Component Interface Defining the Primary Key Class Declaring the Home Interface Implementing an Entity Bean Inheritance and Entity Beans Are Entity Beans Worth It? Choosing to Manage Persistence Yourself JDBC Primer Configuring a Data Source Creating an Entity Bean Loading and Storing an Entity Accessing Other Entity Beans Implementing Finder Methods Deleting an Entity Deploying an Entity Bean Using BMP Troubleshooting Chapter 7 Container-Managed Persistence Chapter 8 EJB Query Language Chapter 9 Session Beans Building Portable Entity Beans Declaring a CMP Entity Bean Implementing the Container Callback Methods Deploying an Entity Bean Using CMP Managing Relationships Using EJB 1.1 CMP Troubleshooting What Is the EJB Query Language? Defining a FROM Clause Defining a WHERE Clause Defining a SELECT Clause Using the Built-In Functions EJB QL Syntax in BNF Notation Troubleshooting What Is a Session Bean? Differences Between Stateless and Stateful Session Beans Declaring the Component Interface Declaring the Home Interface Chapter 10 Java Message Service Implementing a Session Bean Deploying a Session Bean Reentrant Issues Troubleshooting Introduction to Messaging Components of the JMS Architecture The Two JMS Message Models The JMS Interfaces The Details of a JMS Message Message Selection and Filtering Using the JMS Point-to-Point Model Using the JMS Publish/Subscribe Model Synchronous Versus Asynchronous Messaging Message Persistence Using Transactions with JMS Using JMS with Enterprise JavaBeans Troubleshooting Chapter 11 Message-Driven Beans Chapter 12 Transactions Chapter 13 Exception Handling Chapter 14 Security Design and Management What Are Message-Driven Beans? The Message-Driven Bean and the Container Using Message-Driven Beans with EJB Using JMS Queues or Topics with Message-Driven Beans Creating a Message-Driven Bean Deploying a Message-Driven Bean Sending Messages to a Message-Driven Bean Acknowledging Messages from Message-Driven Beans Using Transactions with Message-Driven Beans Troubleshooting Understanding Transactions Passing the ACID Test Programming with the Java Transaction API Using Container-Managed Transactions Using Bean-Managed Transactions Using Client-Demarcated Transactions Isolating Access to Resources Troubleshooting EJB Exception Handling Application Exceptions System Exceptions Exceptions and Transactions Packaging Exceptions Troubleshooting The Importance of Application Security Understanding Your Application's Security Requirements Basic Security Concepts Java Security Fundamentals Using Security with Enterprise JavaBeans and J2EE Sketching Out the Auction Security Java Authentication and Authorization Service (JAAS) Chapter 15 Deployment Deployment Descriptors and EJB Roles Bean Provider Responsibilities Application Assembler Responsibilities Deployer Responsibilities Packaging EJBs Troubleshooting Part II: Design and Performance Chapter 16 Patterns and Strategies in EJB Design Chapter 17 Addressing Performance Chapter 18 Performance and Stress Testing Your Applications What Are Patterns? Strategies for Enterprise JavaBeans Designing EJB Classes and Interfaces Managing Client Access Implementing a Singleton in EJB Troubleshooting The Role of Performance in Design Minimizing Remote Calls Optimizing Entity Bean Persistence Building a Pick List Managing Transactions Troubleshooting Why Stress Test Your Applications? Performance Testing Your Beans Stress Testing Your Beans Using ECperf 1.0 Troubleshooting Part III: Building the Web Tier Chapter 19 Building a Presentation Tier for EJB The Different Types of Presentation Tiers Using a Faỗade Pattern to Hide EJB Using Servlets and Java Server Pages with EJB Using JSP Tag Libraries Using the Struts Open-Source Framework Caching on the Web Server or in Stateful Session Beans Part IV: Advanced Concepts Chapter 20 Distribution and EJB Interoperability Chapter 21 Horizontal Services Chapter 22 EJB Clustering Concepts Chapter 23 EJB 2.0 Programming Restrictions Interoperability Overview Portability Versus Interoperability EJB 2.0 Interoperability Goals The Relationship Between CORBA and Enterprise JavaBeans Remote Invocation Interoperability Transaction Interoperability Naming Interoperability Security Interoperability What Are Horizontal Services? Horizontal Services Provided by EJB Traditional Buy Versus Build Analysis Auction Example Horizontal Services Building the Auction Logging Service Java 1.4 Logging API Building an E-Mail Horizontal Service Troubleshooting Too Much Isn't Always a Good Thing What Is Clustering? Clustering in the Web Tier Clustering in the EJB Tier Single VM Versus Multiple VM Architectures The Purpose of the Restrictions The EJB 2.0 Restrictions Summary Part V: Appendixes Appendix A The EJB 2.0 API Interfaces Exceptions The javax.ejb.spi Package Appendix B Changes from EJB 1.1 Local Clients Message-Driven Beans Home Interface Business Methods Container-Managed Persistence Changes EJB Query Language Security Changes Component Interoperability Copyright Copyright â 2002 by Queđ Corporation All rights reserved No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from the publisher No patent liability is assumed with respect to the use of the information contained herein Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions Nor is any liability assumed for damages resulting from the use of the information contained herein Library of Congress Catalog Card Number: 2001087886 Printed in the United States of America First Printing: September 2001 04 03 02 01 4 3 2 1 Trademarks All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized Que Corporation cannot attest to the accuracy of this information Use of a term in this book should not be regarded as affecting the validity of any trademark or service mark JavaBeans is a trademark of Sun Microsystems Warning and Disclaimer Every effort has been made to make this book as complete and as accurate as possible, but no warranty or fitness is implied The information provided is on an "as is" basis The authors and the publisher shall have neither liability nor responsibility to any person or entity with respect to any loss or damages arising from the information contained in this book or from the use of the CD or programs accompanying it Credits Associate Publisher Dean Miller Acquisitions Editor Todd Green Development Editor Sean Dixon Managing Editor Thomas F Hayes Project Editor Tonya Simpson Copy Editor Sossity Smith Indexer Bill Myers Exceptions Although exception handling is covered in detail in Chapter 13, basic exception information is provided here for your convenience Figure A.2 shows you a UML diagram of the EJB exceptions and their relationships Figure A.2 A UML diagram representing the EJB exception hierarchy Note All the EJB exceptions declare a no-argument constructor and one that accepts a String message AccessLocalException An AccessLocalException is thrown to indicate that the caller does not have permission to call the method This exception is thrown to local clients CreateException The CreateException exception must be included in the throws clauses of all create methods defined in an enterprise bean's component interface DuplicateKeyException The DuplicateKeyException exception is thrown if an entity EJB object cannot be created because an object with the same key already exists EJBException The EJBException exception is thrown by an enterprise bean instance to its container to report that the invoked business method or callback method could not be completed because of an unexpected error Note EJBException also declares a constructor that takes an Exception as the single argument With this constructor, you can declare an EJBException that wraps another exception FinderException The FinderException exception must be included in the throws clause of every find method of an entity bean's home interface NoSuchEntityException The NoSuchEntityException exception is thrown by an enterprise bean instance to its container to report that the invoked business method or callback method could not be completed because the underlying entity was removed from the database This exception can be thrown by the bean class methods that implement the business methods defined in the bean's remote interface and also by the ejbLoad and ejbStore methods Note NoSuchEntityException also declares a constructor that takes an Exception as the single argument With this constructor, you can create a NoSuchEntityException that wraps another exception NoSuchObjectLocalException A NoSuchObjectLocalException is thrown if an attempt is made to invoke a method on a local object that no longer exists ObjectNotFoundException The ObjectNotFoundException exception is thrown by a single object finder method to indicate that the specified EJB object does not exist RemoveException The RemoveException exception is thrown at an attempt to remove an EJB object when the enterprise bean or the container does not allow the EJB object to be removed TransactionRequiredLocalException This exception indicates that a request carried a null transaction context, but the target object requires an activate transaction TransactionRolledbackLocalException This exception indicates that the transaction associated with processing of the request has been rolled back, or marked to roll back Thus, the requested operation either could not be performed or was not performed because further computation on behalf of the transaction is not necessary The javax.ejb.spi Package The javax.ejb.spi package is a brand new package added in the EJB 2.0 Specification For now, only a single interface is defined there In the future, however, it's likely that more interfaces and classes will be added to grant more flexibility to the EJB vendors HandleDelegate The EJB container provides the implementation for the HandleDelegate interface It is used by portable implementations of javax.ejb.Handle and javax.ejb.HomeHandle It is not used by EJB components or by client components The HandleDelegate interface provides methods to serialize and deserialize EJBObject and EJBHome references to streams The HandleDelegate object is obtained by JNDI lookup at the reserved name java:comp/ HandleDelegate Table A.11 lists the methods in the HandleDelegate interface Table A.11 The Methods of the HandleDelegate Interface Return Type Method Name EJBHome readEJBHome EJBObject readEJBObject void writeEJBHome void writeEJBObject readEJBHome Deserialize the EJBHome reference corresponding to a HomeHandle readEJBHome is called from the readObject method of portable HomeHandle implementation classes The ObjectInputStream object is the same object that was passed in to the HomeHandle class's readObject When readEJBHome is called, the input stream must point to the location in the stream at which the EJBHome reference can be read The container must ensure that the EJBHome reference is capable of performing invocations immediately after deserialization Here is the method signature: public EJBHome readEJBHome(ObjectInputStream str) throw ClassNotFoundException; readEJBObject Deserialize the EJBObject reference corresponding to a Handle The readEJBObject method is called from the readObject method of portable Handle implementation classes The ObjectInputStream object is the same object that was passed in to the Handle class's readObject When readEJBObject is called, the input stream must point to the location in the stream at which the EJBObject reference can be read The container must ensure that the EJBObject reference is capable of performing invocations immediately after deserialization Here is the method signature: public EJBObject readEJBObject(ObjectInputStream str) t ClassNotFoundException; writeEJBHome Serialize the EJBHome reference corresponding to a HomeHandle This method is called from the writeObject method of portable HomeHandle implementation classes The ObjectOutputStream object is the same object that was passed in to the Handle class's writeObject Here is the method signature: public void writeEJBHome(EJBHome home, ObjectOutputStre writeEJBObject Serialize the EJBObject reference corresponding to a Handle This method is called from the writeObject method of portable Handle implementation classes The ObjectOutputStream object is the same object that was passed in to the Handle class's writeObject Here is the method signature: public void writeEJBObject(EJBObject obj, ObjectOutputS Appendix B Changes from EJB 1.1 In this chapter Local Clients Message-Driven Beans Container-Managed Persistence Changes EJB Query Language Home Interface Business Methods Security Changes Component Interoperability Local Clients Prior to EJB 2.0, all clients of an enterprise bean were treated as remote clients Support for location independence required that pass-by-value semantics always be followed when a bean was accessed through its home and remote interfaces Even when a session bean made a call to an entity bean running in the same JVM, the call was subjected to much of the overhead associated with a remote call Any optimizations were vendorspecific, if they existed at all Because of this drawback, some of the EJB design patterns first adopted were those aimed at reducing the number of calls made to entity beans EJB 2.0 introduces support for local clients in addition to remote clients If a session or entity bean supports other enterprise beans deployed in the same container as clients, those beans can be treated as local clients Instead of the remote and home interfaces used by remote clients, local clients interact with a bean through its local and local home interfaces Calls made through these interfaces use pass-by-reference semantics and avoid the overhead of remote calls Local clients give up location independence and they're tightly coupled to the beans they access, but the performance advantages make up for these limitations whenever remote access by a client isn't needed As part of this change to the specification, a new term was introduced to identify the interfaces exposed to clients of an enterprise bean The local and remote interfaces are collectively known as the component interface For more information on local clients, see "Local Versus Remote EJB Clients," p 45 Message-Driven Beans EJB 2.0 defines a third type of enterprise bean—the messagedriven bean—for the purpose of integrating the EJB architecture with the Java Message Service (JMS) This bean type differs most from entity and session beans in that it doesn't serve a synchronous client Instead, a client makes use of the business logic defined by a message-driven bean by sending a message to a JMS destination (queue or topic) with which the bean has been associated When the destination receives a message that satisfies the selector criteria (if any) established for the bean, the container invokes the bean's onMessage method, which allows the bean to function as a JMS message consumer You're responsible for implementing the onMessage method so that it performs any business logic you want to execute based on a received message Unlike session and entity beans, you don't declare home and component interfaces to expose the functionality of a message-driven bean All work is done through onMessage and the client has no direct access to the bean A future goal (beyond EJB 2.0) is to support messaging APIs other than JMS For more information on the new message-driven bean, see "What are Message-Driven Beans?" p 316 Container-Managed Persistence Changes EJB 1.1 required container providers to support containermanaged persistence (CMP), but it didn't require support for relationships between entities and it left the implementation details for finder methods totally up to the vendors This led to limited capabilities in some cases and nonportable solutions in others Some of the significant changes brought by EJB 2.0 relate to how CMP must be supported The first difference you'll notice with EJB 2.0 is in how you code your entity bean classes The bean classes you write for a CMP entity bean are now abstract classes that define their fields through a series of abstract get and set method declarations Instead of including any actual field declarations, your classes identify their fields using only their method declarations These declarations form part of what is known as the abstract persistence schema for a CMP bean It's the responsibility of the container to generate a concrete bean class based on the fields declared by your get and set methods and the relationships you define in the deployment descriptor As part of this, the container now manages relationships between entity beans as a standard capability For more information on EJB 2.0 CMP, see Chapter 7, "Container-Managed Persistence," p 185 EJB Query Language The biggest problem with finder methods in EJB 1.1 was that every vendor offered a unique way to implement them The standard support for relationships between entity beans in EJB 2.0 CMP and the introduction of the abstract persistence schema have made it possible to change this Finder methods are now defined using a syntax derived from SQL92 known as the EJB Query Language (EJB QL) It's up to the container to translate this standard format into the target language of the underlying data store If you're comfortable with SQL, writing queries for finder methods will come naturally to you More importantly, your finder method queries will be supported by any EJB 2.0 container In addition, support has been added for select methods, which allow entities that use CMP to execute internal queries using EJB QL These methods can make use of container-managed relationships to retrieve related entities or specific data values associated with them For more information on EJB QL, see "What Is the EJB Query Language?" p 218 Home Interface Business Methods EJB 1.1 allowed the declaration of create and finder methods for an entity bean in its home interface and business methods in its remote interface Because static methods aren't allowed in an EJB, this meant that a business method could only be executed by invoking it on a particular entity object through its remote interface Business methods related to a bean class but independent of a particular entity instance were best implemented as session bean methods that acted on the entities involved EJB 2.0 allows you to implement business methods within a bean class that are independent of a particular entity object This still doesn't involve declaring static methods in the component interface though Instead, you declare these methods, known as home methods, in the home interface The container executes a home method by selecting an available instance of the entity class from the pool and invoking the method on that instance The method isn't allowed to reference the attributes of the instance used to invoke it In fact, the container never activates the instance, so it's never associated with a particular entity object A home method is allowed to locate instances of its associated entity class using finder method calls It can then access those entities using the methods exposed by the component interface just like any other client would These methods are useful if you want to perform an operation on all instances of an entity or some particular subset of them Instead of declaring a session bean method to do this, you can now keep that logic within the bean class when it's appropriate For more information on exposing business methods through the home interface, see "Declaring the Home Interface," p 130 Security Changes Access to an enterprise bean's methods can be restricted declaratively by assigning method permissions in the deployment descriptor These permissions define the security roles that may call particular methods It's also possible to enforce restrictions or modify a bean's behavior programmatically using calls to the isCallerInRole method of the EJBContext These EJB security characteristics are unchanged from EJB 1.1 What's different is the control you have over the security identity associated with a call Prior to EJB 2.0, the principal associated with a caller was always the security identity checked to determine if a particular EJB method could be called EJB 2.0 also allows you to include an entry in the deployment descriptor that specifies a security principal to be associated with all calls made by an enterprise bean in place of the caller's security identity A client still must have any permissions required to call a particular beans method However, you can specify a principal that applies to any calls to other methods (or beans) that a particular bean makes This option gives the application assembler greater control of how method restrictions on an enterprise bean affect the behavior of an application For more information on specifying a security principal, see "Using Security with Enterprise JavaBeans and J2EE," p 399 Component Interoperability When you develop a distributed application, you want the components to be as independent as possible A component developer shouldn't be concerned about the location of other components or any details of their implementation EJB 2.0 helps achieve this goal by specifying an interoperability protocol based on CORBA/IIOP The goal is to allow session and entity beans developed and deployed into one vendor's EJB container to be accessible to other applications running in a different vendor's J2EE application server These applications can include other EJBs, servlets, JSPs, and standalone applications By basing the interoperability requirement on CORBA, access from CORBA clients written in languages such as C++ or COBOL is also simplified You can now develop your EJBs without concern for the vendor of the server used to deploy them and be guaranteed interoperability with applications running inside other compliant servers For more information on the new interoperability requirements, see Chapter 20, "Distribution and EJB Interoperability," p 511 ... contained herein Library of Congress Catalog Card Number: 20 0 108 7886 Printed in the United States of America First Printing: September 20 01 04 03 02 01 4 3 2 1 Trademarks All terms mentioned in this book that are known to be... Table of Contents • Examples Special Edition Using Enterprise JavaBeans 2. 0 By Chuck Cavaness, Brian Keeton Publisher : Que Pub Date : September 19, 20 01 ISBN : 0- 7897 -25 67-3 Pages : 648 Slots : 1 Copyright... 20 1 West 103 rd Street Indianapolis, IN 4 629 0 USA Introduction In this chapter This Book Is for You How This Book Is Organized Conventions Used in This Book Welcome to Enterprise JavaBeans 2. 0! As part of the Java 2

Ngày đăng: 25/03/2019, 17:11

Mục lục

  • Special Edition Using Enterprise JavaBeans 2.0

  • Table of Contents

  • Copyright

  • About the Authors

  • Acknowledgments

  • Tell Us What You Think!

  • Introduction

    • This Book Is for You

    • How This Book Is Organized

    • Conventions Used in This Book

    • Part I: Developing Enterprise JavaBeans

      • Chapter 1. Introduction to Enterprise Applications

        • The Enterprise JavaBeans Architecture

        • Component-Based Distributed Computing

        • N-Tiered Architectures

        • Why Use EJB?

        • Chapter 2. Setting the Stage–An Example Auction Site

          • The Auction Example

          • Overview of an English Auction

          • Choosing the Use Cases to Implement

          • Defining the Object Model

          • What's Next?

          • Chapter 3. EJB Concepts

            • Grasping the Concepts Early

            • What Is an Enterprise Bean?

Tài liệu cùng người dùng

Tài liệu liên quan