performance and scalability goals Item 9: Restrict EJB to transactional processing Item 10: Never optimize without profiling first Item 11: Recognize the cost of vendor neutrality Ite
Trang 2Effective Enterprise Java Table of Contents
Copyright
Effective Software
Development Series Scott Meyers, Consulting Editor Titles in the Series
Foreword
Preface
About the items
Acknowledgments
Reporting bugs, making
suggestions, and getting book
Trang 3updates
List of Abbreviations
Chapter 1 Introduction
The goals of J2EE
Middleware and J2EE
Trang 4across component boundaries Item 3: Differentiate layers from tiers
Item 4: Keep data and
processors close together
Item 5: Remember that identity breeds contention
Item 6: Use hook points to
Trang 5performance and scalability goals
Item 9: Restrict EJB to
transactional processing
Item 10: Never optimize
without profiling first
Item 11: Recognize the cost of vendor neutrality
Item 12: Build in monitoring support
Item 13: Build in administration support
Item 14: Make deployment as simple as possible
Trang 6Chapter 3 Communication Item 15: Understand all your communications options
Item 16: Consider your lookup carefully
Item 17: Recognize the cost of network access
Item 18: Prefer
context-complete communication styles Item 19: Prefer data-driven
communication over driven communication
behavior-Item 20: Avoid waiting for
remote service requests to
Trang 7respond
Item 21: Consider partitioning components to avoid excessive load on any one machine
Item 22: Consider using Web Services for open integration Item 23: Pass data in bulk Item 24: Consider rolling your own communication proxies Chapter 4 Processing
Item 25: Keep it simple
Item 26: Prefer rules engines for complex state evaluation and execution
Trang 8Item 27: Prefer transactional processing for implicitly
nonatomic failure scenarios Item 28: Differentiate user transactions from system
Item 31: Understand EJB
transactional affinity
Item 32: Prefer local
Trang 9transactions to distributed ones Item 33: Consider using
optimistic concurrency for
better scalability
Item 34: Consider using
pessimistic concurrency for explicit concurrency control Item 35: Consider lower
isolation levels for better
Trang 10when possible to avoid lock
Item 40: Use objects-first
persistence to preserve your domain model
Item 41: Use relational-first
persistence to expose the power
of the relational model
Item 42: Use procedural-first
Trang 11Item 47: Eager-load frequently used data
Item 48: Batch SQL work to
Trang 12Item 54: Keep style separate from content
Item 55: Pregenerate content to minimize processing
Trang 13Item 56: Validate early, validate everything
Chapter 7 Security
Item 57: Security is a process, not a product
Item 58: Remember that
security is not just prevention Item 59: Establish a threat
model
Item 60: Assume insecurity Item 61: Always validate user input
Item 62: Turn on platform
security
Trang 14Item 63: Use role-based
authorization
Item 64: Use SignedObject to provide integrity of Serialized objects
Item 65: Use SealedObject to provide confidentiality of
Trang 15Item 68: Tune the JVM
Item 69: Use independent JREs for side-by-side versioning
Item 73: Prefer
container-managed resource management Item 74: Use reference objects
to augment garbage collection behavior
Trang 16Item 75: Don't be afraid of JNI code on the server
Trang 17index_H index_I index_J index_K index_L index_M index_N index_O index_P index_Q index_R index_S index_T index_U
Trang 18index_V index_W index_X index_Y index_Z
Trang 20"With this book, Ted Neward helps you make the leap from being a good Java enterprise developer to a great
developer!"
John Crupi, Sun Distinguished Engineer coauthor, Core J2EE Patterns
If you want to build better Java enterprise applications and work more efficiently, look no
Trang 21further Inside, you will find an accessible guide to the nuances
of Java 2 Platform, Enterprise Edition (J2EE) development Learn how to:
Use in-process or local
storage to avoid the network
Set lower isolation levels for better transactional
throughput
Use Web services for open integration
Trang 22Consider your lookup
carefully
Pre-generate content to minimize processing
Trang 23Ted Neward provides you with
75 easily digestible tips that will help you master J2EE
development on a systemic and architectural level His
panoramic look at the good, the bad, and the ugly aspects of J2EE development will address your most pressing concerns Learn how to design your
enterprise systems so they
adapt to future demands.
Improve the efficiency of your code without compromising its
Trang 24correctness Discover how to implement sophisticated
functionality that is not directly supported by the language or platform After reading
Effective Enterprise Java , you will know how to design and implement better, more scalable enterprise-scope Java software systems.
Trang 26Copyright
Effective Software Development Series Scott
Meyers, Consulting Editor
Titles in the Series
Foreword
Preface
About the items
Acknowledgments
Reporting bugs, making suggestions, and
getting book updates
List of Abbreviations
Chapter 1 Introduction
The goals of J2EE
Middleware and J2EE
Trang 27Item 2: Prefer loose coupling across
component boundaries
Item 3: Differentiate layers from tiers
Item 4: Keep data and processors close
together
Item 5: Remember that identity breeds
contention
Item 6: Use hook points to inject
optimizations, customizations, or new
functionality
Item 7: Be robust in the face of failure
Item 8: Define your performance and
scalability goals
Item 9: Restrict EJB to transactional
processing
Item 10: Never optimize without profiling first
Item 11: Recognize the cost of vendor
neutrality
Item 12: Build in monitoring support
Item 13: Build in administration support
Trang 28Item 14: Make deployment as simple as
possible
Chapter 3 Communication
Item 15: Understand all your communications
options
Item 16: Consider your lookup carefully
Item 17: Recognize the cost of network
access
Item 18: Prefer context-complete
communication styles
Item 19: Prefer data-driven communication
over behavior-driven communication
Item 20: Avoid waiting for remote service
requests to respond
Item 21: Consider partitioning components to
avoid excessive load on any one machine
Item 22: Consider using Web Services for
open integration
Item 23: Pass data in bulk
Item 24: Consider rolling your own
communication proxies
Chapter 4 Processing
Trang 29Item 25: Keep it simple
Item 26: Prefer rules engines for complex
state evaluation and execution
Item 27: Prefer transactional processing for
implicitly nonatomic failure scenarios
Item 28: Differentiate user transactions from
system transactions
Item 29: Minimize lock windows
Item 30: Never cede control outside your
component while holding locks
Item 31: Understand EJB transactional
affinity
Item 32: Prefer local transactions to
distributed ones
Item 33: Consider using optimistic
concurrency for better scalability
Item 34: Consider using pessimistic
concurrency for explicit concurrency control
Item 35: Consider lower isolation levels for
better transactional throughput
Item 36: Use savepoints to keep partial workin the face of rollback
Trang 30Item 37: Replicate resources when possible to
avoid lock regions
Item 38: Favor the immutable, for it needs no
locks
Chapter 5 State Management
Item 39: Use HttpSession sparingly
Item 40: Use objects-first persistence to
preserve your domain model
Item 41: Use relational-first persistence to
expose the power of the relational model
Item 42: Use procedural-first persistence to
create an encapsulation layer
Item 43: Recognize the object-hierarchical
impedance mismatch
Item 44: Use in-process or local storage to
avoid the network
Item 45: Never assume you own the data or
the database
Item 46: Lazy-load infrequently used data
Item 47: Eager-load frequently used data
Item 48: Batch SQL work to avoid
Trang 31Item 49: Know your JDBC provider
Item 50: Tune your SQL
Chapter 6 Presentation
Item 51: Consider rich-client UI technologies
Item 52: Keep HTML minimal
Item 53: Separate presentation from
processing
Item 54: Keep style separate from content
Item 55: Pregenerate content to minimize
processing
Item 56: Validate early, validate everything
Chapter 7 Security
Item 57: Security is a process, not a product
Item 58: Remember that security is not just
prevention
Item 59: Establish a threat model
Item 60: Assume insecurity
Item 61: Always validate user input
Item 62: Turn on platform security
Item 63: Use role-based authorization
Trang 32Item 64: Use SignedObject to provide
integrity of Serialized objects
Item 65: Use SealedObject to provide
confidentiality of Serializable objects
Item 66: Use GuardedObject to provide
access control on objects
Chapter 8 System
Item 67: Aggressively release resources
Item 68: Tune the JVM
Item 69: Use independent JREs for
side-by-side versioning
Item 70: Recognize ClassLoader boundaries
Item 71: Understand Java Object Serialization
Item 72: Don't fight the garbage collector
Item 73: Prefer container-managed resource
management
Item 74: Use reference objects to augment
garbage collection behavior
Item 75: Don't be afraid of JNI code on the
server
Bibliography
Trang 33Books
Web Sites
Index
Trang 34Many of the designations used by
manufacturers and sellers to distinguishtheir products are claimed as trademarks.Where those designations appear in thisbook, and Addison-Wesley was aware of
a trademark claim, the designations havebeen printed with initial capital letters or
in all capitals
The author and publisher have taken care
in the preparation of this book, but make
no expressed or implied warranty of anykind and assume no responsibility forerrors or omissions No liability is
assumed for incidental or consequentialdamages in connection with or arising out
Trang 35of the use of the information or programscontained herein.
The publisher offers discounts on thisbook when ordered in quantity for bulkpurchases and special sales For moreinformation, please contact:
U.S Corporate and Government Sales(800) 382-3419
Trang 36ISBN 0-321-13000-6 (pbk : alk paper)
1 Java (Computer program language) I Title
Trang 37publication may be reproduced, stored in
a retrieval system, or transmitted, in anyform, or by any means, electronic,
mechanical, photocopying, recording, orotherwise, without the prior consent of thepublisher Printed in the United States ofAmerica Published simultaneously inCanada
For information on obtaining permissionfor use of material from this work, pleasesubmit a written request to:
Pearson Education, Inc
Rights and Contracts Department
75 Arlington Street, Suite 300
Boston, MA 02116
Fax: (617) 848-7047
Trang 38Text printed on recycled paper
1 2 3 4 5 6 7 8 9 10CRS0807060504
First printing, August 2004
Trang 39Effective Software
Development Series
Scott Meyers,
Consulting Editor
The Effective Software Development
Series provides expert advice on all
aspects of modern software development.Books in the series are well written,
technically sound, of lasting value, andtractable length Each describes the
critical things the experts almost alwaysdoor almost always avoid doingto
produce outstanding software
Scott Meyers (author of the Effective C++
Trang 40books and CD) conceived of the seriesand acts as its consulting editor Authors
in the series work with Meyers and withAddison-Wesley Professional's editorialstaff to create essential reading for
software developers of every stripe
Trang 41Titles in the Series
Elliotte Rusty Harold, Effective XML: 50
Specific Ways to Improve Your XML,
0321150406
Diomidis Spinellis, Code Reading: The
Open Source Perspective, 0201799405
For more information on books in thisseries please see
www.awprofessional.com/esds
Trang 42TheServerSide.com (Your EnterpriseJava Community) TheServerSide.com
really grew from the needs of developersfaced with the new world of J2EE It wasthe water cooler that allowed us to chatabout solutions that worked for us, and it
Trang 43saw the growth of enterprise Java
patterns
Developing for the enterprise is a verydifferent beast when compared to buildingsmaller, standalone applications We have
to consider issues that we can safelyignore in the other world As soon as we
have to share data among multiple users,
we start down the enterprise path Then
we start facing questions: What is the bestsolution for allowing concurrency to thisdata? How coherent and correct does ithave to be? How can we scale up from 2
to 50 to 1,000 clients? These are all
significant questions, and I don't feel thatthe average developer has enough help inanswering them Well, simply answering
Trang 44the questions may not be the correct focus.
We need to be taught about the variousissues involved and shown techniques thatcan help with the various problems WithTed Neward's book, we are now armedwith the knowledge that will allow us tocome up with the right balance in thesolution for each particular problem
No book has attacked these problems
quite like Effective Enterprise Java does.
The most important part of this book isthat it teaches you two things really well
You will understand the general issues
of enterprise computing.
These enterprise problems are far fromnew Ted has been around the block, and
Trang 45he understands the core issues at work Anon-Java developer would get a lot out ofthis book for this very reason What youlearn here will be with you for as long asyou develop enterprise solutions Thelanguage and APIs may change, but youwill understand the issues in building agood architecture, the options you have forcommunication, the choices for where tostore state, the various security concerns,and so much more.
You will be able to attack the problems
by using enterprise Java.
Although the book offers genuine insightinto the general enterprise problems, italso gives you tools to solve them with
Trang 46enterprise Java today You will
understand more about where the variousenterprise Java technologies fit together.When would you use Web Services? Whatcan messaging do for you? What is EJBgood for? This book provides answers tothese questions
It is great to have some answers to thesecommon questions The style of the book,
in which you are given a set of "effectiveitems," gets right to the point Get stuck in,and enjoy the ride!
Dion Almaer
Editor-in-Chief, TheServerSide.com
Trang 47Those who cannot remember the
past are doomed to repeat it.
George Santayana
These are heady days for Java
programmers Commercially available forless than a decade, Java has neverthelessemerged as the language of choice forenterprise systems on nearly all majorcomputing platforms Companies andindividuals with challenging programmingproblems increasingly embrace the
language and platform, and the questionfaced by those who do not use Java is
Trang 48often when they will start, not if The
breadth and scope of the specificationsand libraries defined by and under theJava 2 Enterprise Edition
Specificationwhich both dwarfs andsubsumes that of the Java language
itselfmakes it possible to write rich,complex systems without sacrificingperformance or implementing commonalgorithms or data structures from scratch.The Java language and virtual machinesgrow ever more powerful Tools andenvironments for Java development growever more abundant and robust
Commercial libraries all but obviate theneed to write code in many applicationareas