Geeks With Blogs

News View Michael Stephenson's profile on BizTalk Blog Doc View Michael Stephenson's profile on LinkedIn
Michael Stephenson keeping your feet on premise while your heads in the cloud

Following on from previous samples I wanted to talk about how to implement a scatter gather pattern using the AppFx.ServiceBus framework. In this sample we want to achieve the following:

  1. Send a message from a client application to a Service Bus topic
  2. Use the topic filters to route this client to two subscriptions
  3. Have a service listening to each subscription which will handle the message and return a response
  4. Have the client wait for multiple responses to come back and return them as a collection.

Hopefully that is straight forward so let's walk through this sample.

Accessing the Sample

On the codeplex site you can download the samples. In the samples folder you need to go to the folder: Sample5-ScatterGather

This folder contains all of the code for the scatter gather sample.

 

Configuring the Azure Service Bus

To begin with we will need to create the following in the Windows Azure Service Bus

  • A response queue which response messages back to the client will be published to. This queue will need to have session enabled
  • A topic which the client will publish messages to
  • 2 x subscriptions, 1 for each client
  • A rule for each subscription using 1=1 so that the message is routed to all subscriptions

The response queue will be called: sample5-scattergather-response

The topic will be called: sample5-scattergather

The subscriptions will be called:

  1. sample5-scattergather/Subscriptions/server1
  2. sample5-scattergather/Subscriptions/server2

 

Note that in the samples folder there is an xml file containing the setup for all queues and topics used in these samples which you can import into your namespace using the Service Bus Explorer tool.

 

The message contract

The message contract follows the usual standard from previous samples. We will use an xsd schema to define out messages and then generate classes to represent these. In the sample the client and server will use this object but as data goes over the wire so to speak it will be in a serialized format which can be either xml or JSON depending on what your specify.

The contract looks like the below picture. You can see that again we are using the get customer sample message.

 

Configuring the Client

Moving on to the client where we have a simple windows application we will need to begin by setting up the configuration. In the same way as previously we will use the AppFx.ServiceBus configuration section. Firstly I will add my connection string for my service bus namespace like in the below picture.

After the section is declared and also the connection string I will add my AppFx.ServiceBus section and configure it to talk to the Topic in the Windows Azure Service Bus. See the below picture:

You can see that above I have added my client and specified the queue/topic to send the message to and also my response queue. Ive also specified my serialization format.

 

Client Side Code

Next in the client side code I need to use the SessionScatterGatherMessagingClient class. When I have constructed this class I have used Generics to tell it what type of messages I will be dealing with. I have also specified a client name so it knows what configuration to pick up from the config file (note you cant quite see that in the pic).

 

 

When I come to send the message there are a couple of key settings to be aware of. Firstly in the constructor you can override the default timeout if you want to. You can also in the SendScatterGather method specify the minimum number of responses you want to get back. This means that the messaging client class will wait for 30 seconds for responses to come through but if the response queue is empty before 30 seconds but the messaging client has already got your minimum number of responses back then it will return to the caller with what it has regardless.

Hopefully you can see that the client code is pretty simple.

 

Configuring the Server

On the server side we will take a similar approach to the previous samples. In the ..\Library folder (in relation to the solution file) there is three folders. One has the dll's for the client side of the framework which the client will reference, but the other two contain instances of the AppFx.ServiceBus console host application which will be used to listen on the service Bus.

In the server project in Visual Studio we have two configuration files:

  1. Server1.AppFx.ServiceBus.Hosts.Console.exe
  2. Server2.AppFx.ServiceBus.Hosts.Console.exe

These files contain the configuration for each of the hosts to connect to the service bus. The only real difference between then is the message entity they will listen to. Each host will listen on a different subscription. An example of this is below:

During the build process for this project I have modified the build using the OverrideBuild.targets file in the project so that these two configuration files and the server and contracts assemblies are copied to the right server folders so they can be picked up by each host. If you compile the projects and then check these folders you will see they are copied over.

 

The server Message Handler

The message handler for each server instance is exactly the same. It will simply return a response. An example of the code is below:

 

 

 

Running the sample

To run the sample, click F5 and the client application will open up. You will also need to go into the ..\library\AppFx.ServiceBus.Hosts.Console…. Folders and run the AppFx.ServiceBus.Hosts.Console.exe file. This will start up the servers listening on their subscriptions.

When you click send you should get a response shortly afterwards displaying that you have responses like the below:

 

You should also be able to check both server console windows and see that they have processed messages like the below:

If you look carefully at the text in the console window you will see that the message was received and handled by the message handler. You should see the same in both windows.

 

What about Errors?

It is possible that one or more of the listening services could return an error. Using the AppFx.ServiceBus framework it you have a pattern available to help you deal with this scenario. If any servers return an error then this is returned in the same way that errors were returned in the RPC samples. The key difference is that using the Scatter Gather messaging client class it will catch these errors and return them to you at the end of the call in the response object. You get the reply which contains a list of good responses and a list of error responses.

The calling client can then deal with these however it needs to.

 

Summary

Hopefully you can see this walk through makes it nice and easy to implement the scatter gather messaging pattern. I think in a future release we will also include another option with the scatter gather pattern so that the message client will raise an event each time an error or success response comes back as I can also see scenarios where that will be useful too.

Posted on Saturday, February 16, 2013 12:13 PM Azure Service Bus | Back to top


Comments on this post: AppFx.ServiceBus – Scatter Gather

# re: AppFx.ServiceBus – Scatter Gather
Requesting Gravatar...
good a few of your favourite tunes as well as Vidmate APK You will not be disappointed, in the event you are a music buff. Vidmate Free Download great.
Left by nims on Mar 01, 2016 11:17 PM

# re: AppFx.ServiceBus – Scatter Gather
Requesting Gravatar...
awsome game charts and the strategy for nearly annually. Clash of Clans on PC Locate Clash of Clans in all programs in BlueStacks, great.
Left by ram23 on Mar 02, 2016 10:43 PM

# re: AppFx.ServiceBus – Scatter Gather
Requesting Gravatar...
That's really cool. I would be interested in seeing more graphs of different information you pull from these logs.

fifa16hacked.com
Left by dw on Sep 09, 2016 10:31 AM

# re: AppFx.ServiceBus – Scatter Gather
Requesting Gravatar...
With the increasingly important increase of cybercrime, surfing the web returns in a sense to carry out a permanent technological watch. Threats are therefore important and the risk of piracy is still great. Fraud relating to piracy of the bank card has exploded in recent years, due to the increase in electronic commerce. Researchers at the University of Newcastle, UK, went through a small exercise to find the number of a blue card and its cryptogram.
Micro Machines

Left by fwdQaxcaef on Dec 12, 2016 2:07 AM

# re: AppFx.ServiceBus – Scatter Gather
Requesting Gravatar...
By multiplying the demands on various online merchant sites, they managed to decrypt the credit card data in only 6 seconds! The objective of this exercise was to guess the exact numbers of a Visa bank card. Not only the number of the card that appears on the front of it, but also the true expiration date and the famous cryptogram in the back.

theewerevue.com
Left by Liokac on Dec 12, 2016 2:09 AM

# re: AppFx.ServiceBus – Scatter Gather
Requesting Gravatar...

That's really cool. I would be interested in seeing more graphs of different information you pull from these logs.
sarcastic features
Left by druid on Mar 30, 2017 5:49 PM

# re: AppFx.ServiceBus – Scatter Gather
Requesting Gravatar...
There is definately a lot to know about this subject. I like all of the points you've made.
hill climb racing 2
Left by hill climb racing 2 on May 03, 2017 9:46 PM

# re: AppFx.ServiceBus – Scatter Gather
Requesting Gravatar...
It has been wonderful information how to apply passport Thank you
Left by Sumit smith on Jul 26, 2017 12:14 AM

# re: AppFx.ServiceBus – Scatter Gather
Requesting Gravatar...
best free gift code generator free ebay gift code generator
Left by Maitri Shah on Aug 04, 2017 8:09 PM

# re: AppFx.ServiceBus – Scatter Gather
Requesting Gravatar...
High quality back link get free in link submits
Left by Ujan CA on Oct 01, 2017 10:46 PM

# re: AppFx.ServiceBus – Scatter Gather
Requesting Gravatar...
That's really cool. free xbox gift code generator
Left by alex on Oct 20, 2017 11:35 PM

Your comment:
 (will show your gravatar)


Copyright © Michael Stephenson | Powered by: GeeksWithBlogs.net