Tải bản đầy đủ

ActiveMQ in action

Bruce Snyder
Dejan Bosanac
Rob Davies

MANNING
www.it-ebooks.info


ActiveMQ in Action

www.it-ebooks.info


www.it-ebooks.info


ActiveMQ in Action
BRUCE SNYDER
DEJAN BOSANAC
ROB DAVIES


MANNING
Greenwich
(74° w. long.)

www.it-ebooks.info


For online information and ordering of this and other Manning books, please visit
www.manning.com. The publisher offers discounts on this book when ordered in quantity.
For more information, please contact
Special Sales Department
Manning Publications Co.
180 Broad St.
Suite 1323
Stamford, CT 06901
Email: orders@manning.com

©2011 by Manning Publications Co. All rights reserved.

No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in
any form or by means electronic, mechanical, photocopying, or otherwise, without prior written
permission of the publisher.

Many of the designations used by manufacturers and sellers to distinguish their products are
claimed as trademarks. Where those designations appear in the book, and Manning
Publications was aware of a trademark claim, the designations have been printed in initial caps
or all caps.

Recognizing the importance of preserving what has been written, it is Manning’s policy to have
the books we publish printed on acid-free paper, and we exert our best efforts to that end.
Recognizing also our responsibility to conserve the resources of our planet, Manning books
are printed on paper that is at least 15 percent recycled and processed without the use of
elemental chlorine

Manning Publications Co.
180 Broad St.
Suite 1323
Stamford, CT 06901

Development editor:


Copyeditor:
Proofreader:
Typesetter:
Cover designer:

Jeff Bleiel
Benjamin Berg
Katie Tennant
Dottie Marsico
Marija Tudor

ISBN 978-1-933988-94-8
Printed in the United States of America
1 2 3 4 5 6 7 8 9 10 – MAL – 16 15 14 13 12 11

www.it-ebooks.info


brief contents
PART 1 AN INTRODUCTION TO MESSAGING
AND ACTIVEMQ ........................................................... 1
1



Introduction to Apache ActiveMQ

3

2



Understanding message-oriented middleware and JMS

3



The ActiveMQ in Action examples

17

42

PART 2 CONFIGURING STANDARD
ACTIVEMQ COMPONENTS .......................................... 55
4



Connecting to ActiveMQ

5



ActiveMQ message storage

6



Securing ActiveMQ 117

57
96

PART 3 USING ACTIVEMQ TO BUILD
MESSAGING APPLICATIONS . ....................................... 143
7



Creating Java applications with ActiveMQ

8



Integrating ActiveMQ with application servers

9



ActiveMQ messaging for other languages
v

www.it-ebooks.info

145
221

174


vi

BRIEF CONTENTS

PART 4 ADVANCED FEATURES IN ACTIVEMQ ......................... 255
10



Deploying ActiveMQ in the enterprise

11



257

ActiveMQ broker features in action

12



Advanced client options

13



Tuning ActiveMQ for performance

14



Administering and monitoring ActiveMQ 331

277

295

www.it-ebooks.info

312


contents
preface xv
acknowledgments xvii
about this book xix

PART 1 AN INTRODUCTION TO MESSAGING
AND ACTIVEMQ ...................................................1

1

Introduction to Apache ActiveMQ
1.1
1.2

ActiveMQ features 4
Using ActiveMQ: why and when?
Loose coupling and ActiveMQ

1.3

3

6



6
When to use ActiveMQ

8

Getting started with ActiveMQ 10
Downloading and installing the Java SE 10 Downloading
ActiveMQ 11 Examining the ActiveMQ directory 11
Starting up ActiveMQ 12




1.4
1.5

2

Running your first examples with ActiveMQ
Summary 16

14

Understanding message-oriented middleware and JMS
2.1
2.2

Introduction to enterprise messaging 18
What’s message-oriented middleware? 20
vii

www.it-ebooks.info

17


viii

CONTENTS

2.3
2.4

What’s the Java Message Service?
The JMS specification 23

21

JMS clients 23 Non-JMS clients 25 The JMS provider 25
The JMS message 25 JMS message internals 25 Message
selectors 29 JMS domains 32 Administered objects 35










2.5



Using the JMS APIs to create JMS applications
A simple JMS application

2.6

3

Summary

36



Message-driven beans

42

Downloading Maven and compiling the examples
Use case one: the stock portfolio example 45
Running the stock portfolio example

3.3

43

46

Use case two: the job queue example 50
Running the job queue example

3.4

39

41

The ActiveMQ in Action examples
3.1
3.2

35

Summary

51

53

PART 2 CONFIGURING STANDARD
ACTIVEMQ COMPONENTS...................................55

4

Connecting to ActiveMQ
4.1
4.2

4.3

57

Understanding connector URIs
Transport connectors 60

58

Configuring transport connectors
portfolio example 61



60

Adapting the stock

Connecting to ActiveMQ over the network 63
Transmission Control Protocol (TCP) 64 New I/O API
protocol (NIO) 66 User Datagram Protocol (UDP) 68
Secure Sockets Layer Protocol (SSL) 70 Hypertext Transfer
Protocol (HTTP/HTTPS) 77






4.4
4.5

Connecting to ActiveMQ inside the virtual machine
(VM connector) 79
Network connectors 81
Static networks

4.6

Summary

83



Dynamic networks

94

www.it-ebooks.info

88


ix

CONTENTS

5

ActiveMQ message storage
5.1
5.2

96

How are messages stored by ActiveMQ? 97
The KahaDB message store 98
The KahaDB message store internals 99 The KahaDB message
store directory structure 100 Configuring the KahaDB message
store 101




5.3

The AMQ message store

103

The AMQ message store internals 103 The AMQ message store
directory structure 104 Configuring the AMQ message store 105




5.4

The JDBC message store

107

Databases supported by the JDBC message store 107 The JDBC
message store schema 108 Configuring the JDBC message
store 109 Using the JDBC message store with the ActiveMQ
journal 111






5.5

The memory message store
Configuring the memory store

5.6

111
112

Caching messages in the broker for consumers

113

How message caching for consumers works 113 The ActiveMQ
subscription recovery policies 114 Configuring the subscription
recovery policy 115




5.7

6

Summary

116

Securing ActiveMQ
6.1

Authentication

117
118

Configuring the simple authentication plug-in
Configuring the JAAS plug-in 121

6.2

Authorization

123

Destination-level authorization
authorization 127

6.3

124



Building a custom security plug-in
Implementing the plug-in 132
Testing the plug-in 134

6.4

118

Certificate-based security



Message-level

131

Configuring the plug-in

133

135

Preparing certificates 136 Creating a truststore 136
Configuring the broker 138 Authorization explained 139
Testing it out 139




6.5

Summary

142

www.it-ebooks.info


x

CONTENTS

PART 3 USING ACTIVEMQ TO BUILD
MESSAGING APPLICATIONS ................................143

7

Creating Java applications with ActiveMQ
7.1

Embedding ActiveMQ using Java

145

146

Embedding ActiveMQ using the BrokerService 147
Embedding ActiveMQ using the BrokerFactory 149

7.2

Embedding ActiveMQ using Spring 150
Pure Spring XML 151 Using the BrokerFactoryBean 153
Using Apache XBean with Spring 154 Using a custom XML
namespace with Spring 156




7.3

Implementing request/reply with JMS

158

Implementing the server and the worker 160 Implementing the
client 162 Running the request/reply example 164




7.4

Writing JMS clients using Spring

165

Configuring JMS connections 166 Configuring JMS
destinations 167 Creating JMS consumers 167
Creating JMS producers 168 Putting it all together 171






7.5

8

Summary

172

Integrating ActiveMQ with application servers
8.1
8.2

174

The sample web application 176
Integrating with Apache Tomcat 181
Using local JNDI to integrate ActiveMQ with Tomcat 182
Using global JNDI to integrate ActiveMQ with Tomcat 184

8.3

Integrating with Jetty

187

Using local JNDI to integrate ActiveMQ with Jetty 187
Using global JNDI to integrate ActiveMQ with Jetty 189

8.4

Integrating with Apache Geronimo

192

Installing Geronimo and configuring the ActiveMQ plug-in
in Geronimo 192 Configuring the ActiveMQ JMS resources in
Geronimo 196 Preparing the sample application for deployment
in Geronimo 202 Deploying and verifying the sample
application in Geronimo 205






8.5

Integrating with JBoss

208

Installing JBoss and configuring the ActiveMQ resource adapter
in JBoss 209 Configuring the ActiveMQ JMS resources in
JBoss 212 Preparing the sample application for deployment




www.it-ebooks.info


xi

CONTENTS

in JBoss
in JBoss

8.6

212
215

Deploying and verifying the sample application



ActiveMQ and JNDI

217

Client-side JNDI configuration

8.7

9

Summary

217

220

ActiveMQ messaging for other languages
9.1
9.2

221

Adapting the stock portfolio example 222
Messaging for scripting languages 224
STOMP protocol basics 224 Configuring STOMP
transport 226 Ruby STOMP consumer 227 Python
STOMP consumer 229 PHP STOMP consumer 233
Perl STOMP consumer 234 Advanced messaging with
STOMP 236










9.3

Messaging for compiled languages

241

Writing a C# consumer (using the NMS API) 242
Writing a C++ consumer (using the CMS API) 244

9.4

Messaging on the web with ActiveMQ
Using the ActiveMQ REST API
Ajax API 250

9.5

Summary

248



247

Using the ActiveMQ

254

PART 4 ADVANCED FEATURES IN ACTIVEMQ .................255

10

Deploying ActiveMQ in the enterprise
10.1

Configuring ActiveMQ for high availability
Shared nothing master/slave
master/slave 261

10.2

257

258



Shared storage

How ActiveMQ passes messages across a network
of brokers 263
Store and forward 264 Network discovery
Network configuration 268


10.3

266

Deploying ActiveMQ for large numbers
of concurrent applications 272
Vertical scaling 272 Horizontal scaling
Traffic partitioning 275


10.4

258

Summary

276

www.it-ebooks.info

275


xii

CONTENTS

11

ActiveMQ broker features in action
11.1

277

Wildcards and composite destinations

278

Consume from multiple destinations using wildcards
Sending a message to multiple destinations 279

11.2
11.3
11.4
11.5
11.6

278

Advisory messages 280
Supercharge JMS topics by going virtual 284
Retroactive consumers 286
Message redelivery and dead-letter queues 287
Extending functionality with interceptor plug-ins

288

Visualization 288 Enhanced logging 290 Central
timestamp messages with the timestamp interceptor plug-in 291
Statistics 291


11.7
11.8

12

Routing engine with Apache Camel framework 292
Summary 294

Advanced client options
12.1



295

Exclusive consumers

296

Selecting an exclusive message consumer 296
consumers to provide a distributed lock 297

12.2
12.3
12.4
12.5
12.6
12.7

13

Using exclusive

Message groups 298
ActiveMQ streams 301
Blob messages 303
Surviving network or broker failure
with the failover protocol 305
Scheduling messages to be delivered
by ActiveMQ in the future 309
Summary 311

Tuning ActiveMQ for performance
13.1



General techniques

312

313

Persistent versus nonpersistent messages 313
Transactions 314 Embedding brokers 315 Tuning the
OpenWire protocol 318 Tuning the TCP transport 319






13.2

Optimizing message producers
Asynchronous send

13.3

319



319

Producer flow control

Optimizing message consumers

www.it-ebooks.info

323

320


xiii

CONTENTS

Prefetch limit 323 Delivery and acknowledgment of
messages 325 Asynchronous dispatch 326




13.4
13.5

14

Tuning in action
Summary 330

327

Administering and monitoring ActiveMQ
14.1

The JMX API and ActiveMQ

331

332

Local vs. remote JMX access 332 Exposing the JMX MBeans for
ActiveMQ 334 Exploring broker properties using the JMX
API 336 Advanced JMX configuration 339 Restricting
JMX access to a specific host 340 Configuring JMX password
authentication 341










14.2

Monitoring ActiveMQ with advisory messages
Configuring advisory support 344
messages 345 Conclusion 350



344

Using advisory



14.3

Tools for ActiveMQ administration

350

Command-line tools 350 Command agent
JConsole 357 Web console 359


355



14.4

Configuring ActiveMQ logging
Broker logging 361
event logging 365

14.5

Summary
index



360

Client logging

366

367

www.it-ebooks.info

362



Internal broker


www.it-ebooks.info


preface
To this day, enterprise message queuing is a concept that is not well understood by
most software developers. Commercial message-oriented middleware (MOM) did not
help to encourage adoption of the process; most of the MOMs on the market were
closed source, were costly to buy and support, and required trained system administrators for proper installation. ActiveMQ was developed to provide an open source alternative: its central theme is to put the power in the hands of developers. To date, the
ActiveMQ project has been successful in this endeavor by creating a MOM that is easy
to install, administer, and utilize, while providing a large number of unique features.
Today, six or seven years after ActiveMQ was originally created, it’s surprising how
far and widely it is being used. ActiveMQ is a crucial component in many applications.
Used by small businesses and very large enterprises alike, ActiveMQ is deployed
throughout a wide variety of industries around the world including manufacturing,
government, retail, healthcare, finance, military, telecom, and many more. The versatility of not only ActiveMQ but also event-based systems has appealed to a very large
audience of users and that appeal continues to grow.
In writing a book about ActiveMQ, our intent was to provide a comprehensive
guide for its features and how to use them. Although ActiveMQ implements the JMS
specification, which has not moved in many years, ActiveMQ provides many features
beyond this spec and it has not stopped innovating. As is always the case, authoring a
book about software is a game of chase; as the software evolves, so must the book that
is still being written. We did our best to keep this book up to date with ActiveMQ 5.4.1.
We hope that, by reading this book, you will come to appreciate not only ActiveMQ
but the concepts surrounding event-based systems. After all, some of the largest systems in the world are designed using events and message queuing.
xv

www.it-ebooks.info


www.it-ebooks.info


acknowledgments
The authors would collectively like to thank the following individuals:
Jeff Bleiel, our development editor, for his work liaising with Manning—without
Jeff’s input, the book would not be what it is today; Gary Tully for his tireless technical
reviews of the entire book; the Manning staff for their arduous work on the book; Filip
Hanik for his assistance with Tomcat; Jan Bartel and Greg Wilkins for their assistance
with Jetty; David Jencks and Kevan Miller for their assistance with Geronimo; and
Jaikiran Pai for his assistance with JBoss.
We would also like to thank the following reviewers, who read the manuscript at
different stages of its development, for their invaluable feedback: Jeff Davis, Deepak
Vohra, Robert Hanson, Davide Piazza, David Strong, Tijs Rademakers, Prasad A.
Chodavarapu, John Merryman, Jeroen Benckhuijsen, Pratik Patel, Scott Dawson,
Jason Kolter, Rod Biresch, and Roberto Rojas.
Finally, thanks to the many readers of Manning’s Early Access Program (MEAP) for
their comments and input on early drafts of the manuscript posted in the Author
Online forum.

BRUCE SNYDER
I would like to thank my incredible wife Janene for her patience and understanding
during another book project, and my girls Bailey and Jade for reminding me what
really matters in life.

xvii

www.it-ebooks.info


xviii

ACKNOWLEDGMENTS

DEJAN BOSANAC
I would like to thank my lovely wife for supporting me through yet another book
project.

ROB DAVIES
I would like to thank my wife Karen for editing and proofreading my chapters; my
children Chris, Connor, and Michael for keeping the zombies at bay; and my dog Rex
for forcing me to go on walks in the snow and the rain.

www.it-ebooks.info


about this book
ActiveMQ in Action is for software architects, developers, and integrators interested in
enterprise message queuing in general and ActiveMQ in particular. This book is
designed to serve as part introduction and part reference for both beginners and
experienced application developers. It begins with an introduction to ActiveMQ and a
high-level overview of JMS, followed by a progressively deeper dive into Active MQ as
the book advances.
The concepts discussed throughout this book assume that the reader possesses
enough knowledge of Java EE to design and develop applications. Though such
knowledge is not a strict requirement, it will make it easier to grasp many of the concepts touched upon throughout the chapters. Chapter 9 even discusses using
ActiveMQ with languages other than Java, including C++, C#, JavaScript, Perl, PHP,
Python, and Ruby.

Roadmap
This book is divided into four parts:
Part 1 provides an introduction to ActiveMQ, a high-level overview of JMS, and a brief
discussion of the examples used throughout the book. Chapter 1 introduces ActiveMQ
at a high level and discusses why and when to use ActiveMQ. It also demonstrates how
to download and install ActiveMQ and how to run the examples that come with
ActiveMQ in Action.
Chapter 2 introduces enterprise messaging, message-oriented middleware (MOM)
and the JMS specification.
Chapter 3 introduces the examples to be used throughout ActiveMQ in Action.

xix

www.it-ebooks.info


xx

ABOUT THIS BOOK

Part 2 focuses on the three standard components in ActiveMQ including connectivity
into the message broker, message persistence, and message broker security.
Chapter 4 covers all the connectivity options for Active MQ. It discusses ActiveMQ
URIs and all the transport connectors for both client-to-broker and broker-to-broker
communications including TCP, NIO, STOMP, failover, SSL, HTTP/S, and much more.
Chapter 5 discusses message persistence in ActiveMQ; how messages are stored for
queues and topics, the four styles of message stores available, and message caching.
Chapter 6 introduces and elaborates on security in ActiveMQ. It covers authentication, authorization, and certificate-based security, as well as how to create a custom
security plug-in.
The theme of part 3 is using ActiveMQ to build applications using technologies such
as the Spring Framework, leading open source application servers, and numerous
applications beyond just Java.
Chapter 7 deals with creating Java applications using ActiveMQ. It shows some
options for embedding ActiveMQ in Java applications, developing a request/reply
application, and writing JMS clients using Spring.
Chapter 8 is all about integrating ActiveMQ with some popular open source application servers including Tomcat, Jetty, Geronimo, and JBoss. It also discusses the
client-side JNDI support provided by ActiveMQ.
Chapter 9 discusses messaging with ActiveMQ using languages other than Java
including C++, C#, JavaScript, Perl, PHP, Python, and Ruby.
Part 4 discusses advanced features in ActiveMQ such as high availability, scalability,
many advanced broker and client features, performance tuning, and administration
of ActiveMQ.
Chapter 10 discusses concepts around deploying ActiveMQ for production systems.
Topics in this chapter are focused on high availability and scalability.
Chapter 11 presents advanced features provided by ActiveMQ such as wildcards
and composite destinations, advisory messages, virtual topics, some info about
ActiveMQ plug-ins, and an introduction to message routing with Apache Camel.
Chapter 12 covers advanced ActiveMQ client features including exclusive consumers, message groups, ActiveMQ streams and large objects, the failover transport, and
message scheduling.
Chapter 13 deals with ActiveMQ performance tuning. It presents some general
tuning techniques covering such topics as persistent versus nonpersistent messages,
transactions, embedded brokers, tuning the wire level protocol, tuning the TCP transport, and some optimizations for message producers and message consumers.
Chapter 14 finishes up by discussing the administration and monitoring of
ActiveMQ. It shows how to configure ActiveMQ for JMX monitoring and demonstrates
this using JConsole. It also discusses and demonstrates the use of advisory messages
for monitoring ActiveMQ. There is also coverage of command-line tools, the command agent, use of XMPP, JConsole, and the web console. The discussion then moves
on to broker- and client-level logging.

www.it-ebooks.info


ABOUT THIS BOOK

xxi

CODE CONVENTIONS AND DOWNLOADS

This book contains many code examples in many different programming languages,
all of which are presented using a fixed-width font like this to set it apart from the
regular text. Many code listings are annotated to point out important items, and the
listings are discussed by the surrounding text.
The full source code that is presented in the book is freely available for download
from the publisher’s website at http://manning.com/ActiveMQinAction.
AUTHORS’ NOTE

This book was authored using DocBook XML and was processed using the Docbkx
Tools Maven plug-in on Mac OS X. Other items that became part of the book-writing
process include MacBook Pros, Google Docs, GMail, Foonz (until it shut down), FreeConferenceCall.com, barking dogs during conference calls, company acquisitions,
lots and lots of music, loud construction next door, sleepless nights, too much work
on airplanes, and plain old exhaustion.

Author Online
Purchase of ActiveMQ in Action includes free access to a private web forum run by Manning Publications where you can make comments about the book, ask technical questions, and receive help from the authors and from other users. To access the forum
and subscribe to it, point your web browser to www.manning.com/ActiveMQinAction.
This page provides information on how to get on the forum once you are registered,
what kind of help is available, and the rules of conduct on the forum.
Manning’s commitment to our readers is to provide a venue where a meaningful
dialog between individual readers and between readers and the authors can take
place. It is not a commitment to any specific amount of participation on the part of
the authors, whose contribution to the book’s forum remains voluntary (and unpaid).
We suggest you try asking them some challenging questions lest their interest stray!
The Author Online forum and the archives of previous discussions will be accessible from the publisher’s website as long as the book is in print.

About the cover illustration
The figure on the cover of ActiveMQ in Action is taken from a French travel book, Encyclopédie Des Voyages by J. G. De Saint-Sauveur, published in 1796. Travel for pleasure was
a relatively new phenomenon at the time and travel guides such as this one were popular, introducing both the tourist as well as the armchair traveler to the inhabitants of
other regions of France and abroad.
The diversity of the drawings in the Encyclopédie Des Voyages speaks vividly of the
uniqueness and individuality of the world’s towns and provinces just 200 years ago.
This was a time when the dress codes of two regions separated by a few dozen miles
identified people uniquely as belonging to one or the other. The travel guide brings
to life a sense of isolation and distance of that period and of every other historic
period except our own hyperkinetic present.

www.it-ebooks.info


xxii

ABOUT THIS BOOK

Dress codes have changed since then and the diversity by region, so rich at the
time, has faded away. It is now often hard to tell the inhabitant of one continent from
another. Perhaps, trying to view it optimistically, we have traded a cultural and visual
diversity for a more varied personal life, or a more varied and interesting intellectual
and technical life.
We at Manning celebrate the inventiveness, the initiative, and the fun of the computer business with book covers based on the rich diversity of regional life two centuries ago brought back to life by the pictures from this travel guide.

www.it-ebooks.info


Part 1
An introduction
to messaging
and ActiveMQ

A

pache ActiveMQ is a message broker for remote communication
between systems using the JMS (Java Message Service) specification. Although
ActiveMQ is written in Java, APIs for many languages other than Java are provided, including C/C++, .NET, Perl, PHP, Python, Ruby, and many more. This
book provides the information you need to understand, configure, and use
ActiveMQ successfully to meet the requirements of many business applications.
In part 1, you’ll be introduced to ActiveMQ briefly to get you up and running.
We’ll discuss the concepts surrounding message-oriented middleware and JMS so
that you have an adequate background on how enterprise messaging came to be
what it is today. We’ll also introduce the examples for the book, including their
use cases and how to run each example. We’ll use these examples throughout
the book, so it’s important to understand them before they’re applied through
the chapters. The chapters in part 1 provide a good base set of knowledge that
prepares you for the rest of the book.

www.it-ebooks.info


www.it-ebooks.info


Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay

×