An example java maven project with code examples is posted here.
Have you ever encountered a problem that involved messaging between 2 or more applications that don’t occupy the same datacenter or network? Enterprise IT organizations usually have many solutions to this problem. Sometimes it’s an FTP server that distributes update files to a group of “subscribers”. Or it might be a specialized web service and a set of networking rules that allow for the subscribers to tunnel through the firewall. These solutions show their warts when it’s time to add a new subscriber. Sometimes it will involve opening new firewall holes or adding additional domains to the allowed list. Or it may require software updates. Lastly, these types of solutions rarely give much flexibility to the subscribers. Often one consumer of your data will only need specific items. Cloud computing is not only for hosting servers and data, it can also act as a data bus enabling messaging between applications running in disparate datacenters and written in various languages.
In my last post I introduced the new Azure SDK for java and used it in a Maven project. This time around I want to introduce Azure Service Bus Topics within the same context; Java, Maven and Azure. Topics are similar to queues but they are significantly more powerful. They are perfect for broadcasting messages to a group of subscribers. The Azure Service Bus is also easy to use from most modern computer languages, and client libraries are available for java, PHP, .net, and others. This makes it ideal for integration challenges that involve apps written in two or more different languages.
Let’s take a look at Azure Service Bus Topics in the context of a hypothetical problem. Pretend you are the software architect for a large travel IT company that collects and distributes hotel reviews. You have one group of content providers that share hotel reviews. These might be sub-brands or affiliated web sites that opt into your review database provided they share the reviews users submit on their site with you. You also have a larger group of content subscribers. This will include the group of review providers as well as hotels that are interested in what consumers are saying about them. Here is a quick diagram you’ve drawn to explain the problem to your team:

Hotel Reviews Integration Project Sketch
In the center you’ve drawn an info bus, some technology that allows posting of new reviews as well as a way for interested parties to watch for and receive the new ones as they are posted. Two hypothetical partners, hotelreview.com and mystays.com act as content suppliers to your info bus. Your company is also interested in all the reviews and stores them immediately in its review database. You’ve also included one of your hotel partners, Hyatt, who is only interested in seeing reviews for their brand. Furthermore, they are primarily interested in the bad ones, so that they can react to any customer satisfaction issues as soon as the review is posted. You’re imagining some kind of filter feature, so that Hyatt can ask for reviews with a rating < 4 and having their hotel code. Finally, it’s important to note that while you prefer java, many of your partners are using PHP or other languages.
After discussing the problem with your team, your senior developer proposes a solution. It involves several web services and the deployment of an enterprise services bus. He estimates 3 developers and a systems engineer to write network rules to poke holes in their firewall for specific partners. Although load won’t be great he points out you’ll need at least two powerful servers to account for failover, as the solution must be live and receiving reviews 24/7. All in all he estimates about a 6 month effort. Then another team member chimes in, mentioning that your diagram looks like a pub/sub topic, that Windows Azure has a cloud service that meets all of the requirements, and more specifically points out that:
- Azure Service Bus supports the creation of “Topics” that can be subscribed to, and even filtered down using SQL like rules.
- Interaction with the service bus topics is via an API, with existing SDKs for java, node.js, PHP, and of course C# and many other languages.
- Interaction with the service bus is via HTTPS, so all partners will be able to publish and subscribe without needing firewall holes added.
- The effort on your side will consist of provisioning a new service bus in your windows azure console, sharing authentication information with your partners, and writing a subscriber to the queue that populates your review DB as new items come in; about a 2 day effort.
The next day that same developer emails a set of code examples that demonstrate how you can publish and subscribe to the azure service bus “reviews” topic. The first example shows how to create the topic using java:
Configuration c = ServiceBusConfiguration.configureWithWrapAuthentication(
"namespace", "yourid", "yourpassword");
ServiceBusContract servicebus = ServiceBusService.create(c);
TopicInfo topic = new TopicInfo("hotelreviews");
servicebus.createTopic(topic);
After running that code, there would be a public REST service located at https://hotelreviews-sb.accesscontrol.windows.net . This is the magic of the Azure Service Bus. It’s a RESTful service api that you have provisioned yourself, using the power of Windows Azure and the management consol. The next example provided in the email shows how each review provider partner would publish new reviews to the topic:
BrokeredMessage message = new BrokeredMessage(hotelReviewXML);
Review review = new Review();
message.setProperty("hotelcode", review.code);
message.setProperty("rating", review.rating);
servicebus.sendTopicMessage("hotelreviews", message);
The last example is the most interesting he sent. It demonstrates how a particular hotel can subscribe to the topic and only receive the reviews for their chain:
SubscriptionInfo subscription =
new SubscriptionInfo("hyattsubscription");
RuleDescription rd = new RuleDescription();
SqlFilter f = new SqlFilter();
f.setSqlExpression("rating < 4");
f.setCompatibilityLevel(20);
rd.setFilter(f);
subscription.setDefaultRuleDescription(rd);
servicebus.createSubscription("hotelreviews", subscription);
servicebus.receiveSubscriptionMessage("hotelreviews", subscription.getName());
With about 15 lines of code and a windows azure service bus endpoint we’ve solved the requirements of our hotel review scenario. Instead of heading off on our own and developing a solution from scratch, or cobbling a solution together with FTP servers and shell scripts, we solved the problem with a cloud computing service. So you’re thinking, how much does it cost? Let’s say we get 10 thousand new reviews a week, with a total of 10 actors either producing post transactions or pull transactions on the topic. That makes about 100 thousand transactions a week, for a grand total of…drum role…$0.10 (ten cents). Windows Azure Service Bus transactions are billed at 1 cent per 10k transactions. For more information and concise instructions on how to setup a Windows Azure Service Bus please visit the Windows Azure portal .