Building on the overwhelming success of his previous two editions, renowned author Ed Roman has returned-along with Enterprise JavaBeans (EJB) gurus Gerald Brose and Rima Patel Sriganesh-to offer you the inside scoop on the EJB 2.1 specification and related enhancements in J2EE 1.4. Continuing the tradition of providing you with an unparalleled EJB tutorial, this expert team of authors has revised more than 30 percent of the book and added new chapters that explore the dramatic changes in the way EJB is now used for building applications.
Serving as the ultimate resource that boasts the most up-to-date information on EJB, this edition begins with the fundamentals of building an EJB system and gradually moves on to a discussion of programming with EJB. You'll learn about advanced EJB concepts and best practices that you won't find anywhere else, such as transactions, persistence, clustering, integration, performance monitoring, security, and choosing an EJB server. Along the way, you'll get in-depth coverage of: EJB security mechanisms, Best practices for EJB application development, deployment, and testing, Tips for selecting appropriate Web frameworks and EJB containers, EJB integration with back-end enterprise information systems using J2EE Connector technology, Performance optimizations for various types of EJB
|Edition description:||Older Edition|
|Product dimensions:||7.54(w) x 9.20(h) x 1.56(d)|
About the Author
Scott W. Ambler is the author and editor of a series of successful books on Java and object technology. He is also a frequent speaker at major conferences.
Tyler Jewell is a software engineer at Talarian SmartSockets and is the primary author of all BEA WebLogic Server courses. He is also a speaker at a wide variety of conferences including Java Expo, Object Expo, Cysive, and JavaOne.
Read an Excerpt
Chapter 8: Introduction to Message-Driven BeansIn this chapter, we will learn about messaging, which is a lightweight vehicle for communications. Messaging is more appropriate than RMI-IIOP in numerous scenarios. We'll also learn about message-driven beans, special beans that can be accessed via messaging and a new addition to the EJB 2.0 specification.
Specifically, you'll learn about the following:
- An introduction to messaging, including an overview of asynchronous behavior and message-oriented middleware
- A brief tutorial of the Java Message Service (JMS), which message-driven beans depend on
- Features of message-driven beans
- How message-driven beans compare with entity and session beans
- How to develop message-driven beans
- Advanced message-driven bean topics, gotchas, and possible solutions
Motivation to Use Message-Driven BeansIn previous chapters, you learned how to code session and entity beans—distributed components that are accessed using RMI-IIOP. RMI-IIOP is a tradi-tional, heavyweight way to call components. While RMI-IIOP may be useful in many scenarios, several other areas are challenging for RMI-IIOP. Here are just three examples.
Performance. An RMI-IIOP client must wait (or block) while the server per-forms its processing. Only when the server completes its work does the client receive a return result, which allows it to continue processing.
Reliability. When an RMI-IIOP client calls the server, it has to be running. If the server crashes or the network crashes, the client cannot perform its intended operation.
Support for multiple senders and receivers. RMI-IIOP limits you to a single client talking to a single server at any given time. There is no built-in functionality for multiple clients to broadcast events to multiple servers.
Messaging is an alternative to remote method invocations and is shown in Figure 8.1. The idea behind messaging is that a middleman sits between the client and the server. (A layer of indirection solves every problem in computer science). This middleman receives messages from one or more message producers and broadcasts those messages to one or more message consumers. Because of this middleman, the producer can send a message and then continue processing. He can optionally be notified of the response later when the consumer finishes. This is called asynchronous programming.
Messaging addresses the three previous concerns with RMI-IIOP as follows.
Performance. A messaging client does not need to block when performing a request. As an example, when you purchase a book using Amazon.com's one-click order functionality, you can continue browsing the site without waiting to see if your credit card authorizes. Unless something goes wrong, Amazon.com sends you a confirmation email afterwards. This type of fire-and-forget system could easily be coded using messaging. When the user clicks to buy the book, a message is sent that results in credit card processing later. The user can continue to browse.
Reliability. If your message-oriented middleware supports guaranteed delivery, you can send a message and know for sure that it will reach its destination, even if the consumer is not available. You send the message to the MOM middleman, and that middleman routes the message to the consumer when he comes back alive again. With RMI-IIOP, this is not possible because there is no middleman: If the server is down, an exception is thrown.
Support for multiple senders and receivers. Most message-oriented middleware products can accept messages from many senders and broadcast them to many receivers. This allows you to have n-ary communications.
Note that messaging also has many disadvantages. Performance, for one, can be slower in many circumstances due to the overhead of having the messaging middleman. For a complete comparison of when to (and when not to) use messaging, see Chapter 13.
Message-oriented middleware (MOM) is a term used to refer to any infrastructure that supports messaging. Avariety of products are considered to have a MOM-based architecture. Examples include Tibco Rendezvous, IBM MQSeries, BEA Tuxedo/Q, Microsoft MSMQ, Talarian SmartSockets, Progress SonicMQ, and Fiorano FioranoMQ. These products can give you a whole host of value-added services, such as guaranteed message delivery, fault tolerance, load balancing of destinations, subscriber throttling of message consumption, inactive subscribers, and much, much more. By allowing the MOM server to address these infrastructure issues, you can focus on the business task at hand.
The Java Message Service (JMS)Over the years, MOM systems have evolved in a proprietary way. Each prod-uct has its own API, which creates vendor lock-in because code is not portable to other messaging systems. It also hurts developers, because they need to relearn each messaging product's proprietary API.
The Java Message Service (JMS) is a messaging standard, designed to eliminate many of the disadvantages that MOM-based products faced over past years. JMS has two parts: an API, which you write code to send and receive mes-sages, and a Service Provider Interface (SPI) where you plug in JMS drivers. A JMS driver knows how to talk to a specific MOM implementation. The JMS promise is that you can learn the JMS API once and reuse your messaging code with different plug-and-play MOM implementations (an idea similar to the other J2EE APIs, such as JNDI or JDBC).
How Does Guaranteed Message Delivery Work? With guaranteed message delivery, the MOM system persists your messages to a file, database, or other store. Your message resides in the persistent store until it's sent to a message consumer, and the message consumer acknowledges the consumption of the message. If the acknowledgement of a message is not received in a reasonable amount of time, the message remains on the persistent store and is redelivered.
This feature is beneficial when the message consumer is brought down on a regular basis for maintenance, and lost messages are unacceptable. This is especially true in industries such as financial services, where messages represent securities changing hands.
A variation on the guaranteed message delivery theme is certified message delivery. Certified message delivery not only ensures the delivery of a message from a producer to a consumer, but also generates a consumption receipt that is delivered to the message originator, indicating a successful consumption of the message. Certified message delivery is used by producers to better manage communication with consumers.
Another variation of guaranteed message delivery is called store and forward. Store and forward allows a message producer to successfully send a message to an inactive MOM system. The producer transparently spools the message to a local store until the MOM system is reactivated, at which point the message is delivered to the MOM system and forwarded to any available consumers. Guaranteed message delivery without the store-and-forward option requires producers to send messages to active MOM systems, but consumers do not have to be active. Store and forward with guaranteed message delivery allows messages to be sent whether MOM systems or consumers are active or inactive.
Let's explore the JMS API and see how to write a simple JMS program that publishes messages.
Messaging DomainsWhen you perform messaging, you need to choose a domain. A domain is a fancy word for style of messaging. The types of domains are:
Publish/subscribe (pub/sub). Publish/subscribe is analogous to watching television. Many TV stations broadcast their signals, and many people listen to those broadcasts. Thus, with publish/subscribe, you can have many message producers talking to many message consumers. In this sense, the pub/sub domain is an implementation of a distributed event-driven processing model. Subscribers (listeners) register their interest in a particular event topic. Publishers (event sources) create messages (events) that are distributed to all of the subscribers (listeners). Producers aren't hard-coded to use specific consumers; rather, the MOM system maintains the subscriber list.
Point-to-point (PTP). Point-to-point is analogous to calling a toll-free number and leaving a voice mail. Some person will listen to your voice mail and then delete it. Thus, with point-to-point, you can have only a single consumer for each message. Multiple consumers can grab messages off the queue, but any given message is consumed exactly once. In this sense, point-to-point is a degenerate case of publish/subscribe. Multiple producers can send messages to the queue, but each message is delivered only to a single consumer. The way this works is that publishers send messages directly to the consumer or to a centralized queue. Messages are typically distributed off the queue in a first-in, first-out (FIFO) order, but this isn't assured.
The difference between publish/subscribe and point-to-point is shown in Figure 8.2....
Table of Contents
|The Motivation for Enterprise JavaBeans||4|
|Divide and Conquer to the Extreme with Reusable Services||9|
|Introducing Enterprise JavaBeans||11|
|The EJB Ecosystem||15|
|The Java 2 Platform, Enterprise Edition (J2EE)||21|
|Chapter 2||EJB Fundamentals||27|
|Distributed Objects: The Foundation for EJB||30|
|Distributed Objects and Middleware||32|
|What Constitutes an Enterprise Bean?||36|
|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|
|Running the System||72|
|Implementing Component Interfaces||73|
|Part 2||The Triad of Beans||77|
|Chapter 4||Introduction to Session Beans||79|
|Session Bean Lifetime||79|
|Session Bean Subtypes||80|
|Special Characteristics of Stateful Session Beans||83|
|Chapter 5||Writing Session Bean Web Services||103|
|Web Services Concepts||103|
|Implementing a Web Service||110|
|Implementing a Web Service Client||114|
|Chapter 6||Introduction to Entity Beans||119|
|What Is an Entity Bean?||122|
|Features of Entity Beans||125|
|Chapter 7||Writing Bean-Managed Persistent Entity Beans||141|
|Entity Bean Coding Basics||141|
|Bean-Managed Persistence Example: A Bank Account||150|
|Running the Client Program||175|
|Putting It All Together: Walking through a BMP Entity Bean's Life Cycle||177|
|Chapter 8||Writing Container-Managed Persistent Entity Beans||181|
|Features of CMP Entity Beans||181|
|Implementation Guidelines for Container-Managed Persistence||191|
|Container-Managed Persistence Example: A Product Line||196|
|Running the Client Program||214|
|The Life Cycle of a CMP Entity Bean||214|
|Chapter 9||Introduction to Message-Driven Beans||217|
|Motivation to Use Message-Driven Beans||217|
|The Java Message Service||219|
|Integrating JMS with EJB||226|
|Developing Message-Driven Beans||231|
|JMS Message-Driven Bean Gotchas||244|
|Chapter 10||Adding Functionality to Your Beans||255|
|Calling Beans from Other Beans||255|
|Part 3||Advanced Enterprise JavaBeans Concepts||267|
|Chapter 11||EJB Best Practices||269|
|When to Use EJB||270|
|How to Choose a Web Application Framework to Work with EJB||272|
|Applying Model Driven Development in EJB Projects||275|
|Applying Extreme Programming in EJB Projects||277|
|Implementing Client-Side Callback Functionality in EJB||282|
|Choosing Between Servlets and Stateless Session Beans as Service Endpoints||284|
|Considering the Use of Aspect-Oriented Programming Techniques in EJB Projects||284|
|Reflection, Dynamic Proxy, and EJB||287|
|Deploying EJB Applications to Various Application Servers||288|
|Inheritance and Code Reuse in EJB||291|
|Writing Singletons in EJB||293|
|When to Use XML with EJB||293|
|When to Use Messaging Versus RMI-IIOP||294|
|Motivation for Transactions||300|
|Benefits of Transactions||303|
|Enlisting in Transactions with Enterprise JavaBeans||310|
|Programmatic Transactions in EJB||324|
|Transactions from Client Code||330|
|Designing Transactional Conversations in EJB||343|
|J2EE Transaction Service and Extended Transactions||346|
|Web Application Security||353|
|Understanding EJB Security||357|
|Web Services Security||381|
|Chapter 14||EJB Timers||391|
|EJB and Scheduling||392|
|The EJB Timer Service||394|
|Timer Example: CleanDayLimitOrdersEJB||399|
|Strengths and Limitations of EJB Timer Service||406|
|Chapter 15||BMP and CMP Relationships||409|
|The CMP and BMP Difference||410|
|Aggregation Versus Composition and Cascading Deletes||434|
|Relationships and EJB-QL||436|
|Chapter 16||Persistence Best Practices||445|
|Comparing Entity Beans with Other Persistence Approaches||446|
|Choosing Between CMP and BMP||450|
|Choosing the Right Granularity for Entity Beans||453|
|Persistence Tips and Tricks||454|
|Chapter 17||EJB Integration||477|
|Why Does Integration Matter?||477|
|EJB and Integration||479|
|J2EE Connector Architecture||480|
|The J2EE Connector API||486|
|Connector Example: OutboundLoanRA||508|
|Integration Best Practice: When to Use Which Technology||541|
|Chapter 18||EJB Performance Optimizations||545|
|It Pays to Be Proactive!||545|
|The Stateful Versus Stateless Debate from a Performance Point of View||547|
|How to Guarantee a Response Time with Capacity Planning||549|
|Use Session Facade for Better Performance||550|
|Choosing Between Local Interfaces and Remote Interfaces||552|
|Partitioning Your Resources||553|
|Tuning Stateless Session Beans||554|
|Tuning Stateful Session Beans||555|
|Tuning Entity Beans||556|
|Tuning Message-Driven Beans||563|
|Tuning Java Virtual Machine||563|
|Miscellaneous Tuning Tips||565|
|Choosing the Right EJB Server||567|
|Overview of Large-Scale Systems||569|
|Instrumenting Clustered EJBs||578|
|Other EJB Clustering Issues||589|
|Chapter 20||Starting Your EJB Project on the Right Foot||593|
|Get the Business Requirements Down||593|
|Decide Whether J2EE Is the Right Choice||594|
|Staff Your Project||598|
|Design Your Complete Object Model||600|
|Implement a Single Vertical Slice||601|
|Choose an Application Server||603|
|Divide Your Team||604|
|Invest in Tools||606|
|Invest in a Standard Build Process||607|
|Chapter 21||Choosing an EJB Server||609|
|J2EE Standard Compliance||610|
|Third-Party JDBC Driver Support||611|
|Deferred Database Writes||611|
|Pluggable Persistence Providers||611|
|In-Memory Data Cache||612|
|Integrated Tier Support||612|
|UML Editor Integration||615|
|Intelligent Load Balancing||615|
|Stateless Transparent Fail-over||615|
|Java Management Extension (JMX)||616|
|Automatic EJB Generation||617|
|Superior Messaging Architecture||618|
|Provided EJB Components||619|
|Chapter 22||EJB-J2EE Integration: Building a Complete Application||623|
|The Business Problem||623|
|A Preview of the Final Web Site||624|
|Scoping the Technical Requirements||630|
|Appendix A||RMI-IIOP and JNDI Tutorial||653|
|Appendix B||CORBA Interoperability||683|
|Appendix C||Deployment Descriptor Reference||705|
|Appendix D||The EJB Query Language (EJB-QL)||739|
|Appendix E||EJB Quick Reference Guide||757|
I was part of a task force to add enterprise features to this backbone, such as transaction control, security, and load-balancing. Our goal was to improve the backbone into a product worthy of large-scale deployment.
So that day, after hours of racking my brain, I finally finished crafting what I believed to be a highly creative and optimal load-balancing strategy. Looking for feedback, I walked to my friend Court Demas' office. Court is one of those developers who can really pick apart almost any design and expose its flaws— a unique quality that only a few developers I know have.
Walking into Court's office, I was expecting a typical developer-level conversation, and that's what I received. We turned the design inside and out, marking up my freshly printed hard copy with scribbles and other unintelligible comments that only we could understand. Finally, satisfied that we had reached a conclusion, I thanked Court and walked toward the door, prepared to implement the changes we had agreed upon.
But I didn't make it that far. Court said something to me that would change my way of thinking. His comment baffled and confused me at first, but would eventually result in a complete paradigm shift and career move for me. What did Court say? Nothing profound, but simply, "You know Ed, this stuff is really what Enterprise JavaBeans is for."
At first, I had no idea what he was talking about. Enterprise JavaBeans? What's that? Something like regular JavaBeans? Eventually, Court managed to explain to me what EJB was. And once he explained it, I knew that Trilogy had to do a 180-degree turn or lose its competitive advantage.
You see, EJB is a specification for a server-side component marketplace. EJB enables you to purchase off-the-shelf components from one vendor, combine them with components from another vendor, and run those components in an application server written by yet a third vendor. This means companies can collaborate on the server side. EJB enables you to buy, rather than build, elements of server-side applications.
The EJB value proposition had strong ramifications for Trilogy. EJB represented a way for Trilogy to get out of the middleware business and concentrate on its e-commerce strategic efforts. This meant discarding the backbone completely in favor of a third-party vendor's architecture. Not only would this reduce Trilogy's maintenance costs, but it would also solidify its software suite, since their middleware would now be written by professionals who had been in the business for 20 years. This proposition would eventually lead to Trilogy forming an entirely new business unit.
I decided to start researching EJB and pushing for Trilogy to adopt it. I went to the Sun Microsystems Web page, downloaded the EJB 1.0 specification in PDF form, and printed it out. Back then, the specification was about a third of the size it is today.
Understanding the specification turned out to be much more challenging than downloading it. The specification was written for system-level vendors and was not meant to be a tutorial for end developers. The section on entity beans, for example, took me a good two months to really grasp, as the notion of persistent components was new to me.
This arduous struggle with understanding the EJB specification is what eventually led me to write this book for you. This book represents everything I wish I had when I first started using EJB in 1998. So what is this book about? Well, it may be more accurate to tell you what this book is not. This is not EJB propaganda. It is not a book on how to write EJB code on any single application server. This is not a nice book that paints a perfect picture of the EJB world. Nor is it an advertisement for any particular EJB product or a campaign to rid the world of Microsoft.
The goal of this book is to help you. I want you to be able to craft solid, secure, and scalable server-side deployments. As you read this book, you'll learn how to design, implement, and deploy EJB solutions. This book covers both the vision and the reality of EJB from an independent developer's perspective. I hope it will prepare you for the challenges you will face.
I wish the grass was greener and that I could write a book on how clean and portable EJB is; but the truth is that this technology is not perfect, and you should know exactly what the imperfections are. I will expose you to the gruesome and incompatible parts of EJB and also explain how the industry is solving these problems.
Indeed, the newer specifications (especially EJB 2.0) improve portability and reduce incompatibilities tremendously. I hope that by the time you're done reading this book, you are convinced that the vision of EJB is solid, and the future is very bright.
My hope is that I can save you time and energy, and aid you in designing wellcrafted server-side deployments. But this is merely the beginning. The EJB marketplace is just getting started, and there's a whole lot more work ahead. I encourage you to take an active role in the middleware industry and to work with me taking EJB to the next level. Feel free to write your experiences, tips, and design strategies, and post them on TheServerSide.com to share with others. Our goal is to increase our knowledge of EJB as a community, and together, we can do it.