.

RabbitMQ demo

Here is some sample code (or download here):

[TestFixture]
public class TestFixture1
{
    [Test]
    public void TestRabbitMq()
    {
        Task.Factory.StartNew(() =>{RunListeningService();});
            

        var factory = new ConnectionFactory() {HostName = "localhost"};
        using (var connection = factory.CreateConnection())
        using (var channel = connection.CreateModel())
        {
            channel.ExchangeDeclare("logs", "fanout");

            var message = GetMessage(new string[] {"logs"});

            var body = Encoding.UTF8.GetBytes(message);
            channel.BasicPublish("logs", "", null, body);
            Console.WriteLine(" [x] Sent {0}", message);
        }

        System.Threading.Thread.Sleep(2000);
    }

    private static string GetMessage(string[] args)
    {
        return ((args.Length > 0) ? string.Join(" ", args) : "info: Hello World!");
    }

    private void RunListeningService()
    {
        var factory = new ConnectionFactory() { HostName = "localhost" };
        using (var connection = factory.CreateConnection())
        {
            using (var channel = connection.CreateModel())
            {
                channel.ExchangeDeclare("logs", "fanout");

                var queueName = channel.QueueDeclare().QueueName;

                channel.QueueBind(queueName, "logs", "");
                var consumer = new QueueingBasicConsumer(channel);
                channel.BasicConsume(queueName, true, consumer);

                Console.WriteLine(" [*] Waiting for logs." +
                                    "To exit press CTRL+C");
                while (true)
                {
                    var ea = (BasicDeliverEventArgs)consumer.Queue.Dequeue();

                    var body = ea.Body;
                    var message = Encoding.UTF8.GetString(body);
                    Console.WriteLine(" [x] {0}", message);
                }
            }
        }

    }
}

Sample code from here: https://www.rabbitmq.com/tutorials/tutorial-three-dotnet.html

Grab Rabbit MQ from here: https://www.rabbitmq.com/download.html

Rabbit MQ admin site:  http://localhost:15672   default username: guest & password: guestRabbit MQ API: http://localhost:15672/api/

 

What's your thoughts on this?

*

Protected by WP Anti Spam