예제 #1
0
        static void Main(string[] args)
        {
            var host = Environment.GetEnvironmentVariable("host") ?? "localhost";

            Console.WriteLine(host);
            EftContext context = new EftContext();
            var        factory = new ConnectionFactory()
            {
                HostName = host, UserName = "******", Password = "******"
            };

            using (IConnection connection = factory.CreateConnection())
                using (IModel channel = connection.CreateModel())
                {
                    //TODO: Received methoduyla gelen dataları yakalayıp işlem yapacağımız için EventingBasicConsumer classından nesne alıyoruz.
                    var consumer = new EventingBasicConsumer(channel);
                    //TODO: Yeni data geldiğinde bu event otomatik tetikleniyor.
                    consumer.Received += (model, ea) =>
                    {
                        var             body            = ea.Body;                                                  //TODO: Kuyruktaki içerik bilgisi.
                        var             message         = Encoding.UTF8.GetString(body);                            //TODO: Gelen bodyi stringe çeviriyoruz.
                        SendingEftModel sendingEftModel = JsonConvert.DeserializeObject <SendingEftModel>(message); //TODO: Mesajdan dönen veriyi classa çeviriyoruz.
                        context.Send(sendingEftModel);                                                              //TODO: Contextimize gönderip Database.json dosyamıza kaydedilmesini sağlıyoruz.
                        Console.WriteLine($" {sendingEftModel.FromId} - {sendingEftModel.Money}₺ --> {sendingEftModel.ToId}");
                    };
                    channel.BasicConsume(queue: "Eft",        //TODO: Consume edilecek kuyruk ismi
                                         autoAck: true,       //TODO: Kuyruk ismi doğrulansın mı
                                         consumer: consumer); //TODO: Hangi consumer kullanılacak.

                    Console.WriteLine("Eft kuyruğuna bağlantı başarılı. Dinleniyor...");
                    Console.ReadLine();
                }
        }
예제 #2
0
        public static void SendMoney(SendingEftModel model)
        {
            //RabbitMQ bağlantısı için
            var factory = new ConnectionFactory()
            {
                HostName = "192.168.1.42", UserName = "******", Password = "******"
            };

            //Channel yaratmak için
            using (IConnection connection = factory.CreateConnection())
                using (IModel channel = connection.CreateModel())
                {
                    //Kuyruk oluşturma
                    channel.QueueDeclare(queue: "Eft",
                                         durable: false,    //Data fiziksel olarak mı yoksa memoryde mi tutulsun
                                         exclusive: false,  //Başka connectionlarda bu kuyruğa ulaşabilsin mi
                                         autoDelete: false, //Eğer kuyruktaki son mesaj ulaştığında kuyruğun silinmesini istiyorsak kullanılır.
                                         arguments: null);  //Exchangelere verilecek olan parametreler tanımlamak için kullanılır.

                    string message = JsonConvert.SerializeObject(model);
                    var    body    = Encoding.UTF8.GetBytes(message);

                    //Queue ya atmak için kullanılır.
                    channel.BasicPublish(exchange: "",      //mesajın alınıp bir veya birden fazla queue ya konmasını sağlıyor.
                                         routingKey: "Eft", //Hangi queue ya atanacak.
                                         body: body);       //Mesajun içeriği
                }
        }
예제 #3
0
        static void Main(string[] args)
        {
            EftContext context = new EftContext();
            var        factory = new ConnectionFactory()
            {
                HostName = "localhost", UserName = "******", Password = "******"
            };

            using (IConnection connection = factory.CreateConnection())
                using (IModel channel = connection.CreateModel())
                {
                    var consumer = new EventingBasicConsumer(channel);
                    consumer.Received += (model, ea) =>
                    {
                        var             body            = ea.Body;
                        var             message         = Encoding.UTF8.GetString(body);
                        SendingEftModel sendingEftModel = JsonConvert.DeserializeObject <SendingEftModel>(message);
                        context.Send(sendingEftModel);
                        Console.WriteLine($" {sendingEftModel.FromId} - {sendingEftModel.Money}₺ --> {sendingEftModel.ToId}");
                    };
                    channel.BasicConsume(queue: "Eft", autoAck: true, consumer: consumer);

                    Console.WriteLine("Eft kuyruğuna bağlantı başarılı. Dinleniyor...");
                    Console.ReadKey();
                }
        }
예제 #4
0
        public void Send(SendingEftModel model)
        {
            var fromCustomer = _customers.FirstOrDefault(p => p.Id == model.FromId);

            if (fromCustomer == null)
            {
                throw new ArgumentNullException("From Customer Not Found");
            }

            var toCustomer = _customers.FirstOrDefault(p => p.Id == model.ToId);

            if (toCustomer == null)
            {
                throw new ArgumentNullException("To Customer Not Found");
            }

            if (fromCustomer.Balance < model.Money)
            {
                throw new ArgumentNullException("Insufficient balance");
            }

            fromCustomer.Balance -= model.Money;
            toCustomer.Balance   += model.Money;

            var serialize = JsonConvert.SerializeObject(_customers);

            File.WriteAllText("Database.json", serialize);
        }
        public static void SendMoney(SendingEftModel model)
        {
            var factory = new ConnectionFactory()
            {
                HostName = "localhost", UserName = "******", Password = "******"
            };

            using (var connection = factory.CreateConnection())
                using (var channel = connection.CreateModel())
                {
                    channel.QueueDeclare(queue: "Eft",
                                         durable: false,
                                         exclusive: false,
                                         autoDelete: false,
                                         arguments: null);

                    string message = JsonConvert.SerializeObject(model);
                    var    body    = Encoding.UTF8.GetBytes(message);

                    channel.BasicPublish(exchange: "", routingKey: "Eft", body: body);
                }
        }
 public IActionResult SendToMoney([FromBody] SendingEftModel model)
 {
     EftProducerHelper.SendMoney(model);
     return(Ok("Eft isteğiniz listelenmiştir."));
 }