www.traintelco.com www.traintelco.com Java Message Service www.traintelco.com www.traintelco.com SECOND EDITION Java Message Service Mark Richards, Richard Monson-Haefel, and David A. Chappell Beijing • Cambridge • Farnham • Köln • Sebastopol • Taipei • Tokyo www.traintelco.com Java Message Service, Second Edition by Mark Richards, Richard Monson-Haefel, and David A. Chappell Copyright © 2009 Mark Richards. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com. Editors: Mike Loukides and Julie Steele Production Editor: Sarah Schneider Production Services: Appingo, Inc. Cover Designer: Karen Montgomery Interior Designer: David Futato Illustrator: Robert Romano Printing History: May 2009: Second Edition. O’Reilly and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Java Message Service, Second Edition, the image of a passenger pigeon, and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information con- tained herein. ISBN: 978-0-596-52204-9 [C] 1242320347 www.traintelco.com Table of Contents Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii 1. Messaging Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 The Advantages of Messaging 3 Heterogeneous Integration 3 Reduce System Bottlenecks 3 Increase Scalability 4 Increase End User Productivity 4 Architecture Flexibility and Agility 5 Enterprise Messaging 5 Centralized Architectures 7 Decentralized Architectures 7 Hybrid Architectures 8 Centralized Architecture As a Model 8 Messaging Models 9 Point-to-Point 10 Publish-and-Subscribe 10 JMS API 11 Point-to-Point API 13 Publish-and-Subscribe API 14 Real-World Scenarios 14 Service-Oriented Architecture 15 Event-Driven Architecture 16 Heterogeneous Platform Integration 16 Enterprise Application Integration 17 Business-to-Business 17 Geographic Dispersion 18 Information Broadcasting 18 Building Dynamic Systems 18 RPC Versus Asynchronous Messaging 21 v www.traintelco.com Tightly Coupled RPC 21 Enterprise Messaging 23 2. Developing a Simple Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 The Chat Application 25 Getting Started with the Chat Example 28 Examining the Source Code 30 Sessions and Threading 39 3. Anatomy of a JMS Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Headers 42 Automatically Assigned Headers 43 Developer-Assigned Headers 46 Properties 47 Application-Specific Properties 47 JMS-Defined Properties 49 Provider-Specific Properties 50 Message Types 50 Message 50 TextMessage 51 ObjectMessage 52 BytesMessage 53 StreamMessage 56 MapMessage 58 Read-Only Messages 60 Client-Acknowledged Messages 61 Interoperability and Portability of Messages 61 4. Point-to-Point Messaging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 Point-to-Point Overview 63 When to Use Point-to-Point Messaging 66 The QBorrower and QLender Application 67 Configuring and Running the Application 67 The QBorrower Class 69 The QLender Class 76 Message Correlation 81 Dynamic Versus Administered Queues 83 Load Balancing Using Multiple Receivers 84 Examining a Queue 85 5. Publish-and-Subscribe Messaging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Publish-and-Subscribe Overview 87 When to Use Publish-and-Subscribe Messaging 89 vi | Table of Contents www.traintelco.com The TBorrower and TLender Application 90 Configuring and Running the Application 90 The TLender Class 92 The TBorrower Class 96 Durable Versus Nondurable Subscribers 100 Dynamic Versus Administered Subscribers 101 Unsubscribing Dynamic Durable Subscribers 104 Temporary Topics 104 6. Message Filtering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 Message Selectors 109 Identifiers 110 Literals 111 Comparison Operators 111 Arithmetic Operators 113 Declaring a Message Selector 114 Message Selector Examples 116 Managing Claims in an HMO 116 Notification of Certain Bids on Inventory 116 Priority Handling 116 Stock Trade Order Auditing 117 Not Delivered Semantics 117 Design Considerations 118 7. Guaranteed Messaging and Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 Guaranteed Messaging 125 Message Autonomy 126 Store-and-Forward Messaging 126 Message Acknowledgments and Failure Conditions 126 Message Acknowledgments 127 AUTO_ACKNOWLEDGE 127 DUPS_OK_ACKNOWLEDGE 132 CLIENT_ACKNOWLEDGE 132 Message Groups and Acknowledgment 133 Handling Redelivery of Messages in an Application 134 Message Groups Example 134 Message Grouping and Multiple Receivers 143 Transacted Messages 145 Creating and Using a JMS Transaction 147 Transacted Session Example 147 Distributed Transactions 150 Lost Connections 151 The ExceptionListener Example 152 Table of Contents | vii www.traintelco.com Dead Message Queues 153 8. Java EE and Message-Driven Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 Java EE Overview 155 Enterprise JavaBeans 156 Enterprise JavaBeans 3.0 (EJB3) Overview 157 Simplified Bean Development 158 Dependency Injection 158 Simplified Callback Methods 159 Programmatic Defaults 159 Interceptors 160 Java Persistence API 162 JMS Resources in Java EE 162 The JNDI Environment Naming Context (ENC) 164 Message-Driven Beans 166 Concurrent Processing and Scalability 168 Defining Message-Driven Beans 168 Message-Driven Bean Use Cases 171 Message Facade 171 Transformation and Routing 173 9. Spring and JMS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 Spring Messaging Architecture 177 JmsTemplate Overview 180 Send Methods 181 convertAndSend Methods 181 receive and receiveSelected Methods 182 receiveAndConvert Methods 183 Connection Factories and JMS Destinations 184 Using JNDI 184 Using Native Classes 187 Sending Messages 189 Using the send Method 190 Using the convertAndSend Method 191 Using a Nondefault JMS Destination 193 Receiving Messages Synchronously 195 Message-Driven POJOs 198 The Spring Message Listener Container 198 MDP Option 1: Using the MessageListener Interface 199 MDP Option 2: Using the SessionAwareMessageListener Interface 201 MDP Option 3: Using the MessageListenerAdapter 202 Message Conversion Limitations 207 The Spring JMS Namespace 208 viii | Table of Contents www.traintelco.com [...]... Chapter 6, Message Filtering Provides a detailed explanation of message filtering using message selectors Chapter 7, Guaranteed Messaging and Transactions Provides an in-depth explanation of advanced topics, including guaranteed messaging, transactions, acknowledgments, message grouping, and failures Chapter 8, Java EE and Message- Driven Beans Provides an overview of the Java 2, Enterprise Edition (Java. .. generally, one JMS provider In addition, a JMS client that produces a message is called a message producer, while a JMS client that receives a message is called a message consumer A JMS client can be both a message producer and a message consumer When we use the term consumer or producer, we mean a JMS client that consumes messages or produces messages, respectively We use this terminology throughout the book... need to reliably exchange large quantities of messages Enterprise messaging vendors use different message formats and network protocols for exchanging messages, but the basic semantics are the same An API is used to create a message, load the application data (message payload), assign routing information, and send the message The same API is used to receive messages produced by other applications In all... publish-and-subscribe, including how to filter messages using message selectors Chapters 7 and 10 should be considered “advanced topics,” covering deployment and administration of messaging systems Chapter 8 provides an overview of the Java 2, Enterprise Edition (Java EE) with regard xiv | Preface www.traintelco.com to JMS, including coverage of message- driven beans as part of the Enterprise JavaBeans 3.0 specification... discussion of some of the more common messaging anti-patterns Appendix A, The Java Message Service API Provides a quick reference to the classes and interfaces defined in the JMS package Appendix B, Message Headers Provides detailed information about message headers Appendix C, Message Properties Provides detailed information about message properties Appendix D, Installing and Configuring ActiveMQ Provides... Request/Reply Messaging Design Messaging Design Anti-Patterns Single-Purpose Queue Message Priority Overuse Message Header Misuse 229 230 231 232 236 236 240 240 A The Java Message Service API 245 B Message Headers 265 C Message Properties 277 D Installing... concept of enterprise messaging is that messages are delivered asynchronously from one system to others over a network To deliver a message asynchronously means the sender is not required to wait for the message to be received or handled by the recipient; it is free to send the message and continue processing Asynchronous messages are treated as autonomous units—each message is self-contained and carries... included in the first edition I added several new chapters that were not included in the first edition, for obvious reasons You will find new sections in the first chapter on the JMS API, updated messaging use cases, and a discussion of how messaging has changed how we design systems You will also find new chapters on message filtering, Java EE and messagedriven beans, Spring JMS and message- driven POJOs,... model, messages are published to a virtual channel called a topic Message producers are called publishers, whereas message consumers are called subscribers Unlike the point-to-point model, messages published to a topic using the publish-and-subscribe model can be received by multiple subscribers This technique is sometimes referred to as broadcasting a message Every subscriber receives a copy of each message. .. messaging systems, applications exchange messages through virtual channels called destinations When a message is sent, it’s addressed to a destination (i.e., queue or topic), not a specific application Any application that subscribes or registers an interest in that destination may receive the message In this way, the applications that receive messages and those that send messages are decoupled Senders and . 49 Provider-Specific Properties 50 Message Types 50 Message 50 TextMessage 51 ObjectMessage 52 BytesMessage 53 StreamMessage 56 MapMessage 58 Read-Only Messages 60 Client-Acknowledged Messages 61 Interoperability. transactions, acknowledgments, message grouping, and failures. Chapter 8, Java EE and Message- Driven Beans Provides an overview of the Java 2, Enterprise Edition (Java EE) version 3.0 with regard. 132 CLIENT_ACKNOWLEDGE 132 Message Groups and Acknowledgment 133 Handling Redelivery of Messages in an Application 134 Message Groups Example 134 Message Grouping and Multiple Receivers 143 Transacted Messages