Example #1
0
        /// <summary>
        /// Publishes message from clients into RabbitMQ.
        /// </summary>
        /// <param name="message">Incoming message.</param>
        public void AcceptMessage(ServiceBusMessage message)
        {
            if (string.IsNullOrEmpty(message.ClientID))
            {
                throw new ArgumentNullException(nameof(message.ClientID));
            }

            if (string.IsNullOrEmpty(message.MessageTypeID))
            {
                throw new ArgumentNullException(nameof(message.MessageTypeID));
            }

            var password          = ConfigurationManager.AppSettings["DefaultRmqUserPassword"];
            var connectionFactory = GetConnectionFactoryForUser(message.ClientID, password);

            try
            {
                using (var connection = connectionFactory.CreateConnection())
                {
                    var model = connection.CreateModel();

                    var exchange   = _namingManager.GetExchangeName(message.MessageTypeID);
                    var routingKey = _namingManager.GetRoutingKey(message.MessageTypeID);

                    var messageBuilder = BuildMessage(message, model);

                    model.ConfirmSelect();
                    model.BasicPublish(exchange, routingKey, messageBuilder.Properties,
                                       messageBuilder.GetContentBody());
                    //we should wait confirm from broker before accept message
                    model.WaitForConfirmsOrDie();
                }
            }
            catch (BrokerUnreachableException e)
            {
                _logger.LogUnhandledException(e,
                                              title: $"Unavailable broker on publish message {message.MessageTypeID} from client {message.ClientID} to broker");
                throw;
            }
            catch (AlreadyClosedException e)
            {
                _logger.LogUnhandledException(e,
                                              title: $"Broker rejected incoming message {message.MessageTypeID} from {message.ClientID}. Reason: {e.ShutdownReason}.");
            }
            catch (Exception e)
            {
                _logger.LogUnhandledException(e, title: $"Error on receive message {message.MessageTypeID} from {message.ClientID}.");
            }
        }
Example #2
0
        /// <summary>
        /// Актуализация разрешений в RabbitMQ для конкретного пользователя.
        /// </summary>
        /// <param name="user">Пользователь RabbitMQ.</param>
        /// <param name="esbPermissions">Разрешения из шины.</param>
        /// <returns>Асинхронная операция синхронизации разрешений пользователя.</returns>
        private Task SynchronizePermissionsForClient(User user, List <SendingPermission> esbPermissions = null)
        {
            List <SendingPermission> currentEsbPermissions;
            string clientId = user.Name;

            if (esbPermissions == null)
            {
                currentEsbPermissions = _dataService.Query <SendingPermission>(SendingPermission.Views.ServiceBusView).Where(p => p.Client.ID == clientId).ToList();
            }
            else
            {
                currentEsbPermissions = esbPermissions.Where(p => p.Client.ID == clientId).ToList();
            }

            Permission mqPermission = _managementClient.GetPermissionsAsync().Result.Where(p => p.User == clientId && p.Vhost == Vhost.Name).FirstOrDefault();

            if (currentEsbPermissions.Count > 0)
            {
                List <string> rmqPermissionRegex = new List <string>();
                foreach (SendingPermission esbPermission in currentEsbPermissions)
                {
                    rmqPermissionRegex.Add(_namingManager.GetExchangeName(esbPermission.MessageType.ID));
                }

                if (mqPermission == null)
                {
                    return(_managementClient.CreatePermissionAsync(CreatePermissionInfo(user, $"^({string.Join("|", rmqPermissionRegex)})$")));
                }
                else
                {
                    PermissionInfo permissionInfo = CreatePermissionInfo(user, $"^({string.Join("|", rmqPermissionRegex)})$", mqPermission.Read, mqPermission.Configure);
                    return(_managementClient.CreatePermissionAsync(permissionInfo));
                }
            }
            else
            {
                if (mqPermission == null)
                {
                    return(_managementClient.CreatePermissionAsync(CreatePermissionInfo(user)));
                }
                else
                {
                    PermissionInfo permissionInfo = CreatePermissionInfo(user, "^$", mqPermission.Read, mqPermission.Configure);
                    return(_managementClient.CreatePermissionAsync(permissionInfo));
                }
            }
        }