RabbitMQ Cookbook - Sigismondo Boschi - E-Book

RabbitMQ Cookbook E-Book

Sigismondo Boschi

0,0
34,79 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

RabbitMQ is an open source message broker software (sometimes called message-oriented middleware) that implements the Advanced Message Queuing Protocol (AMQP). The RabbitMQ server is written in the Erlang programming language and is built on the Open Telecom Platform framework for clustering and failover. Messaging enables software applications to connect and scale. Applications can connect to each other as components of a larger application or to user devices and data.
RabbitMQ Cookbook touches on all the aspects of RabbitMQ messaging. You will learn how to use this enabling technology for the solution of highly scalable problems dictated by the dynamic requirements of Web and mobile architectures, based for example on cloud computing platforms. This is a practical guide with several examples that will help you to understand the usefulness and the power of RabbitMQ.
This book helps you learn the basic functionalities of RabbitMQ with simple examples which describe the use of RabbitMQ client APIs and how a RabbitMQ server works. You will find examples of RabbitMQ deployed in real-life use-cases, where its functionalities will be exploited combined with other technologies. This book helps you understand the advanced features of RabbitMQ that are useful for even the most demanding programmer. Over the course of the book, you will learn about the usage of basic AMQP functionalities and use RabbitMQ to let decoupled applications exchange messages as per enterprise integration applications. The same building blocks are used to implement the architecture of highly scalable applications like today's social networks, and they are presented in the book with some examples. You will also learn how to extend RabbitMQ functionalities by implementing Erlang plugins.
This book combines information with detailed examples coupled with screenshots and diagrams to help you create a messaging application with ease.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 290

Veröffentlichungsjahr: 2013

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Table of Contents

RabbitMQ Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers and more
Why Subscribe?
Free Access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. Working with AMQP
Introduction
Connecting to the broker
Getting ready
How to do it…
How it works…
There's more…
Producing messages
Getting ready
How to do it…
How it works…
There's more…
How to check the RabbitMQ status
Checking whether a queue already exists
Consuming messages
Getting ready
How to do it…
How it works…
There's more…
More on consumer threads
Blocking semantics
See also
Using body serialization with JSON
Getting ready
How to do it…
How it works…
There's more…
Using RPC with messaging
Getting ready
How to do it…
How it works…
There's more…
Using blocking RPC
Scalability notes
Broadcasting messages
Getting ready
How to do it…
How it works…
There's more…
See also
Working with message routing using direct exchanges
Getting ready
How to do it…
How it works…
There's more…
Working with message routing using topic exchanges
Getting ready
How to do it…
How it works…
There's more…
Guaranteeing message processing
Getting ready
How to do it…
How it works…
There's more…
See also
Distributing messages to many consumers
Getting ready
How to do it…
How it works...
See also…
Using message properties
Getting ready
How to do it…
How it works…
There's more…
See also
Messaging with transactions
Getting ready
How to do it…
How it works…
There's more…
See also
Handling unroutable messages
Getting ready
How to do it…
How it works…
There's more…
2. Going beyond the AMQP Standard
Introduction
How to let messages expire
Getting ready
How to do it...
How it works...
See also
How to let messages expire on specific queues
Getting ready
How to do it...
How it works...
There's more...
See also
How to let queues expire
Getting ready
How to do it...
How it works...
Managing rejected or expired messages
Getting ready
How to do it...
How it works...
There's more...
Understanding the alternate exchange extension
Getting ready
How to do it...
How it works...
There's more...
Understanding the validated user-ID extension
Getting ready
How to do it...
How it works...
See also
Notifying the consumers of queue failures
Getting ready
How to do it...
How it works...
There's more...
See also
Understanding the exchange-to-exchange extension
Getting ready
How to do it...
How it works...
Embedding message destinations within messages
Getting ready
How to do it....
How it works...
There's more...
3. Managing RabbitMQ
Introduction
Using vhosts
Getting ready
How to do it…
How it works…
Configuring users
Getting ready
How to do it…
How it works…
There's more…
User tags for the management plugin
Using SSL
Getting ready
How to do it…
How it works…
There's more…
Implementing client-side certificates
Getting ready
How to do it…
How it works…
Managing RabbitMQ from a browser
Getting ready
How to do it…
How it works…
There's more…
See also
Configuring RabbitMQ parameters
How to do it…
How it works…
There's more…
Developing Python applications to monitor RabbitMQ
Getting ready
How to do it…
How it works…
There's more…
See also
Developing your own web applications to monitor RabbitMQ
Getting ready
How to do it…
How it works…
There's more…
See also
4. Mixing Different Technologies
Introduction
Using a .NET client
Getting ready
How to do it…
How it works…
There's more…
See also
Binding an app from iPhone to RabbitMQ via MQTT
Getting ready
How to do it…
How it works…
There's more…
See also
Using messaging to update Google Maps on Android
Getting ready
How to do it…
How it works…
There's more…
Publishing messages from Android in the background
Getting ready…
How to do it…
How it works…
There's more…
Exchanging RabbitMQ messages with Qpid
Getting ready
How to do it…
How it works…
There's more…
Exchanging RabbitMQ messages with Mosquitto
Getting ready
How to do it…
How it works…
There's more…
See also
Binding a WCF application with .Net clients
Getting ready
How to do it…
How it works…
There's more…
See also
5. Using RabbitMQ in Web Applications
Introduction
Developing web monitoring applications with Spring
Getting ready
How to do it…
How it works…
There's more…
Developing asynchronous web searches with Spring
Getting ready
How to do it…
How it works…
There's more…
See also
Developing web monitoring applications with STOMP
Getting ready
How to do it…
How it works…
6. Developing Scalable Applications
Introduction
Creating a localhost cluster
Getting ready
How to do it…
How it works…
There's more…
Creating a simple cluster
Getting ready
How to do it…
How it works…
There's more…
See also
Adding a RabbitMQ cluster automatically
Getting ready
How to do it…
How it works…
Introducing a load balancer to consumers
Getting ready
How to do it…
How it works…
There's more…
See also
Creating clients of the cluster
Getting ready
How to do it…
How it works…
There's more…
See also
7. Developing High-availability Applications
Introduction
Mirroring queues
Getting ready
How to do it…
How it works…
There's more…
See also
Synchronizing queues
Getting ready
How to do it…
How it works…
There's more…
See also
Optimizing mirror policies
Getting ready
How to do it…
How it works…
There's more…
See also
Distributing messages between a couple of brokers
Getting ready
How to do it…
How it works…
There's more…
Creating a geographical cluster replication
Getting ready
How to do it…
How it works…
There's more…
See also
Filtering and forwarding messages
Getting ready
How to do it…
How it works…
There's more…
See also
Combining high-availability technologies together
Getting ready
How to do it…
How it works…
There's more…
See also
Client high availability
Getting ready
How to do it…
How it works…
8. Performance Tuning for RabbitMQ
Introduction
Multithreading and queues
Getting ready
How to do it…
How it works…
There's more…
System tuning
Getting ready
How to do it…
How it works…
There's more…
Improving bandwidth
Getting ready
How to do it…
How it works…
Prefetch
NoAck
There's more…
See also
Using different distribution tools
Getting ready
How to do it…
How it works…
There's more…
See also
9. Extending RabbitMQ Functionality
Introduction
Enabling and configuring the STOMP plugin
Getting ready
How to do it…
How it works…
There's more…
See also
Managing a RabbitMQ cluster
Getting ready
How to do it…
How it works…
Monitoring Shovel status
Getting ready
How to do it…
How it works…
Developing new plugins – attaching to a relational database with ODBC
Getting ready
How to do it…
How it works…
There's more…
10. RabbitMQ on AWS
Introduction
Using RabbitMQ EC2 instances
Getting ready
How to do it…
How it works…
There's more…
See also
Creating a master image
Getting ready
How to do it…
How it works…
See also
Creating a cluster with two EC2 instances
Getting ready
How to do it…
How it works…
There's more…
See also
Using AWS Load Balancing in front of a RabbitMQ cluster
Getting ready
How to do it…
How it works…
There's more…
See also
Configuring EC2 dynamic bind
Getting ready
How to do it…
How it works…
There's more…
See also
Dealing with load spikes and resource optimization in the cloud
Getting ready
How to do it…
How it works…
There's more…
See also
11. AMQP and Cloud Computing – RabbitMQ on PaaS
Introduction
RabbitMQ on CloudAMQP
Getting ready
How to do it...
How it works...
There's more...
First application on Cloud Foundry
Getting ready
How to do it...
How it works...
There's more...
Using RabbitMQ on Cloud Foundry
Getting ready
How to do it...
How it works...
There's more...
12. Managing RabbitMQ Error Conditions
Introduction
Monitoring RabbitMQ's behavior
Getting ready
How to do it...
How it works...
There's more...
Using RabbitMQ to troubleshoot itself
Getting ready
How to do it...
How it works...
There's more...
Tracing RabbitMQ's ongoing activity
Getting ready
How to do it...
How it works...
Debugging RabbitMQ's messages
Getting ready
How to do it...
How it works...
There's more...
See also
What to do when RabbitMQ fails to restart
Getting ready
How to do it...
How it works...
There's more...
See also
Debugging using Wireshark
Getting ready
How to do it...
How it works...
There's more…
See also
Index

RabbitMQ Cookbook

RabbitMQ Cookbook

Copyright © 2013 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

First published: December 2013

Production Reference: 1171213

Published by Packt Publishing Ltd.

Livery Place

35 Livery Street

Birmingham B3 2PB, UK.

ISBN 978-1-84951-650-1

www.packtpub.com

Cover Image by Sandeep Vaity (<[email protected]>)

Credits

Authors

Sigismondo Boschi

Gabriele Santomaggio

Reviewers

Lucas Hrabovsky

Jorge Puente Sarrín

Ken Taylor

Ignacio Colomina Torregrosa

Héctor Veiga

Acquisition Editors

Usha Iyer

Llewellyn Rozario

Lead Technical Editor

Chalini Snega Victor

Copy Editors

Alisha Aranha

Roshni Banerjee

Dipti Kapadia

Karuna Narayanan

Kirti Pai

Shambhavi Pai

Alfida Paiva

Laxmi Subramanian

Technical Editors

Shubhangi Dhamgaye

Novina Kewalramani

Pratik More

Suneeth Nair

Rohit Kumar Singh

Project Coordinator

Anugya Khurana

Proofreader

Linda Morris

Indexer

Tejal Soni

Graphics

Ronak Dhruv

Abhinash Sahu

Production Coordinator

Adonia Jones

Cover Work

Adonia Jones

About the Authors

Sigismondo Boschi is a software developer currently involved in projects of messaging and networking distributed applications.

Prior to this, he has had more than 10 years' experience working with distributed applications and message-passing paradigms. He first acquired a PhD in Computational Physical Chemistry from the University of Bologna, and then has worked in the Development of Scientific High Performance Computing Projects.

To my wife, Maria Luisa, and my children, Gaia and Marco.

Gabriele Santomaggio has worked in the IT industry for more than 15 years. He is a developer, and very keen on middleware and distributed applications. Currently, he is working on high-performance Java applications.

He is the member of a big IT Italian community (www.indigenidigitali.com/) where he has published some posts about Amazon Web Services and message-oriented middleware (http://blog.indigenidigitali.com/tag/gabriele-santomaggio/).

He likes running and listening to jazz music.

To my beautiful wife, Dia, and my little ninja turtle, Riccardo, with love.

About the Reviewers

Jorge Puente Sarrín is a Peruvian software developer, currently working at Red Científica Peruana (RCP) as a software architect designing service-based solutions. He is a passionate developer focused on building distributed systems solutions using asynchronous programming with Python and .NET. Also, he has been contributing towards the translation of documentation projects and online courses into Spanish. He is a proud member of Masters of MongoDB, a group of persons who are promoting MongoDB around the world.

Ken Taylor has worked in software development and technology for over 15 years. During the course of his career, he has worked as a systems analyst on multiple software projects in several industries as well as with the state government. He has used RabbitMQ for messaging on multiple projects as a way of scaling and as an integration point. He is a member and speaker of the 757 Ruby users group and the Hampton Roads .NET users group (HRNUG). Ken holds an A.S. in Computer Science from Paul D. Camp Community College and was awarded a U.S. Patent for a Real Estate financial software product. He is currently working at Outsite Networks Inc. in Norfolk, Virginia. He lives in Suffolk, Virginia with his lovely wife Lucia and his two sons, Kaide and Wyatt.

I would like to thank my wife for her support while writing this book and my sons for reminding me of the importance of being inquisitive. Thank you Packt Publishing for asking me to participate as a technical reviewer for this excellent resource on RabbitMQ.

Ignacio Colomina Torregrosa is a technical engineer in telecommunications and a master in free software. He works as a PHP/Symfony developer and has had experience using RabbitMQ as a tool to optimize and improve the performance of web applications that deal with large amounts of traffic.

Héctor Veiga is a software engineer specializing in real-time data integration. Recently, he has focused his work on different cloud technologies (AWS, Heroku, OpenShift, and so on) to develop scalable, resilient, and high-performing applications that are able to handle high-volume, real-time data in diverse protocols and formats. Additionally, he has a strong knowledge of messaging systems, such as RabbitMQ and AMQP. Also, Héctor has a Master's degree in Telecommunication Engineering from the Universidad Politécnica de Madrid and a Master's degree in Information Technology and Management from the Illinois Institute of Technology.

Héctor currently works at HERE as part of Global Data Integration team and is actively developing scalable applications to consume data from several different sources. HERE heavily utilizes RabbitMQ to address their messaging requirements. In the past, Héctor has worked at Xaptum Technologies, a company dedicated to M2M technologies.

I would like to thank my family and friends for their support. They showed me that giving up was not an option and that you just need to keep pushing. Also, I am greatly thankful for the opportunity I was given to live and work in a foreign country. Finally, I would like to acknowledge Steve Fosdal who has been my friend and RabbitMQ colleague during this journey.

www.PacktPub.com

Support files, eBooks, discount offers and more

You might want to visit www.PacktPub.com for support files and downloads related to your book.

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at <[email protected]> for more details.

At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.

http://PacktLib.PacktPub.com

Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can access, read and search across Packt's entire library of books. 

Why Subscribe?

Fully searchable across every book published by PacktCopy and paste, print and bookmark contentOn demand and accessible via web browser

Free Access for Packt account holders

If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine entirely free books. Simply use your login credentials for immediate access.

Preface

The RabbitMQ Cookbook covers RabbitMQ and the corresponding AMQP protocol.

Even the less-experienced programmer can find a lot of information that is useful to start developing messaging applications using RabbitMQ, especially in the first few chapters.

However, in order to fully appreciate some of the chapters of the book, the reader should also know the basic concepts about web and cloud applications.

The book is full of examples that show you how to use RabbitMQ in very disparate scenarios, in different technologies, right from single hosts to geographically replicated high-availability clusters. Mostly everyone working with these technologies will find a lot of useful information within this book.

What this book covers

Chapter 1, Working with AMQP, introduces the basic concepts of AMQP, the Advanced Message Queuing Protocol, on which RabbitMQ is based. It shows how the fire-and-forget messaging model works and how to use it from clients.

Chapter 2, Going beyond the AMQP Standard, covers the RabbitMQ extensions of AMQP and how they can be used to increase the efficiency of messaging applications.

Chapter 3, Managing RabbitMQ, explains how to configure RabbitMQ parameters, enable RabbitMQ plugins, and monitor RabbitMQ activities.

Chapter 4, Mixing Different Technologies, covers the integration of different technologies with other protocols, such as MQTT, STOMP, and JSON.

Chapter 5, Using RabbitMQ in Web Applications, shows how to develop web applications with RabbitMQ as a service bus.

Chapter 6, Developing Scalable Applications, explains how to create scalable and robust applications using RabbitMQ clustering.

Chapter 7, Developing High-availability Applications, covers the high-availability options offered by RabbitMQ.

Chapter 8, Performance Tuning for RabbitMQ, explains how to optimize the performance of applications based on RabbitMQ in different scenarios.

Chapter 9, Extending RabbitMQ Functionality, covers the use and development of RabbitMQ plugins.

Chapter 10, RabbitMQ on AWS, explains how to deploy RabbitMQ applications on Amazon Web Services.

Chapter 11, AMQP and Cloud Computing – RabbitMQ on PaaS, explains how to deploy and use RabbitMQ on Cloud Foundry.

Chapter 12, Managing RabbitMQ Error Conditions, explains how to diagnose and deal with RabbitMQ error conditions.

What you need for this book

You can use Linux/Unix, Mac OS X, or Windows as the operating system since RabbitMQ is cross-platform middleware. Most of the examples have been developed in Java, but to execute other recipes, you will also need:

PythonRuby.NET (here you will be bound to Windows only)ErlangObjective-CJavaScript

The RabbitMQ Cookbook also covers the cloud computing world (both IaaS and PaaS). To utilize the respective chapters, you will need an account on:

Amazon Web Services (IaaS)Cloud Foundry (PaaS)

Who this book is for

The RabbitMQ Cookbook is for software developers who want to develop distributed applications based on messaging. It’s assumed that the reader has some experience with multithreaded applications and distributed applications.

The reader should also know the basic concepts of web and cloud applications in order to follow the recipes specifically.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to <[email protected]>, and mention the book title via the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at <[email protected]> with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at <[email protected]> if you are having a problem with any aspect of the book, and we will do our best to address it.

Chapter 1. Working with AMQP

In this chapter we will cover:

Connecting to a brokerProducing messagesConsuming messagesUsing body serialization with JSONUsing RPC with messagingBroadcasting messagesWorking with message routing using direct exchangesWorking with message routing using topic exchangesGuaranteeing message processingDistributing messages to many consumersUsing message propertiesMessaging with transactionsHandling unroutable messages

Introduction

Advanced Message Queuing Protocol (AMQP) has been developed because of the need for interoperability among the many different messaging solutions, that were developed a few years ago by many different vendors such as IBM MQ-Series, TIBCO, or Microsoft MSMQ.

The AMQP 0-9-1 standard gives a complete specification of the protocol, particularly regarding:

The API interfaceThe wire protocol

RabbitMQ is a free and complete AMQP broker implementation. It implements version 0-9-1 of the AMQP specification; this is the most widespread version today and it is the last version that focuses on the client API. That's what we want to put the focus on, especially in this chapter.

On the other hand, AMQP 1.0 only defines the evolution of the wire-level protocol—the format of the data being passed at the application level—for the exchange of messages between two endpoints; so 0-9-1 is actually the most updated client library specification.

RabbitMQ includes:

The broker itself, that is, the service that will actually handle the messages that are going to be sent and received by the applicationsThe API implementations for Java, C#, and Erlang languages

It is also possible to use APIs for languages downloadable from the RabbitMQ site itself, from third-party sites, or even using AMQP APIs not strictly related to RabbitMQ (http://www.rabbitmq.com/devtools.html). Since the AMQP standard specifies the wire protocol, they are going to be mostly interoperable, except for some custom extensions. That will be discussed in detail in the next chapter.

In the course of the book we will particularly use some of the following APIs:

The Java AMQP client library (http://www.rabbitmq.com/java-client.html)Pika, one of the Python AMQP client libraries (http://pypi.python.org/pypi/pika)The .NET/C# AMQP client (http://www.rabbitmq.com/dotnet.html)The RabbitMQ C client API (https://github.com/alanxz/rabbitmq-c)The Ruby client library (https://github.com/ruby-amqp/bunny)

In this first chapter we are mainly using Java since this language is widely used in enterprise software development, integration, and distribution. RabbitMQ is a perfect fit in this environment.

In order to run the examples in this recipe, you will first need to:

Install Java JDK 1.6+Install the Java RabbitMQ client libraryProperly configure CLASSPATH and your preferred development environment (Eclipse, NetBeans, and so on)Install the RabbitMQ server on a machine (this can be the same local machine)

The natural choice is to install it on your desktop (Windows, Linux, and Mac OS X are all fine choices), but you can also install it on one or more external servers; for example, virtual machines, physical servers, and Raspberry PI servers (http://www.raspberrypi.org/) on cloud service providers.

Tip

In this book we are not providing instructions on the installation of RabbitMQ itself. You can find detailed instructions on the RabbitMQ site.

Most of the examples will work connecting to the RabbitMQ broker running on the localhost. If you have chosen to install or use RabbitMQ from a different machine, you will need to specify its hostname as a command-line parameter of the examples themselves, for example:

java -cp ./bin rmqexample.Publish [Rabbitmq-host]

For the examples involving Python, you will need Python 2.7+ installed and the Pika library, an AMQP implementation for Python (https://pypi.python.org/pypi/pika). The fastest way to install Pika is by using PIP (https://pypi.python.org/pypi/pip). In the command prompt, just type:

pip install pika

We will also present some recipes using .NET where the accent is mainly on interoperability.

You can download the working examples in their full form at http://www.packtpub.com/support.

The recipes presented in this chapter will tackle all the basic concepts exposed by AMQP, using RabbitMQ.