/// <summary> /// 创建消息队列连接并打开 /// </summary> /// <param name="factory">Rabbit连接工厂</param> /// <param name="funQueueReader">消息队列读取</param> /// <param name="options">配置</param> /// <returns>消息队列连接</returns> public static IMessageQueueConnection CreateConnectionAndOpen(this IMessageQueueConnectionFactory factory, Func <string, IRabbitMessageQueueReader> funQueueReader = null, Action <RabbitConnectionWrapInfo> options = null) { if (factory == null) { throw new ArgumentNullException("Rabbit连接工厂不能为null"); } var config = new RabbitConnectionWrapInfo(); if (options != null) { options(config); } var conn = factory.CreateAndOpen(config); IRabbitMessageQueueReader ququeReader = null; if (funQueueReader == null) { ququeReader = new RabbitMessageQueueJson(config.MessageQueueJsonFile); } else { ququeReader = funQueueReader(config.MessageQueueJsonFile); } conn.SetMessageQueueReader(ququeReader); return(conn); }
/// <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; }