Showing posts with label RabbitMQ. Show all posts
Showing posts with label RabbitMQ. Show all posts

Sunday, 29 April 2012

RabbitMQ C# Pub/Sub

RabbitMQ   Burrow

In this blog post I’ll show how to set up RabbitMQ on a Windows7 machine and create a C# pub/sub workflow application using Burrow.NET.

Unable to display content. Adobe Flash is required.

Installing up RabbitMQ

Download and install Erlang and then RabbitMQ:

http://www.erlang.org/download.html  (Windows Binary File)

http://www.rabbitmq.com/server.html  (Installer for Windows systems)

Configuring RabbitMQ

Create the following file:  C:\Users\{username}\AppData\Roaming\RabbitMQ\rabbitmq.config

{"[{listeners,     [{mgmt, [{port, 55672}]}]},
{default_listener, [{port, 55670}]},
{contexts, [{rabbit_mgmt, mgmt}]}]"}

rabbitmq-config


Open a command prompt and run the following commands:

cd C:\Program Files (x86)\RabbitMQ Server\rabbitmq_server-2.8.0\sbin
rabbitmq-plugins enable rabbitmq_management
rabbitmq-service.bat install

rabbitmq-admin


Open Services and configure your RabbitMQ service as per your preferences (startup type and logon account).  Make sure the service is started.


image


RabbitMQ is now installed as a service with the web management portal available at http://localhost:55672

Username:  guest
Password:  guest

rabbit-web 

Setting up RabbitMQ


Add an Exchange of type “direct”


Select the Exchange tab and scroll down to add a new Exchange.


image


Add a Queue per message type


Select the Queue tab and scroll down to add a new Queue for “Durable” durability.

Naming convention:  Burrow.Queue.{SubscriptionName}.{MessageType}

The message type is the serialisable C# object you’ll be publishing to the queue.


image


Note: The Burrow.Queue.Error will be automatically generated if required


image


Finally add a binding per queue


Click on a queue in the list then add a binding to the exchange.

image


image


The Workflow


image


C# Pub/Sub with Burrow.NET


Burrow.NET is available from NuGest, here is how to use it:

string QUEUE_CONNECTION_STRING = "host=localhost;username=guest;password=guest";
string ORDER_ROUTING_KEY = "Order";

// set up rabbitmq tunnel
ITunnel tunnel = RabbitTunnel.Factory.Create(QUEUE_CONNECTION_STRING);
tunnel.SetSerializer(new JsonSerializer());

// sub
// where the named method interacts with the UI
// "Workflow" is used as the subscription name within the queue Burrow.Queue.Workflow.OrderMessage
var subscriptionOrder = tunnel.SubscribeAsync<OrderMessage>("Workflow",
(message, args) => Dispatcher.BeginInvoke(new Action(() => OnOrderReceived(message, args))));

// pub
tunnel.Publish(new OrderMessage
{
OrderId = orderId,
OrderDate = DateTime.Now,
ProductCode = "ABCDE",
Quantity = 10,
UnitPrice = 9.99m
}, ORDER_ROUTING_KEY);

Source


http://stevenhollidge.com/blog-source-code/RabbitMQ-Workflow.zip

Thursday, 22 March 2012

Metro Messaging

Unable to display content. Adobe Flash is required.

Here is a Metro styled client/server application that sends messages via queues:

metro-messaging-2

Queuing Frameworks

msmq

rabbitmq

zeromq

 

Other Frameworks

elysium

mvvmlight

Other Concepts

  • Real time updating Metro style Tiles as messages are received
  • Custom user control with data binding including dependency properties
  • Value converter as mark up extension (no need to reference as a resource)
  • Auto scrolling textboxes using custom attached property
  • Generics with predicates
  • MVVM Light locator, messenger, relay commands and view models
  • Metro style app with Elysium and Metro Icons (http://metro.windowswiki.info/)
  • Custom Build Event:    copy $(SolutionDir)Libs\zeromq\libzmq.dll $(TargetDir)

Deployment

You’ll need to install the server components for MSMQ and RabbitMQ before running the solution from the downloadable source below.

Source code:  http://stevenhollidge.com/blog-source-code/Messaging.zip

Friday, 23 September 2011

RabbitMQ

RabbitMQ

Here’s a simple example of how to use RabbitMQ to provide a simple pub/sub messaging architecture (aka observer/listener pattern).

Pub/Sub example

Once the RabbitMQ server is installed we are going to create a C# client to:

  • Create a queue
  • Send 10 messages to the queue
  • Read the messages back using two different techniques (5 messages each)

The code also shows how to inspect for the last message retrieved from a subscription.

The Output

ScreenShot134

Code

 

RabbitMQ Server Installation

For the server you’ll need to first install Erlang and then run the RabbitMQ installer:

http://www.erlang.org/download.html  (Windows Binary File)

http://www.rabbitmq.com/server.html  (Installer for Windows systems)

RabbitMQ Client Libraries

For the C# client libraries:

http://www.rabbitmq.com/dotnet.html

Why would you use a queue?

Queues allow messages containing work items or events to be stored in a durable manner. This enables disconnected middleware processes to be able to read from the queue, allowing for scalable processing via multiple processes reading from the queue. By contrast, sockets and other network protocols assume that direct connections always exist.

Different message types can be used on the same queue to provide a workflow scenario. Different processes can monitor the same queue for specific message types and post back to the same queue with a message type for the next stage in the workflow, which in turn gets picked up by another process and so it goes on.

For example, we might have a Web Server dealing with HTTP traffic and 2 worker processes all reading from and writing to the same queue.

  • A web server receives a request from a user to for an order and places an OrderReceived message on queue
  • Worker 1, which had subscribed to to this message type reads the message and “confirms the order” on the back end systems.  Once completed it writes OrderConfirmed message type back to the queue, which is picked up by worker 2
  • Worker 2, which had subscribed to the OrderConfirmed message type, completes its processing and writes an OrderDespatched message type back to the queue.

In reality, “Worker X” might be a bank of multiple computers and processes to allow for a high throughput of messages to be actioned.

A nice webcast can be found here by Clemens Vasters:

http://blogs.msdn.com/b/clemensv/archive/2011/03/18/why-would-anyone-ever-use-a-message-queue.aspx

Source Code

You can download the source code for this example:

http://stevenhollidge.com/blog-source-code/RabbitMqDemo.zip