public PharmacyNotificationController(IPharmacyNotificationService service)
 {
     _notificationService = service;
 }
Exemplo n.º 2
0
        public Task StartAsync(CancellationToken cancellationToken)
        {
            using (var scope = scopeFactory.CreateScope())
            {
                var MySQLContext = scope.ServiceProvider.GetRequiredService <PharmacyDbContext>();
                _notificationService = scope.ServiceProvider.GetRequiredService <IPharmacyNotificationService>();

                var factory = new ConnectionFactory
                {
                    Uri = new Uri(url.Replace("amqp://", "amqps://"))
                };
                connection = factory.CreateConnection();
                channel    = connection.CreateModel();

                channel.ExchangeDeclare("psw-exchange", ExchangeType.Direct, true);

                channel.QueueBind("psw-queue", "psw-exchange", "psw-key");

                var consumer = new EventingBasicConsumer(channel);

                bool recive = true;

                while (recive)
                {
                    var reply = channel.BasicGet("psw-queue", false);
                    if (reply != null)
                    {
                        var body = reply.Body.ToArray();
                        try
                        {
                            var msg = Encoding.UTF8.GetString(body);

                            _notificationService.Add(msg);
                            Console.WriteLine(msg);
                            Thread.Sleep(10000);
                            channel.BasicAck(reply.DeliveryTag, false);
                        }
                        catch
                        {
                            channel.BasicReject(reply.DeliveryTag, true);
                        }
                    }
                    else
                    {
                        recive = false;
                    }
                }

                /* For future development
                 * consumer.Received += (ch, ea) =>
                 * {
                 *  var body = ea.Body.ToArray();
                 *  try
                 *  {
                 *      var msg = Encoding.UTF8.GetString(body);
                 *      Console.WriteLine(msg);
                 *      //WriteToFile(msg);
                 *      _notificationService.Add(msg);
                 *      Thread.Sleep(10000);
                 *      channel.BasicAck(ea.DeliveryTag, false);
                 *  }
                 *  catch
                 *  {
                 *      channel.BasicReject(ea.DeliveryTag, true);
                 *  }
                 * };
                 *
                 * channel.BasicConsume(queue: "psw-queue",
                 *                   autoAck: false,
                 *                   consumer: consumer);
                 */
                return(StartAsync(cancellationToken));
            }
        }
Exemplo n.º 3
0
 public RabbitMQService(IPharmacyNotificationService notificationService)
 //public RabbitMQService()
 {
     _notificationService = notificationService;
 }