Upload
vankhanh
View
236
Download
3
Embed Size (px)
Citation preview
Gregory NybergRobert Patrick
Paul BauerschmidtJeffrey McDaniel
Raja Mukherjee
Mastering BEAWebLogic Server
Best Practices for Building andDeploying J2EE Applications
00 28128x FM.qxd 7/29/03 8:45 AM Page i
00 28128x FM.qxd 7/29/03 8:45 AM Page iv
Gregory NybergRobert Patrick
Paul BauerschmidtJeffrey McDaniel
Raja Mukherjee
Mastering BEAWebLogic Server
Best Practices for Building andDeploying J2EE Applications
00 28128x FM.qxd 7/29/03 8:45 AM Page i
C1.jpg
Vice President and Executive Publisher: Bob IpsenPublisher: Joe WikertExecutive Editor: Robert M. ElliottDevelopmental Editor: Brian McDonald and Emilie HermanEditorial Manager: Kathryn A. MalmSenior Production Editor: Angela SmithText Design & Composition: Wiley Composition Services
This book is printed on acid-free paper.
Copyright 2003 by Gregory Nyberg, Robert Patrick, Paul Bauerschmidt, Jeffrey McDaniel,and Raja Mukherjee. All rights reserved.
Published by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
No part of this publication may be reproduced, stored in a retrieval system, or transmittedin any form or by any means, electronic, mechanical, photocopying, recording, scanning, orotherwise, except as permitted under Section 107 or 108 of the 1976 United States CopyrightAct, without either the prior written permission of the Publisher, or authorization throughpayment of the appropriate per-copy fee to the Copyright Clearance Center, Inc., 222 Rose-wood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8700. Requests to the Pub-lisher for permission should be addressed to the Legal Department, Wiley Publishing, Inc.,10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4447, E-mail:[email protected].
Limit of Liability/Disclaimer of Warranty: While the publisher and author have used theirbest efforts in preparing this book, they make no representations or warranties with respectto the accuracy or completeness of the contents of this book and specifically disclaim anyimplied warranties of merchantability or fitness for a particular purpose. No warranty maybe created or extended by sales representatives or written sales materials. The advice andstrategies contained herein may not be suitable for your situation. You should consult witha professional where appropriate. Neither the publisher nor author shall be liable for anyloss of profit or any other commercial damages, including but not limited to special, inci-dental, consequential, or other damages.
For general information on our other products and services please contact our CustomerCare Department within the United States at (800) 762-2974, outside the United States at(317) 572-3993 or fax (317) 572-4002.
Trademarks: Wiley, the Wiley Publishing logo and related trade dress are trademarks orregistered trademarks of Wiley Publishing, Inc., in the United States and other countries,and may not be used without written permission. BEA WebLogic Server is a trademark ofBEA Systems, Inc. J2EE is a trademark of Sun Microsystems, Inc. All other trademarks arethe property of their respective owners. Wiley Publishing, Inc., is not associated with anyproduct or vendor mentioned in this book.
Wiley also publishes its books in a variety of electronic formats. Some content that appearsin print may not be available in electronic books.
Library of Congress Cataloging-in-Publication Data:
ISBN: 0-471-28128-X
Printed in the United States of America
10 9 8 7 6 5 4 3 2 1
00 28128x FM.qxd 7/29/03 8:45 AM Page ii
To Meredith
For her patience, love, and support.
Greg
To Li Yan
For her love, encouragement, and patience over the past five years.
Robert
00 28128x FM.qxd 7/29/03 8:45 AM Page iii
00 28128x FM.qxd 7/29/03 8:45 AM Page iv
Acknowledgments xvii
Introduction xix
About the Authors xxiii
Chapter 1 Building Web Applications in WebLogic 1Java Servlets and JSP Key Concepts 1
Characteristics of Servlets 2Servlets Use the Request/Response Model 2Servlets Are Pure Java Classes 2Servlets Have a Life Cycle 3Servlets Allow Multiple Parallel Requests 3Servlets May Access Request Data 5Servlets Use Session Tracking 6
Characteristics of JavaServer Pages 8JSP Is a Scripting Technology 8JSP Pages Are Converted to Servlets 8Many Tags and Scripting Elements Are Available 9All Servlet Capabilities Are Available 10JSP Response Is Buffered 12JSP Pages Have Unique Capabilities 13
Web Application Best Practices 14Ensure Proper Error Handling 14Use jsp:useBean to Reduce Scriptlet Code 17Use Custom Tags for Selected Behaviors 20
Custom Tag Key Concepts 21Custom Tag Use Is EasyDevelopment Is Complex 21
Cache Page Output to Improve Performance 23
Contents
v
00 28128x FM.qxd 7/29/03 8:45 AM Page v
Use Servlet Filtering for Common Behaviors 28Building a Simple SnoopFilter Filter Class 29Registering SnoopFilter in the web.xml Descriptor File 29Response Caching Using the CacheFilter 30
Using Custom JSP Base Classes 32Using Run-Time Expressions in JSP Directives 32Creating Excel Files Using Servlets and JSP Pages 33Viewing Generated Servlet Code 36Programmatic Authentication in Web Applications 37
Chapter Review 38
Chapter 2 Choosing a Web Application Architecture 39Architecture Key Concepts 39
J2EE Application Tiers 40Model-View-Controller Architecture 40Common J2EE Design Patterns 41
Presentation-Tier Architecture Selection 42Presentation-Tier Requirements 42
Display Requirements 43Form/Update Requirements 46Navigation Requirements 51Building a Presentation-Tier Architecture 54
Other Architecture Considerations 55Candidate Presentation-Tier Architectures 56
JSP-Centric Architecture 56Servlet-Centric Architecture 58Additional Frameworks 60
Chapter Review 61
Chapter 3 Designing an Example J2EE Application 63Application Requirements 63Business Domain Models 64Presentation Requirements 65Web Application Architecture 68Presentation Approach 68
Self-Assembly 70Master Page Assembly 73Master Page Assembly with Page Display Servlet 75
Business-Tier Interfaces 78Displaying Bean Attributes on JSP Pages 79Relationships in Presentation Components 84Action Classes Load Beans and Forms for Display 85Action Classes Perform Bean Updates 87
Chapter Review 88
Chapter 4 Building an Example Web Application 89Overview of Application Components 90Constructing the Application Skeleton 91
vi Contents
00 28128x FM.qxd 7/29/03 8:45 AM Page vi
Constructing the User Site Components 92Reservation Information Components 92Core Reservation Process Components 97
Defining Navigation Paths 97Property Search/Selection Pages 99Date Selection and Availability Display Pages 107Reservation Creation Process 118
Targeted Offers Components 121Construction of Administration Site Components 125
Authorization/Authentication Components 126Property Maintenance Components 131
Property Main Form 132Rate Maintenance Pages 138Availability Maintenance Pages 140
Chapter Review 146
Chapter 5 Packaging and Deploying WebLogic Web Applications 147Packaging Web Applications 148
Web Application Directory Structure 148Internal Components 150
Web Application Descriptor Files 151Standard web.xml Descriptor File 151Locating EJB Components 156weblogic.xml Descriptor File 157
Precompiling JSP Components 160Creating an Exploded Web Application 164Creating a Web Application Archive File 166
Deploying Web Applications 167Automatic Deployment 167WebLogic Deployer Utility and Ant Task 170WebLogic Console Deployment 173Creating Required Users and Group for BigRez.com 174
Chapter Review 176
Chapter 6 Building Enterprise JavaBeans in WebLogic Server 177EJB Technology Overview 177EJB Component Types 178
Stateless Session Beans 178Stateful Session Beans 180Entity Beans 181
Bean-Managed Persistence 185Container-Managed Persistence 187Finder Methods 194Select Methods 196Home Methods 198
Message-Driven Beans 199WebLogic Server EJB Container 200
EJB Container Basics 200EJB Life Cycle in WebLogic Server 201
Contents vii
00 28128x FM.qxd 7/29/03 8:45 AM Page vii
General WebLogic Server EJB Features 203EJB Deployment/Redeployment 203Dynamic EJB Compiling 203Referencing Other EJB Components 204
Referencing EJB Components in the Same Application 204Referencing EJB External Components 206Calling Components by Reference 206
Session Bean Features 207Stateful Session EJB Cache Management 207In-Memory Replication for Stateful Session EJBs 209
Entity Bean Features 211Concurrency Strategies 211
Exclusive Concurrency Strategy 213Database Concurrency Strategy 215Read-Only Concurrency Strategy 218Optimistic Concurrency Strategy 219
Caching Strategies 223Caching between Transactions 223Read-Only Multicast Invalidation 225Read-Mostly Pattern 226Combined Caching Support 230
Tuning Strategies 232Loading Beans During Finder Operations 232Updating Database after Each Method Invocation 236Including Updates in Finder Queries 236Batching Database Operations 238Controlling Timing of Database Inserts 239Controlling Lazy Retrieval Using Field Groups 240Relationship Caching with CMP Entity Beans 244
Additional CMP Features 246Automatic Primary Key Generation 247EJB CMP Multiple-Table Mapping Support 249WebLogic EJB-QL Enhancements 250Dynamic Query Support 252Cascade Delete Support 253Checking for Bean Existence 255
Message-Driven Bean Features 256Chapter Review 257
Chapter 7 Building an Example EJB Application 259Business-Tier Requirements 260
Business Logic Requirements 260Process Encapsulation 260Validation 261
Object-Relational Mapping Requirements 261Mapping Simple Classes 262Mapping Simple Aggregation 262
viii Contents
00 28128x FM.qxd 7/29/03 8:45 AM Page viii
Projection Objects and Queries 263Mapping Associations/Relationships 264Mapping Inheritance 265
Data Access Requirements 267Other Business-Tier Requirements 268Review of Business-Tier Requirements 268
Business-Tier Architecture Options 269Stateless Services with JDBC 270Stateless Services with Entity Bean Persistence 272Stateless Services, Entity Beans, and Direct Interaction 277Chosen Architecture for bigrez.com 280
EJB Construction Options 281Manual Construction and Configuration 282Using WebLogic Builder Utility 283Using the WebLogic EJBGen Utility 284
Class-Level EJBGen Tags 284Method-Level EJBGen Tags 286Invoking the EJBGen Utility 287
Construction of Business-Tier Components 288Construction of Entity Beans 288Construction of Session Beans 293Construction of Message-Driven Beans 294
Chapter Review 295
Chapter 8 Packaging and Deploying WebLogic EJB Applications 297Creating an EJB Archive File 298
Creating EJB Source Code and Descriptor Files 300Compiling EJB Components 302Executing the EJB Compiler 302
Invoking the EJB Compiler Directly 303Using the WebLogic Application Compiler 303Using the ejbjar Optional Ant Task 305
Packaging Enterprise Applications 306Enterprise Application Directory Structure 309Enterprise Application Descriptor Files 312
Standard application.xml Descriptor File 312weblogic-application.xml Descriptor File 314
Packaging Utility Archives in Enterprise Applications 316Creating an Exploded Enterprise Application 318Creating an Enterprise Application Archive File 320
Deploying EJB Applications 322Creating Required Services 322Automatic Deployment 324WebLogic Console Deployment 325
Chapter Review 327
Contents ix
00 28128x FM.qxd 7/29/03 8:45 AM Page ix
Chapter 9 Using WebLogic JMS 329JMS Key Concepts 330
Understanding the Messaging Models 330Reviewing the JMS 1.0.2b API 331
Connection Factories 331Connections 331Destinations 332Sessions 332Message Producers and Consumers 333Messages 336
Upcoming JMS 1.1 Changes 336The WebLogic JMS Provider 338
Understanding WebLogic JMS Servers 338Clustering WebLogic JMS 338
Location Transparency 339Connection Routing, Load Balancing, and Failover 339Distributed Destinations 340Migrating JMS Servers 346
Configuring WebLogic JMS 347Connection Factories 347Templates 349Destination Keys 349Time-to-Deliver Extension 350Persistent Stores 351Delivery Overrides, Destination Quotas, and Flow Control 355
WebLogic JMS Application Design 361Choosing a Destination Type 362Locating Destinations 362Choosing the Appropriate Message Type 363Compressing Large Messages 365Selecting a Message Acknowledgment Strategy 365Designing Message Selectors 367Choosing a Message Expiration Strategy 369
Active Expiration 370Expiration Policies 371
Handling Poison Messages 372Redelivery Delay 373Redelivery Limit and Error Destination 374Ordered Redelivery 375
Handling Message Ordering Issues 376Using Transactions 376Using Multicast Sessions 378Handling Request/Reply Style Message Exchange 379
WebLogic JMS Programming 382Using WebLogic JMS with Servlets and EJBs 382Consuming Asynchronous Messages on the Server 384
Message-Driven Beans 385Application Design Considerations 388
x Contents
00 28128x FM.qxd 7/29/03 8:45 AM Page x
External JMS Providers 389Understanding the Messaging Bridge 390Using Message-Driven Beans 390Mapping External JMS Objects to WebLogic JNDI 391Choosing an Integration Strategy 391
Chapter Review 393
Chapter 10 Using WebLogic Security 395WebLogic Security Overview 396WebLogic Security Framework 398
Embedded LDAP Server 398Default Security Providers 400
Authentication 402Identity Assertion 404Authorization and Adjudication 406Auditing 407Role Mapping and Credential Mapping 407Managing Security Provider Configuration 407
Using Compatibility Mode with the RealmAdapter 410Using External Security Stores 410
Managing External LDAP Authentication 410Setting Up UNIX-Based Authentication 413Setting Up Windows Domain Authentication 414Custom Authentication Providers 415
Setting Up SSL/TLS 416Reviewing SSL Technology 416Obtaining X.509 Certificates 418Configuring One-Way SSL 420Configuring Two-Way SSL 421Debugging SSL Problems 425
Writing Security-Aware Java Clients 425Writing Java Clients That Use JAAS 426Writing Java Clients That Use SSL 429
RMI Clients 429Programmatic HTTP Clients 431Web Services Clients 433Application Authentication 433Hostname Verification 433Trust Managers 434
Managing Application Security 434Setting Up J2EE Application Security 434
Securing Web Applications 435Managing EJB Security 440Securing J2EE CA Resource Adapters 442Securing Enterprise Applications 442
Contents xi
00 28128x FM.qxd 7/29/03 8:45 AM Page xi
Setting Up WebLogic Application Security 443Managing Users and Groups 443Working with Roles and Policies 444
Booting WebLogic Server 448Chapter Review 449
Chapter 11 Administering and Deploying Applications in WebLogic Server 451WebLogic Architecture Key Concepts 451
Domain Architecture 452WebLogic Server Architecture 453
Execute Queues and Execute Threads 454Socket Muxer 458Listen Ports and Listen Threads 460Application Container 460
WebLogic Server Clustering Architecture 461Clustering for Web Applications 462Clustering for RMI-Based Applications 466
Admin Server 469Node Manager 471
WebLogic Administration Key Concepts 472Server States 472Server Self-Health Monitoring 474Network Channels 474
Configuring a WebLogic Server Domain 475Setting Up a New Domain 478Configuring Servers 483Configuring the Cluster 486Configuring Network Channels 487Setting Up the Node Manager 489Operating System Configuration 493Java Virtual Machine Configuration 494Web Server Plug-in Configuration 494Configuring Applications for WebLogic Server 499
Configuring Database Resources 500Configuring JMS Resources 508Configuring JavaMail Sessions 516Configuring Execute Queues and Threads 516Deploying Applications 516
Monitoring WebLogic Server Applications 519Using the Command-Line Administration Tool 519Monitoring with the WebLogic Console 521Programmatic Monitoring with JMX 525Monitoring via SNMP 529
xii Contents
00 28128x FM.qxd 7/29/03 8:45 AM Page xii
Managing WebLogic Server Applications 533Troubleshooting Application Issues 534Versioning Applications 536Managing Failure Conditions 536
Database Failures 537Migrating the JTA Service 538Migrating JMS Servers 538Migrating the Admin Server 539
Chapter Review 540
Chapter 12 Optimizing WebLogic Server Performance 541Overview of System Performance 542
Reviewing the Core Principles 542Tuning a WebLogic Server-Based Application 544
Operating System Tuning 544Network Tuning 550Java Virtual Machine Tuning 551Application Server Tuning 562
Performance Best Practices 567Designing for Performance 567
Session Faade 568Value Object 568Command Pattern 569
Understanding Web Container Best Practices 569Session Management 569JavaServer Pages 571Servlets 572
Understanding EJB Container Best Practices 572JNDI Lookup Strategies 573Optimizing Entity Beans 574Using Appropriate Isolation Levels 576Taking Advantage of Pass-by-Reference 577
Applying Database Access Best Practices 578Basic Database Principles 579Retrieve Columns Explicitly 579Cache Prepared Statements 580Transaction Model 580Commitment Control Level 581Batch Updates 582Connection Pools 582Release JDBC Resources 583
Troubleshooting Performance Problems 584Preparing for Troubleshooting 584Bottleneck Identification and Correction 584Problem Resolution 587Common Application Server Performance Problems 588
Contents xiii
00 28128x FM.qxd 7/29/03 8:45 AM Page xiii
Mastering BEA WebLogic Server: Best Practices for Building and Deploying J2EE ApplicationsContentsAcknowledgmentsIntroductionOrganization of the BookWho Should Read This BookTools You Will NeedOnline Resources
About the AuthorsChapter 1: Building Web Applications in WebLogicJava Servlets and JSP Key ConceptsWeb Application Best PracticesChapter Review
Chapter 2: Choosing a Web Application ArchitectureArchitecture Key ConceptsPresentation-Tier Architecture SelectionCandidate Presentation-Tier ArchitecturesChapter Review
Chapter 3: Designing an Example J2EE ApplicationApplication RequirementsBusiness Domain ModelsPresentation RequirementsWeb Application ArchitecturePresentation ApproachBusiness-Tier InterfacesChapter Review
Chapter 4: Building an Example Web ApplicationOverview of Application ComponentsConstructing the Application SkeletonConstructing the User Site ComponentsConstruction of Administration Site ComponentsChapter Review
Chapter 5: Packaging and Deploying WebLogic Web ApplicationsPackaging Web ApplicationsDeploying Web ApplicationsChapter Review
Chapter 6: Building Enterprise JavaBeans in WebLogic ServerEJB Technology OverviewEJB Component TypesWebLogic Server EJB ContainerGeneral WebLogic Server EJB FeaturesSession Bean FeaturesEntity Bean FeaturesMessage-Driven Bean FeaturesChapter Review
Chapter 7: Building an Example EJB ApplicationBusiness-Tier RequirementsBusiness-Tier Architecture OptionsEJB Construction OptionsConstruction of Business-Tier ComponentsChapter Review
Chapter 8: Packaging and Deploying WebLogic EJB ApplicationsCreating an EJB Archive FilePackaging Enterprise ApplicationsDeploying EJB ApplicationsChapter Review
Chapter 9: Using WebLogic JMSJMS Key ConceptsThe WebLogic JMS ProviderWebLogic JMS Application DesignWebLogic JMS ProgrammingExternal JMS ProvidersChapter Review
Chapter 10: Using WebLogic SecurityWebLogic Security OverviewWebLogic Security FrameworkUsing External Security StoresSetting Up SSL/TLSWriting Security-Aware Java ClientsManaging Application SecurityChapter Review
Chapter 11: Administering and Deploying Applications in WebLogic ServerWebLogic Architecture Key ConceptsWebLogic Administration Key ConceptsConfiguring a WebLogic Server DomainMonitoring WebLogic Server ApplicationsManaging WebLogic Server ApplicationsChapter Review
Chapter 12: Optimizing WebLogic Server PerformanceOverview of System PerformancePerformance Best PracticesTroubleshooting Performance ProblemsChapter Review
Chapter 13: Development Environment Best PracticesDefining Required Hardware and SoftwareInstalling WebLogic Server SoftwareConfiguring the Working DirectoryManaging Configuration InformationEstablishing a Build ProcessChoosing Development ToolsCreating a Unit-Testing InfrastructureChapter Review
Chapter 14: Production Environment Best PracticesDeployment StrategiesGlobal and Local Traffic ManagementProduction Security StrategiesChapter Review
Chapter 15: Developing and Deploying Web ServicesReviewing the Underlying TechnologyCreating Web Services with WebLogic ServerMoving Past the BasicsAdding Web Services toChapter Review
Index