Created
September 23, 2014 15:35
-
-
Save bronumski/3bae9c15eb95cc37ca48 to your computer and use it in GitHub Desktop.
RabbitMQ Demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Text; | |
using RabbitMQ.Client; | |
namespace Publisher | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var factory = new ConnectionFactory { HostName = "localhost" }; | |
Console.WriteLine("1 - Send basic message"); | |
Console.WriteLine("2 - Broadcast message"); | |
while (true) | |
{ | |
string input = Console.ReadLine(); | |
switch (input) | |
{ | |
case "1": | |
Console.WriteLine("Sending basic message"); | |
SendBasicMessage(factory); | |
break; | |
case "2": | |
Console.WriteLine("Broadcasting message"); | |
BroadcastMessage(factory); | |
break; | |
default: | |
Console.WriteLine("Unknonwn: " + input); | |
break; | |
} | |
} | |
} | |
static void SendBasicMessage(ConnectionFactory factory) | |
{ | |
using (var connection = factory.CreateConnection()) | |
using (var channel = connection.CreateModel()) | |
{ | |
channel.QueueDeclare("hello", false, false, false, null); | |
string message = "Hello World!"; | |
var body = Encoding.UTF8.GetBytes(message); | |
channel.BasicPublish("", "hello", null, body); | |
Console.WriteLine(" [x] Sent {0}", message); | |
} | |
} | |
static void BroadcastMessage(ConnectionFactory factory) | |
{ | |
using (var connection = factory.CreateConnection()) | |
using (var channel = connection.CreateModel()) | |
{ | |
channel.ExchangeDeclare("Stuff", "fanout"); | |
var message = "Stuff happened"; | |
var body = Encoding.UTF8.GetBytes(message); | |
channel.BasicPublish("Stuff", "", null, body); | |
Console.WriteLine(" [x] Broadcast {0}", message); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Text; | |
using System.Threading.Tasks; | |
using RabbitMQ.Client; | |
namespace Subscriber | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var factory = new ConnectionFactory { HostName = "localhost" }; | |
Task listenToQueue = Task.Factory.StartNew(() => ListenToMessageQueue(factory)); | |
Task listenForBroadcast = Task.Factory.StartNew(() => ListenToBroadCastMessage(factory)); | |
Console.WriteLine(" [*] Waiting for logs. To exit press CTRL+C"); | |
Task.WaitAll(listenToQueue, listenForBroadcast); | |
} | |
static void ListenToMessageQueue(ConnectionFactory factory) | |
{ | |
using (var connection = factory.CreateConnection()) | |
using (var channel = connection.CreateModel()) | |
{ | |
channel.QueueDeclare("hello", false, false, false, null); | |
var consumer = new QueueingBasicConsumer(channel); | |
channel.BasicConsume("hello", true, consumer); | |
while (true) | |
{ | |
var ea = consumer.Queue.Dequeue(); | |
var body = ea.Body; | |
var message = Encoding.UTF8.GetString(body); | |
Console.WriteLine(" [x] Received message: {0}", message); | |
} | |
} | |
} | |
static void ListenToBroadCastMessage(ConnectionFactory factory) | |
{ | |
using (var connection = factory.CreateConnection()) | |
using (var channel = connection.CreateModel()) | |
{ | |
channel.ExchangeDeclare("Stuff", "fanout"); | |
var queueName = channel.QueueDeclare().QueueName; | |
channel.QueueBind(queueName, "Stuff", ""); | |
var consumer = new QueueingBasicConsumer(channel); | |
channel.BasicConsume(queueName, true, consumer); | |
while (true) | |
{ | |
var ea = consumer.Queue.Dequeue(); | |
var body = ea.Body; | |
var message = Encoding.UTF8.GetString(body); | |
Console.WriteLine(" [x] Recieved broadcast: {0}", message); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment