Windows install RabbitMQ via PowerShell as administrator role

//Run PowerShell as Administrator role
//Run below commands respectively
//First command
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))

//Second Command
choco install rabbitmq 

 

 

 

image

 

 

 

 

 

image

 

 

//Send.cs
using RabbitMQ.Client;
using System.Text;

namespace ConsoleApp6
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
            await SendMessageAsync();
        }

        public static async Task SendMessageAsync()
        {
            var factory = new ConnectionFactory { HostName = "localhost" };
            using (var connection = await factory.CreateConnectionAsync())
            {
                using (var channel = await connection.CreateChannelAsync())
                {
                    await channel.QueueDeclareAsync(queue: "hello", durable: false,
                       exclusive: false, autoDelete: false, arguments: null);
                    for (int i = 1; i < 11; i++)
                    {
                        string msg = $"Id:{i},now {DateTime.Now.ToString("yyyyMMdd HH:mm:ss.ffff")}_{Guid.NewGuid():N}";
                        
                        var body = Encoding.UTF8.GetBytes(msg);
                        await channel.BasicPublishAsync(exchange: string.Empty,
                            routingKey: "hello", body: body);
                        Console.WriteLine($"[X] sent {msg}");
                    }
                    Console.WriteLine($"Press [enter] to exit!");
                    Console.ReadLine();
                }
            }
        }
    }
}


//Receive.cs
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp7
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
            await ReceiveDemo();
        }

        static async Task ReceiveDemo()
        {
            var factory = new ConnectionFactory { HostName = "localhost" };
            using (var connection = await factory.CreateConnectionAsync())
            {
                using (var channel = await connection.CreateChannelAsync())
                {
                    await channel.QueueDeclareAsync(queue: "hello",
                        durable: false, exclusive: false, autoDelete: false, arguments: null);

                    Console.Write($"[*] Waiting for messages.");
                    var consumer=new AsyncEventingBasicConsumer(channel);
                    consumer.ReceivedAsync +=async (s, e) =>
                    {
                        var body = e.Body.ToArray();
                        var message = Encoding.UTF8.GetString(body);
                        Console.WriteLine($"[x] Received {message}");
                    };

                    await channel.BasicConsumeAsync(queue:"hello", autoAck: true, consumer: consumer);
                    Console.WriteLine("Press [Enter] to exit.");
                    Console.ReadLine();
                }
            }
        }
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

image

 

 

image

 

posted @ 2025-10-06 16:28  FredGrit  阅读(5)  评论(0)    收藏  举报