Windows Azure and Java: Service Bus Hands on Lab

Sometimes all you want is a small example and you’re off and running.  Other times it’s nice to have a more comprehesive look at how a new technology lays out, with running examples and a few actors.  servicebuslab4java contains two Eclipse projects, one fully completed, another only partially complete.  It also comes with a guide that walks you through the steps to create a topic, produce messages on that topic, and then consume the messages on another process.  To run and enjoy the lab you’ll need to have Eclipse of course, and the Maven integration for Eclipse plugin.

Download the hands on lab here.

Posted in java, USCloud, Windows Azure | Tagged , , , , , | Leave a comment

Windows Azure and Java: Creating a Maven Project with Eclipse

Visit the Windows Azure java dev center

More Technical Documentation on the Java SDK for Azure

Posted in java, USCloud, Windows Azure | Tagged , , , , , , | Leave a comment

Windows Azure and Java: Service Bus Topics

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 Reivew integration diagram

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:

  1. Azure Service Bus supports the creation of “Topics” that can be subscribed to, and even filtered down using SQL like rules.
  2. 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.
  3. Interaction with the service bus is via HTTPS, so all partners will be able to publish and subscribe without needing firewall holes added.
  4. 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 .

Posted in java, USCloud, Windows Azure | Tagged , , , , | Leave a comment

Windows Azure and Java: Queues

The new Azure SDK for Java allows java developers API access to Azure’s storage platform.  In this post we’ll be looking at the queue service.  Before we dive in to the mechanics let’s look into why a developer would need and use Azure Queues.

  1. Queues separate software components, allowing consumers and producers to scale independently.
  2. Azure Queues can be used from within the cloud, or from outside the cloud.  For example, you might use an azure queue to co-ordinate work between two or more applications residing in different locations around the world, or you may use a queue within your cloud applications to offload work from your web tier.
  3. Items placed on the queue are durable, and remain until deleted.
  4. Under the hood the azure queue service is a RESTfull api that conforms to the Atom protocol.  Azure queus are therefore open to not only java, but many languages.  If you like you can code directly to the REST api, however, I prefer the SDK as it’s much easier to get started with.

Step 1: open and azure account

This is now easier than it has ever been.  If you haven’t already done so sign up for a free 3 month trial here.  The trail includes 750 small compute hours per month, enough to run an app 24/7 on one instance.  You’ll also get 50,000 storage transactions free each month of the trial.  The demo we’ll do today will consume about 5 each time you run it.

Step 2: create a maven project and declare a dependency on Azure

The Java SDK for Azure is now hosted as a Maven POM.  This makes it very easy to add the Azure SDK as well as all of it’s dependencies.

<dependency>
  <groupId>com.microsoft.windowsazure</groupId>
  <artifactId>microsoft-windowsazure-api</artifactId>
  <version>0.1.0</version>
</dependency>

This doesn’t rule out traditional java project builds. If you like you may download all the jars individually from this page.  That said I highly recommend you invest some time in learning maven if you haven’t already done so.

Step 3: Use the SDK to enqueue and dequeue a message

Here’s a full example, represented as a junit test case, that connects to an azure cloud account, creates a queue if it doesn’t already exist, pushes one message on the queue, and retrieves that same message.  The only change you’ll need to make is to replace “youraccount” and “yourkey” with your own Azure Storage account settings.

public class Connect {

   public static final String storageConnectionString = "DefaultEndpointsProtocol=http;"
         + "AccountName=youraccount;"
         + "AccountKey=yourkey";

   @Test
   public void test() throws InvalidKeyException, URISyntaxException {
      CloudStorageAccount acct = CloudStorageAccount
            .parse(storageConnectionString);
      CloudQueueClient client = acct.createCloudQueueClient();
      try {
         CloudQueue queue = client.getQueueReference("example");
         queue.createIfNotExist();
         String msgStr = "example message";
         CloudQueueMessage message =
            new CloudQueueMessage(msgStr.getBytes());
         queue.addMessage(message);

         CloudQueueMessage msg = queue.retrieveMessage();
         System.out.println(msg.getMessageContentAsString());
         queue.deleteMessage(msg);
      } catch (StorageException e) {
         e.printStackTrace();
      }
   }
}

Learning more about azure queues

Inserting and removing a queue messages is the most basic way to use an Azure queue.  Azure queues can be used from an app running within or outside the azure cloud.  Typically Azure queues are used to govern the activities of a group of consumers or workers and ensures that each individual work item is only handled once.  They may not seem interesting, but are the backbone of any well crafted cloud architecture.  To read more about programming Azure queues from java see “How to use the queue storage service from java”

Posted in java, USCloud, Windows Azure | Tagged , , , | 1 Comment

Create a simple leaderboard with Azure Storage

If you are engaged in mobile application development you’ve most likely spent quite a bit of time thinking about services.  How can I provide interesting data to users of my app?  How can I save and share data between users?  Traditional solutions involve finding a hosting provider, standing up a host and writing services.  With cloud computing the barrier to entry is lower, however in many cases you still need to code the services layer.  But in some unique cases, it may be possible to solve your service problem without writing a single line of server side code.  In this example I’ll be using Windows Azure Storage and the Windows Azure toolkit for WP7 to create a leader board (a way to store and retrieve the highest scores).  I’ll be focusing on the basics, and allow you to add additional columns as needed.  For our leaderboard we’ll need two basic operations:

  1. Ability to save a score to the server
  2. Ability to request the top 10, 20, … scores

You may be surprised to find out that this can be accomplished via windows azure storage.  The example WP7 project code may be downloaded here.

 Prerequisits

You’ll need the following tools to run the project:

  1. Windows Phone development kit (http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=27570)
  2. Windows Azure Toolkit for WP7
  3. A Windows Azure Account with one storage service.  You can sign up for a free 90 day trial here http://www.microsoft.com/windowsazure/free-trial/

Step 1: define a domain class for the data

Our domain model will store the score and the alias of the gamer.  The domain model will also need to provide a partition key and row key as required by Azure Tables.  We’ll be taking advantage of the fact that partition keys are kept in indexed order from least to greatest.


public class Score : TableServiceEntity
{
private long p;
public Score() {}
public Score(long p)
{
this.p = p;
Value = p;
PartitionKey = (long.MaxValue - Value).ToString("d19");
}
public string Alias { get; set; }
public long Value { get; set; }
public override string ToString()
{
return Value + " (" + Alias + ")";
}
}

The partition key and row key are inherited from the TableServiceEntity class (part of the toolkit).  Did you notice how the partition key is derived?  We are taking the score and subtracting it from the maximum long value.  This ensures that the highest scores will have the smallest partition key.  This way our azure storage account acts as a priority queue.  When we request the scores, they’ll always return the highest scores, sorted in highest to lowest order.

Step 2: create a simple UX to proof the concept

Well need a button to submit random scores and provision the table with data.  We’ll also need a button to request the top 10 scores.  Finally we’ll need a list box to display the leaderboard.  Here’s the completed UX.  See the project code for details.

Step 3: code operations to persist and query scores

Lets start with the simpler query case.  Pulling data from Azure tables is very simple, and this is particularly the case when we’re using the WP7 toolkit as it comes with a handy C# client api. (Azure tables speak simple OData/Atom/Rest so you could role your own client).  In the following code you’ll need to replace “yourservice” with your azure storage account name.  Similarly you’ll need to supply your own account credentials.  We’ll discuss security and securing your account credentials in a later post.  Much of the example is building a URI…this is standard REST stuff.  The base URI will be your account, taken from the context object, plus the table name of “scores”, and finaly we indicate the number of items to return (10 in this case).  So in 14 lines of code we have a top scores service.

TableServiceContext ctx = new TableServiceContext("https://yourservice.table.core.windows.net", creds);
CloudTableClient c = new CloudTableClient(ctx);
table = new DataServiceCollection(ctx);
table.LoadCompleted += this.OnTableLoaded;
var tableUri = new Uri(
string.Format(
CultureInfo.InvariantCulture,
"{0}{1}?incrementalSeed={2}&$top=10",
ctx.BaseUri,
"scores",
DateTime.UtcNow.Ticks),
UriKind.Absolute);
table.Clear();
table.LoadAsync(tableUri);

Now that we’ve solved retrieving the scores, we need to look at saving the scores. This example merely generates a random score between 0-1000. It also generates a GUID for the row key, to ensure that ties (scores of the same value) won’t conflict, that is, we cannot simply use the score as the primary key. Similarly, the alias are chosen at random. Scores make an ideal partition key as they’ll be distributed fairly well. As above, you’ll need to change “yourservice” to your own storage service name.

TableServiceContext ctx = new TableServiceContext("https://yourservice.table.core.windows.net/", creds);
string[] aliases = {"tacowan", "dizzer", "thedude", "puzzlemaster", "wp7owner"};
int rnd = random.Next(1000);
Score score = new Score(rnd);
score.RowKey = Guid.NewGuid().ToString();
score.Alias = aliases[rnd % 5];
ctx.AddObject("scores", score);
ctx.BeginSaveChanges(asyncResult =>
{try { ctx.EndSaveChanges(asyncResult);}

I recommend downloading my example code, provide your own account key and name where necessary and run the example. Store a score or two then retrieve the results. You’ll see the scores readjust as new leaders are added. Hopefully you’ll think of even better ways to use Azure Storage. The value in this approach is that we are not utilizing and compute power or incuring any additional costs to run a server. Plus it’s simple, fast, and can service nearly unlimited demand as your application becomes more popular. You’ll never have to manage a server, or add new hosts, because this is in cloud storage.

Posted in Uncategorized, USCloud | Tagged | 1 Comment

Nuts About Southwest and IE9

Nuts about southwest blog pinned to my taskbar

Nuts about southwest blog pinned to my taskbar

Go see how Nuts About Southwest blog now integrates with IE9.  You can keep news and info from Southwest Airlines just a click away by “pinning” it to your taskbar.  Jump to major sections of the site such as videos and podcasts using the tasks menu.   You can also see the latest posts in the always updated dynamic menu.

Posted in Uncategorized | Leave a comment

Spanning Panorama Conent Across Two or More Screens

Many of the standard panorama views that come with windows phone 7  such as the XBox and Office hubs have content regions that span more than 1 screen width horizontally.  If you want to do this you’ll be pleased to learn that it is very easy.  Let’s begin by creating a new panorama app using the Visual Studio template.

Creating a new panorama project

Creating a new panorama project

The default panorama has three items, and we’re going to use the second item as a starting point.  Here’s what we’ll be starting with, and notice that the comment for the item already has a hint!

So the first fix we’ll make is to modify the orientation of the panorama item to “Horizontal”.

<controls:PanoramaItem Header=”second item” Orientation=”Horizontal” Width=”800″>

We now have a panorama item that spans 800 pixels horizontally.  If you run the app you’ll have a vertical list on the first page, and an empty screen on the second, but you’ll also be able to see the item span more than one screen.  Next we need to replace all instances of the the string “ListBox” with “ItemControl”.  An ItemControl is similar in it’s data binding, but will act as a better container as we’ll not be needed scrolling.   Here’s a tip that might not have been as obvious as the orientation setting.  Items in this type of horizontal multi-screen are typically laid out left to right and top to bottom.  The best containment for this is a WrapPanel.  To have our ItemControl use a wrap panel we simpl7 need to add this XAML:

<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<toolkit:WrapPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>

And we’ll also need to arrange the items to suite the new layout.  For this we’ll leave the rectangle’s and remove one line of text, and narrow the items a bit to take up less horizontal space:

<DataTemplate>
<StackPanel Orientation=”Horizontal” Margin=”0,0,0,17″>
<!–Replace rectangle with image–>
<StackPanel Width=”200″>
<Rectangle Height=”100″ Width=”100″ Fill=”#FFE5001b” Margin=”0,0,0,0″/>
<TextBlock Text=”{Binding LineOne}” TextWrapping=”Wrap”  HorizontalAlignment=”Center”/>
</StackPanel>
</StackPanel>
</DataTemplate>

That’s it!  Here’s what the new layout looks like in the XAML editor:

And here’s the completed code for the 2nd panorama item:

<controls:PanoramaItem Header="second item" Orientation="Horizontal" Width="800">
<!--Double line list with image placeholder and text wrapping-->
<ItemsControl Margin="0,0,-12,0" ItemsSource="{Binding Items}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<toolkit:WrapPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>

<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="0,0,0,17">
<!--Replace rectangle with image-->
<StackPanel Width="200">
<Rectangle Height="100" Width="100" Fill="#FFE5001b" Margin="0,0,0,0"/>
<TextBlock Text="{Binding LineOne}" TextWrapping="Wrap"  HorizontalAlignment="Center"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</controls:PanoramaItem>
</controls:Panorama>

When you run the app you’ll see that it’s necessary to put a limiter on the data so that you get 2 symmetrical rows, but other than that you’re ready to create a panorama app just as sophisticated as the ones you find already installed on the phone or in the marketplace.

Posted in dotnet, Windows Phone 7 | Tagged , , , | 3 Comments

Windows Phone 7 Panorama Design with GIMP

First you’ll need to download the Windows Phone 7 design templates. More information can be found in Windows Phone 7 Design Resources – UI Guide and Design Templates (http://is.gd/fAliT). The templates are zipped, when you extract the files make sure you place them into a directory, as the contents are all at the root of the archive. I placed mine into a directory called “wp7 templates”.
Once you have extracted the templates, within GIMP (2.x or later) open the file “PanoramaBG_PSD.psd”. I like how they make sure we realize it’s a PSD by typing it redundantly twice in a row…so just in case you missed it, the file is in PSD format, which is supported by GIMP enough for us to use the templates to create a simple panorama design. Within the template we are interested in three of the layers; Layer 6, Shadow, and PSD. I don’t have Adobe or another advanced design tool, I can only relay the names I see in the layer tool. “Layer 6” is the most important as it holds the image of the phone, with a transparent region to allow our panorama to show through. “Shadow” has a nice shadow effect to give the design depth. It’s just a shadow that will sit directly below the phone. “PSD” is an example panorama. Its only importance is its size and its usefulness as an example of what a good panorama might look like.

design template in GIMP

panorama design template in GIMP

Create a new image size 1800×950. This’ll give us room for the panorama, the phone shell, and its shadow. Focus the template image, then in the layers dialog just drag the phone shell and its shadow into your new image, it should look like this:

phone shell template

phone shell template

The panorama size in the original template is 1788×625. The width will depend on how wide you’d like your panorama design to be, but we need to stick with the original size to fit nicely with the phone/device image we’ve already placed onto the work area. Create a new image, and apply any background you like. Very dark or very light backgrounds work best, as we’ll be layering text on top. Typically the background is a photo, but it might just be a solid color…the only advice I have there is that you must be able to read the textual menu items. For text we’ll be using the Segoe WP font set, which by the way came with the WP7 design template download. You may already have it installed on your system if you’ve used the WP7 developer tools. Here’s a simple example…for your idea you’d want a nice themed background, and real images, as well as menu items that are related to your planned application.

example panorma wireframe

example panorma wireframe

The last step is to take your panorama and combine it back with the phone template. I like to move the phone template over two or three of the pages and save as png or jpg’s. These work great in presentations to show customers how the app might look on the phone.

phone shell overlay over panorama

phone shell overlay over panorama

Posted in Uncategorized | Tagged | 1 Comment

neo4j on dotnet

I recently ran across a project called ja.net that provides support for compilation of Java to .Net. Since neo4j has such a dependency clean kernel, I decided to try ja.net as a way of making neo4j usefull under .Net. (no slight against the RESTful interfaces, but perhaps you’d like to run neo4j on .net as an embedded graph DB under .Net). Here’s my VS2010 with the neo4j.dll referenced, and the object browser showing the EmbeddedGraphDB class as a first class .Net object:

Continue reading

Posted in dotnet, neo4j | 1 Comment

Running Jena in Windows Azure: Part 2

In the the first half of this discussion I introduced a Visual Studio Cloud project that configures and runs Tomcat.  In this post I’d like to talk specifically about Jena and provide a few tips on how to get started. Continue reading

Posted in Windows Azure | Tagged , , | 2 Comments