Example #1
0
        static void Main(string[] args)
        {
            var factoryConsumer = new ConnectionFactory
            {
                Uri = new Uri("amqp://*****:*****@localhost:5672")
            };

            using var connectionConsumer = factoryConsumer.CreateConnection();
            using var channelConsumer    = connectionConsumer.CreateModel();
            channelConsumer.ExchangeDeclare("demo-direct-exchange2", ExchangeType.Direct);
            channelConsumer.QueueDeclare("demo-direct-queue2",
                                         durable: true,
                                         exclusive: false,
                                         autoDelete: false,
                                         arguments: null);
            channelConsumer.QueueBind("demo-direct-queue2", "demo-direct-exchange2", "account.init");
            channelConsumer.BasicQos(0, 10, false);

            var factoryPublisher = new ConnectionFactory
            {
                Uri = new Uri("amqp://*****:*****@localhost:5672")
            };

            using var connectionPublisher = factoryPublisher.CreateConnection();
            using var channelPublisher    = connectionPublisher.CreateModel();
            var ttl = new Dictionary <string, object>
            {
                { "x-message-ttl", 30000 }
            };

            channelPublisher.ExchangeDeclare("demo-direct-exchange", ExchangeType.Direct, arguments: ttl);

            HttpListener listener = new HttpListener();

            listener.Prefixes.Add("http://localhost:7771/");
            listener.Start();
            Console.WriteLine("Ожидание подключений...");

            while (true)
            {
                HttpListenerContext context = listener.GetContext();
                HttpListenerRequest request = context.Request;
                string json = context.Request.Headers.GetValues("json")?.GetValue(0)?.ToString() ?? null;
                HttpListenerResponse response = context.Response;
                response.StatusCode = 200;
                string responseString = "";

                if (!string.IsNullOrEmpty(json))
                {
                    responseString = FanoutExchangePublisher.Publish(channelPublisher, channelConsumer, json);
                }

                byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
                response.ContentLength64 = buffer.Length;
                Stream output = response.OutputStream;
                output.Write(buffer, 0, buffer.Length);
                output.Close();
            }
        }
Example #2
0
        static void Main(string[] args)
        {
            var connectionFactory = new ConnectionFactory
            {
                Uri = new Uri("amqp://*****:*****@localhost:5672")
            };

            using var connection = connectionFactory.CreateConnection();
            using var channel    = connection.CreateModel();

            // QueueProducer.Publish(channel);
            // DirectExchangePublisher.Publish(channel);
            // TopicExchangePublisher.Publish(channel);
            // HeaderExchangePublisher.Publish(channel);
            FanoutExchangePublisher.Publish(channel);
        }
Example #3
0
        static void Main(string[] args)
        {
            var factory = new ConnectionFactory
            {
                Uri = new Uri("amqp://*****:*****@localhost:5672")
            };

            using (var connection = factory.CreateConnection())
            {
                using (var chanel = connection.CreateModel())
                {
                    //QueueProducer.Publish(chanel);
                    FanoutExchangePublisher.Publish(chanel);
                }
            }

            Console.ReadKey();
        }
        public static void Consume(IModel channelConsumer, IModel channelProducer)
        {
            var consumer = new EventingBasicConsumer(channelConsumer);

            consumer.Received += (sender, e) => {
                var body    = e.Body.ToArray();
                var message = Encoding.UTF8.GetString(body);
                Console.WriteLine(message);

                RabbitMQJsonModel jsonObj = JsonSerializer.Deserialize <RabbitMQJsonModel>(message);

                HostelUnitOfWork unitOfWork = new HostelUnitOfWork();
                switch (jsonObj.Method)
                {
                case "CreateRoomResident":
                {
                    CreateRoomResidentRequest request = JsonSerializer.Deserialize <CreateRoomResidentRequest>(jsonObj.ObjectJSON);

                    int createdId = unitOfWork.RoomResidents.Create(new HostelDB.Database.Models.RoomResident()
                        {
                            RoomId     = request.RoomId,
                            ResidentId = request.ResidentId,
                            SettleDate = DateTime.Now,
                            EvictDate  = DateTime.Now
                        }).Id;

                    CreateRoomResidentReply reply = new CreateRoomResidentReply()
                    {
                        Id = createdId
                    };

                    string requestJSON = JsonSerializer.Serialize <CreateRoomResidentReply>(reply);

                    FanoutExchangePublisher.Publish(channelProducer, requestJSON);

                    break;
                }

                case "CreateRoomCreateResidentRequest":
                {
                    CreateRoomCreateResidentRequest request = JsonSerializer.Deserialize <CreateRoomCreateResidentRequest>(jsonObj.ObjectJSON);

                    int createdResidentId = unitOfWork.Residents.Create(new Resident()
                        {
                            Birthday   = Convert.ToDateTime(request.Birthday),
                            Name       = request.Name,
                            Surname    = request.Surname,
                            Patronymic = request.Patronymic
                        }).Id;

                    int createdId = unitOfWork.RoomResidents.Create(new HostelDB.Database.Models.RoomResident()
                        {
                            RoomId     = request.RoomId,
                            ResidentId = createdResidentId,
                            SettleDate = DateTime.Now,
                            EvictDate  = DateTime.Now
                        }).Id;

                    CreateRoomCreateResidentReply reply = new CreateRoomCreateResidentReply()
                    {
                        Id         = createdId,
                        ResidentId = createdResidentId
                    };

                    string requestJSON = JsonSerializer.Serialize <CreateRoomCreateResidentReply>(reply);

                    FanoutExchangePublisher.Publish(channelProducer, requestJSON);

                    break;
                }

                case "DeleteRoomResident":
                {
                    DeleteRoomResidentRequest request = JsonSerializer.Deserialize <DeleteRoomResidentRequest>(jsonObj.ObjectJSON);

                    bool result = unitOfWork.RoomResidents.Delete(request.Id);

                    DeleteRoomResidentReply reply = new DeleteRoomResidentReply()
                    {
                        Result = result
                    };

                    string requestJSON = JsonSerializer.Serialize <DeleteRoomResidentReply>(reply);

                    FanoutExchangePublisher.Publish(channelProducer, requestJSON);

                    break;
                }

                default:
                {
                    break;
                }
                }

                unitOfWork.Save();
            };

            channelConsumer.BasicConsume("demo-direct-queue", true, consumer);
            Console.WriteLine("Consumer started");
            Console.ReadLine();
        }