/// <summary>
        /// 将AMQP连接转换为交换机信息数组
        /// </summary>
        /// <param name="amqpConfig">AMQP配置信息</param>
        /// <param name="amqpConnection">AMQP连接</param>
        /// <param name="hostId">主机ID</param>
        /// <param name="connectionStringParse">连接字符串解析</param>
        /// <param name="symmetricalEncryption">加密</param>
        /// <returns>交换机信息数组</returns>
        public static ExchangeInfo[] ToExchanges(this AmqpConfigInfo amqpConfig, AmqpConnectionInfo amqpConnection, out string hostId, IConnectionStringParse <AmqpConnectionInfo> connectionStringParse = null, ISymmetricalEncryption symmetricalEncryption = null)
        {
            hostId = null;
            if (amqpConfig == null || amqpConfig.Amqp.IsNullOrLength0() || amqpConnection == null || string.IsNullOrWhiteSpace(amqpConnection.VirtualPath))
            {
                return(null);
            }
            if (connectionStringParse == null)
            {
                connectionStringParse = AmqpConnectionUtil.DefaultConnectionStringParse;
            }

            symmetricalEncryption = SymmetricalEncryptionUtil.GetSymmetricalEncryption(symmetricalEncryption);

            foreach (var r in amqpConfig.Amqp)
            {
                if (string.IsNullOrWhiteSpace(r.ConnectionString))
                {
                    continue;
                }

                string connString = r.ConnectionEncrypt ? symmetricalEncryption.Decrypt(r.ConnectionString) : r.ConnectionString;
                var    conn       = connectionStringParse.Parse(connString);
                if (conn.Host.Equals(amqpConnection.Host) && conn.Port == amqpConnection.Port && conn.VirtualPath.Equals(amqpConnection.VirtualPath))
                {
                    hostId = r.HostId;

                    return(r.Exchanges);
                }
            }

            return(null);
        }
Example #2
0
        /// <summary>
        /// Initislizes a new instance of the <see cref="RestBusSubscriber"/>
        /// </summary>
        /// <param name="messageMapper">The <see cref="IMessageMapper"/> used by the subscriber.</param>
        /// <param name="settings">The subscriber settings</param>
        public RestBusSubscriber(IMessageMapper messageMapper, SubscriberSettings settings)
        {
            this.messageMapper = messageMapper;
            messagingConfig    = messageMapper.MessagingConfig; //Fetched only once
            if (messagingConfig == null)
            {
                throw new ArgumentException("messageMapper.MessagingConfig returned null", "messageMapper");
            }

            if (messageMapper.SupportedExchangeKinds == default(ExchangeKind))
            {
                throw new ArgumentException("messageMapper.SupportedExchangeKinds is not set up.", "messageMapper");
            }

            serviceName = (messageMapper.GetServiceName(null) ?? String.Empty).Trim();

            subscriberIdHeader = new string[] { AmqpUtils.GetNewExclusiveQueueId() };

            AmqpConnectionInfo.EnsureValid(messageMapper.ServerUris, "messageMapper.ServerUris");
            this.connectionFactory = new ConnectionFactory();
            connectionFactory.Uri  = messageMapper.ServerUris[0].Uri;
            ConnectionNames        = messageMapper.ServerUris.Select(u => u.FriendlyName ?? String.Empty).ToArray();
            connectionFactory.RequestedHeartbeat = Client.RPCStrategyHelpers.HEART_BEAT;

            this.Settings            = settings ?? new SubscriberSettings(); //Make sure a default value is set, if not supplied by user.
            this.Settings.Subscriber = this;                                 //Indicate that the subcriber settings is owned by this subscriber.
        }
Example #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="T:RestBus.RabbitMQ.RestBusClient" /> class.
        /// </summary>
        /// <param name="messageMapper">The <see cref="IMessageMapper" /> the client uses to route messages.</param>
        /// <param name="settings">Client settings.</param>
        public RestBusClient(IMessageMapper messageMapper, ClientSettings settings) : base(new HttpClientHandler(), true)
        {
            //Set default HttpClient related fields
            timeout = TimeSpan.FromSeconds(100);
            MaxResponseContentBufferSize = int.MaxValue;
            //TODO: Setup cancellation token here.

            //Ensure messageMapper server uris are valid
            AmqpConnectionInfo.EnsureValid(messageMapper.ServerUris, "messageMapper.ServerUris");

            //Configure RestBus fields/properties
            this.messageMapper   = messageMapper;
            this.messagingConfig = messageMapper.MessagingConfig; //Fetched only once.
            if (messagingConfig == null)
            {
                throw new ArgumentException("messageMapper.MessagingConfig returned null");
            }

            //Set ClientSettings
            this.Settings        = settings ?? new ClientSettings(); // Always have a default instance, if it wasn't passed in.
            this.Settings.Client = this;                             //Indicate that the settings is owned by this client.

            //Instantiate connection manager and RPC strategies;
            connectionMgr    = new ConnectionManager(messageMapper.ServerUris);
            directStrategy   = new DirectReplyToRPCStrategy();
            callbackStrategy = new CallbackQueueRPCStrategy(this.Settings, messageMapper.GetServiceName(null));
        }
        /// <summary>
        /// 将AMQP连接转换为主机ID
        /// </summary>
        /// <param name="amqpConfig">AMQP配置信息</param>
        /// <param name="amqpConnection">AMQP连接</param>
        /// <param name="connectionStringParse">连接字符串解析</param>
        /// <param name="symmetricalEncryption">加密</param>
        /// <returns>主机ID</returns>
        public static string ToHostId(this AmqpConfigInfo amqpConfig, AmqpConnectionInfo amqpConnection, IConnectionStringParse <AmqpConnectionInfo> connectionStringParse = null, ISymmetricalEncryption symmetricalEncryption = null)
        {
            string hostId;

            ToExchanges(amqpConfig, amqpConnection, out hostId, connectionStringParse, symmetricalEncryption);

            return(hostId);
        }
        /// <summary>
        /// 设置默认值
        /// </summary>
        /// <param name="connectionInfo">连接信息</param>
        protected override void SetDefaultValue(AmqpConnectionInfo connectionInfo)
        {
            base.SetDefaultValue(connectionInfo);

            if (connectionInfo.Port == 0)
            {
                connectionInfo.Port = RabbitConnectionUtil.DEFAULT_PORT;
            }
        }
Example #6
0
        /// <summary>
        /// 根据连接信息获取连接工厂
        /// </summary>
        /// <param name="connectionInfo">连接信息</param>
        /// <returns>连接工厂</returns>
        private ConnectionFactory GetConnectionFactory(AmqpConnectionInfo connectionInfo)
        {
            var factory = new ConnectionFactory()
            {
                HostName    = connectionInfo.Host,
                VirtualHost = connectionInfo.VirtualPath,
                Password    = connectionInfo.Password,
                UserName    = connectionInfo.User,
                Port        = connectionInfo.Port,
                AutomaticRecoveryEnabled = connectionInfo.AutoRecovery,
                RequestedHeartbeat       = TimeSpan.FromSeconds(connectionInfo.Heartbeat)
            };

            return(factory);
        }
Example #7
0
 /// <summary>
 /// 执行打开
 /// </summary>
 /// <param name="connectionModel">连接模型</param>
 protected override void ExecOpen(AmqpConnectionInfo connectionModel)
 {
     connection = GetConnectionFactory(connectionModel).CreateConnection();
 }