Mercurial > silverbladetech
view Messaging/Server/Listeners/RabbitQueueListener.cs @ 29:9919ee227c93
msmq added
author | adminsh@apollo |
---|---|
date | Wed, 21 Mar 2012 22:09:18 +0000 |
parents | 4c0dea4760c5 |
children | 7d9de5746f18 |
line wrap: on
line source
using System; using System.Diagnostics; using System.Text; using Common.Messages; using GalaSoft.MvvmLight.Messaging; using RabbitMQ.Client; using RabbitMQ.Client.Events; using RabbitMQ.Client.MessagePatterns; using Server.Interfaces; namespace Server.Listeners { class RabbitQueueListener : IListener { public bool IsListening { get; set; } private readonly int _port; private Subscription _subscription; private string _queueName; public RabbitQueueListener(int port, string queueName) { _port = port; _queueName = queueName; } public void Start() { try { if (IsListening) return; var serverAddress = "localhost:" + _port; var connectionFactory = new ConnectionFactory { Address = serverAddress }; using (var connection = connectionFactory.CreateConnection()) { using (var channel = connection.CreateModel()) { Log(string.Format("Creating a queue {0} and binding it to amq.direct", _queueName)); string queueName = channel.QueueDeclare(_queueName, false, false, false, null); channel.QueueBind(queueName, "amq.direct", queueName, null); Log("Done."); using (_subscription = new Subscription(channel, queueName)) { IsListening = true; while (IsListening) { foreach (BasicDeliverEventArgs eventArgs in _subscription) { //Log(Encoding.UTF8.GetString(eventArgs.Body)); Messenger.Default.Send(new RabbitClientMessage()); _subscription.Ack(); } } } } } } catch (Exception e) { Log(e.Message); } } public void Stop() { Log("Closing listener..."); _subscription.Close(); IsListening = false; Log("Done."); } private void Log(string message) { Debug.WriteLine(message); Messenger.Default.Send(new RabbitLogMessage() { Body = message }); } } }