Created
April 8, 2012 22:08
RabbitsTestApp
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; | |
using System.Threading.Tasks; | |
using RabbitMQ.Client; | |
using RabbitMQ.Client.MessagePatterns; | |
using Rabbits.Properties; | |
namespace Rabbits | |
{ | |
class Program | |
{ | |
static void Main() | |
{ | |
new Timer(_ => Publisher(), null, 0, 1000); | |
Task.Factory.StartNew(Subscriber); | |
Console.ReadLine(); | |
} | |
static readonly Lazy<ConnectionFactory> ConnFactory = new Lazy<ConnectionFactory>(() => new ConnectionFactory | |
{ | |
// amqp-0-9://localhost:5672 | |
HostName = Settings.Default.RabbitMQHostName | |
}); | |
static void Publisher() | |
{ | |
using (var conn = ConnFactory.Value.CreateConnection()) | |
using (var channel = conn.CreateModel()) | |
{ | |
var msg = DateTime.Now.ToString("F"); | |
var messageBody = Encoding.UTF8.GetBytes(msg); | |
channel.QueueDeclare("queue1", false, false, false, null); | |
channel.BasicPublish("", "queue1", null, messageBody); | |
Console.WriteLine(">>> Enqueued: " + msg); | |
} | |
} | |
static void Subscriber() | |
{ | |
using (var conn = ConnFactory.Value.CreateConnection()) | |
using (var channel = conn.CreateModel()) | |
{ | |
channel.QueueDeclare("queue1", false, false, false, null); | |
var subscription = new Subscription(channel, "queue1"); | |
while (true) | |
{ | |
var message = subscription.Next(); | |
var text = Encoding.UTF8.GetString(message.Body); | |
Console.WriteLine("<<< Dequeued: " + text); | |
subscription.Ack(message); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment