/// <summary>
        /// 设置消息队列读取
        /// </summary>
        /// <param name="conn">消息队列连接</param>
        /// <param name="reader">消息队列读取</param>
        public static void SetMessageQueueReader(this IMessageQueueConnection conn, IRabbitMessageQueueReader reader)
        {
            if (conn == null)
            {
                return;
            }

            RabbitConnection rabbitConn = null;

            if (conn is RabbitAutoRecoveryConnection)
            {
                var autoRabbitConn = conn as RabbitAutoRecoveryConnection;
                if (autoRabbitConn.ProtoConnection == null)
                {
                    autoRabbitConn.ProtoConnection = new RabbitConnection();
                }
                else if (autoRabbitConn.ProtoConnection is RabbitConnection)
                {
                    rabbitConn = autoRabbitConn.ProtoConnection as RabbitConnection;
                }
                else
                {
                    throw new NotSupportedException("RabbitAutoRecoveryConnection.ProtoConnection不是RabbitConnection");
                }
            }
            else if (conn is RabbitConnection)
            {
                rabbitConn = conn as RabbitConnection;
            }
            else
            {
                throw new NotSupportedException("不支持的连接类型,必须是RabbitConnection");
            }

            RabbitMessageQueueInfoConfigFactory msgQueueConfigFactoy = null;

            if (rabbitConn.MessageQueueInfoFactory == null)
            {
                msgQueueConfigFactoy = new RabbitMessageQueueInfoConfigFactory();
                rabbitConn.MessageQueueInfoFactory = msgQueueConfigFactoy;
            }
            else if (rabbitConn.MessageQueueInfoFactory is RabbitMessageQueueInfoConfigFactory)
            {
                msgQueueConfigFactoy = rabbitConn.MessageQueueInfoFactory as RabbitMessageQueueInfoConfigFactory;
            }
            else
            {
                throw new NotSupportedException("消息队列信息工厂不支持的类型,必须是RabbitMessageQueueInfoConfigFactory");
            }

            msgQueueConfigFactoy.MessageQueueReader = reader;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 创建连接且打开
        /// </summary>
        /// <param name="connectionStringConfigName">连接字符串配置名称</param>
        /// <param name="messageQueueFilePath">消息队列文件路径</param>
        /// <param name="dataContentType">数据内容类型。只支持JSON和XML</param>
        /// <returns>连接</returns>
        public static IMessageQueueConnection CreateAndOpen(string connectionStringConfigName = null, string messageQueueFilePath = null, DataContentType dataContentType = DataContentType.JSON)
        {
            if (!(dataContentType == DataContentType.JSON || dataContentType == DataContentType.XML))
            {
                throw new NotSupportedException("数据内容类型只支持JSON或XML");
            }

            var conn = new RabbitAutoRecoveryConnection();

            if (string.IsNullOrWhiteSpace(connectionStringConfigName))
            {
                conn.Open();
            }
            else
            {
                var connString = PlatformTool.AppConfig[connectionStringConfigName];
                if (ConfigUtil.ConnectionEncryption)
                {
                    connString = DESUtil.Decrypt(connString, PlatformTool.AppConfig["DES:Key"], PlatformTool.AppConfig["DES:IV"]);
                }
                conn.Open(connString);
            }

            if (!string.IsNullOrWhiteSpace(messageQueueFilePath))
            {
                var rabbitConn = conn.ProtoConnection as RabbitConnection;
                var messageQueueInfoFactory = new RabbitMessageQueueInfoConfigFactory();
                rabbitConn.MessageQueueInfoFactory = messageQueueInfoFactory;

                if (dataContentType == DataContentType.JSON)
                {
                    messageQueueInfoFactory.MessageQueueReader = new RabbitMessageQueueJson(messageQueueFilePath);
                }
                else
                {
                    messageQueueInfoFactory.MessageQueueReader = new RabbitMessageQueueXml(messageQueueFilePath);
                }
            }

            return(conn);
        }