Skip to content

Instantly share code, notes, and snippets.

@legigor
Created April 8, 2012 22:08
Show Gist options
  • Save legigor/2340062 to your computer and use it in GitHub Desktop.
Save legigor/2340062 to your computer and use it in GitHub Desktop.
RabbitsTestApp
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