Mixing RabbitMQ with Spring Python

Engineering | Greg L. Turnquist | September 20, 2010 | ...

RabbitMQ is a powerful messaging broker based on the Advanced Message Queueing Protocol (AMQP). In an earlier post, we looked into building a Python stock ticker program. We compared using RabbitMQ's pika with py-amqplib, and how it was easy to transition from one to the other with minimal changes.

In this article, we'll show how pika can easily be used by Spring Python. Spring Python is an implementation of the concepts of Spring with the language of Python. It includes many features such as dependency injection. If we choose to build a Spring-like application in Python, it is easy to utilize pika.

At the heart of any Spring application is a set of blue prints or application context. This is the same for Spring Python as for Spring Java.

As I'm sure you've noticed, this context isn't written in XML. Instead it is coded with pure Python. This is very similar to Spring Java Config (which has since been rolled into the core Spring Framework). This defines both a publisher and a subscriber.
  • They are both configured with lazy initialization so we can use one context to support two startup scripts. To create a stock ticker, we need to create the same ticker app, only use the Spring Python container.
  • They are both configured with scope.PROTOTYPE, in the event we wanted to grab more than one copy for a given situation.
We also need a script to fire up a brokerage house that subscribes for these messages, also using Spring Python's container.

The following image shows two terminal windows. The background window shows the ticker application running, spitting out stock quotes. If you look closely, you will notice the last parameter is a message identifying id of 290. The foreground window shows the brokerage house subscribing for stock quotes. It has just received message 290 and then printed out its current net worth.

[caption id="attachment_5659" align="alignnone" width="784" caption="Ticker and brokerage house running inside Spring Python"][/caption]

Because we used Spring Python to inject pika-based RabbitMQ clients, we can easily switch these out for other clients. As pointed out in the previous blog entry, this helps us avoid lockin, and instead choose the solution based on what's important: technical merit, vendor support, and business needs. Pika has shown itself to be an active project with support from the same team that develops the RabbitMQ broker. Migrating to it from py-amqplib is easy and supports whatever tools we are using, including Spring Python as we have just seen. And it will be easy to migrate to another library in the future should the need arise.

Get the Spring newsletter

Stay connected with the Spring newsletter

Subscribe

Get ahead

VMware offers training and certification to turbo-charge your progress.

Learn more

Get support

Tanzu Spring offers support and binaries for OpenJDK™, Spring, and Apache Tomcat® in one simple subscription.

Learn more

Upcoming events

Check out all the upcoming events in the Spring community.

View all