private static string StripPrefixIfNecessary(string address, IAmqpConnection connection, byte type) { if (address == null) { return(null); } if (type == MessageSupport.JMS_DEST_TYPE_QUEUE) { string queuePrefix = connection.QueuePrefix; if (queuePrefix != null && address.StartsWith(queuePrefix)) { return(address.Substring(queuePrefix.Length)); } } else if (type == MessageSupport.JMS_DEST_TYPE_TOPIC) { string topicPrefix = connection.TopicPrefix; if (topicPrefix != null && address.StartsWith(topicPrefix)) { return(address.Substring(topicPrefix.Length)); } } return(address); }
protected async Task <bool> Authenticate() { IAmqpAuthenticator amqpAuth; IAmqpConnection connection = this.Link.Session.Connection; // Check if Principal is IAmqpAuthenticator if (connection.Principal is IAmqpAuthenticator connAuth) { amqpAuth = connAuth; } else if (connection.FindExtension <ICbsNode>() is IAmqpAuthenticator cbsAuth) { amqpAuth = cbsAuth; } else { throw new InvalidOperationException($"Unable to find authentication mechanism for AMQP connection for identity {this.Identity.Id}"); } bool authenticated = await amqpAuth.AuthenticateAsync(this.Identity.Id); if (authenticated) { await this.ClientVersion .Filter(c => !string.IsNullOrWhiteSpace(c)) .ForEachAsync(c => this.productInfoStore.SetProductInfo(this.Identity.Id, c)); await this.ModelId.ForEachAsync(m => this.modelIdStore.SetModelId(this.Identity.Id, m)); } return(authenticated); }
protected IAmqpConsumer CreateMockConsumer(IAmqpConnection connection) { Mock <IAmqpConsumer> mockConsumer = new Mock <IAmqpConsumer>(); mockConsumer.Setup(consumer => consumer.Connection).Returns(connection); mockConsumer.Setup(consumer => consumer.Destination).Returns(new NmsTopic("TestTopic")); return(mockConsumer.Object); }
protected AmqpNmsMessageFacade CreateReceivedMessageFacade(global::Amqp.Message message) { AmqpNmsMessageFacade facade = new AmqpNmsMessageFacade(); IAmqpConnection mockAmqpConnection = CreateMockAmqpConnection(); var mockConsumer = CreateMockConsumer(mockAmqpConnection); facade.Initialize(mockConsumer, message); return(facade); }
public static IDestination GetReplyTo(AmqpNmsMessageFacade message, IAmqpConnection connection, IDestination consumerDestination) { string replyTo = message.ReplyToAddress; object typeAnnotation = message.GetMessageAnnotation(SymbolUtil.JMSX_OPT_REPLY_TO); if (typeAnnotation != null) { byte type = Convert.ToByte(typeAnnotation); string name = StripPrefixIfNecessary(replyTo, connection, type); return(CreateDestination(name, type)); } else { string name = StripPrefixIfNecessary(replyTo, connection); return(CreateDestination(name, consumerDestination, true)); } }
//private readonly TenantContextAccessor _tenantContextAccessor; //private readonly ITenantContextFactory _tenantContextFactory; public RabbitMQEventBus( IServiceProvider serviceProvider, string queueName = null, int retryCount = 5, string brokerName = "est_event_bus") { _serviceProvider = serviceProvider; _persistentConnection = serviceProvider.GetRequiredService <IAmqpConnection>(); _logger = _serviceProvider.GetService <ILogger <RabbitMQEventBus> >(); _subsManager = serviceProvider.GetRequiredService <IEventBusSubscriptionsManager>(); //_tenantContextAccessor = serviceProvider.GetRequiredService<TenantContextAccessor>(); //_tenantContextFactory = serviceProvider.GetRequiredService<ITenantContextFactory>(); _queueName = queueName; _brokerName = brokerName; _retryCount = retryCount; _consumerChannel = CreateConsumerChannel(); _subsManager.OnEventRemoved += SubsManager_OnEventRemoved; }
public RabbitMessageDispatcher( IAmqpConnection persistentConnection, ILogger <RabbitMessageDispatcher> logger, IServiceProvider serviceProvider, IRequestHandlerManager handlerManager, string rpcQueueName = "rpc_queue", string eventQueueName = "event_queue", int retryCount = 5) { _persistentConnection = persistentConnection; _serviceProvider = serviceProvider; _logger = logger; _handlerManager = handlerManager; _eventQueueName = eventQueueName; _rpcQueueName = rpcQueueName; _retryCount = retryCount; _rpcChannel = CreateRpcChannel(); _eventChannel = CreateEventChannel(); }
protected Task <bool> Authenticate() { IAmqpAuthenticator amqpAuth; IAmqpConnection connection = this.Link.Session.Connection; // Check if Principal is IAmqpAuthenticator if (connection.Principal is IAmqpAuthenticator connAuth) { amqpAuth = connAuth; } else if (connection.FindExtension <ICbsNode>() is IAmqpAuthenticator cbsAuth) { amqpAuth = cbsAuth; } else { throw new InvalidOperationException($"Unable to find authentication mechanism for AMQP connection for identity {this.Identity.Id}"); } return(amqpAuth.AuthenticateAsync(this.Identity.Id)); }
public static string GetDestinationAddress(IDestination destination, IAmqpConnection connection) { if (destination != null) { string qPrefix = null; string tPrefix = null; if (!destination.IsTemporary) { qPrefix = connection.QueuePrefix; tPrefix = connection.TopicPrefix; } string destinationName = null; string prefix = null; if (destination.IsQueue) { destinationName = (destination as IQueue)?.QueueName; prefix = qPrefix ?? string.Empty; } else { destinationName = (destination as ITopic)?.TopicName; prefix = tPrefix ?? string.Empty; } if (destinationName != null) { if (!destinationName.StartsWith(prefix)) { destinationName = prefix + destinationName; } } return(destinationName); } else { return(null); } }
private static string StripPrefixIfNecessary(string address, IAmqpConnection connection) { if (address == null) { return(null); } string queuePrefix = connection.QueuePrefix; if (queuePrefix != null && address.StartsWith(queuePrefix)) { return(address.Substring(queuePrefix.Length)); } string topicPrefix = connection.TopicPrefix; if (topicPrefix != null && address.StartsWith(topicPrefix)) { return(address.Substring(topicPrefix.Length)); } return(address); }
/// <summary> /// 启动监听消费集合 /// </summary> /// <param name="lifetime">主机应用生命周期</param> /// <param name="provider">服务提供者</param> /// <param name="conn">AMQP连接</param> /// <param name="options">配置</param> /// <returns>主机应用生命周期</returns> public static IHostApplicationLifetime ListenConsumers(this IHostApplicationLifetime lifetime, IServiceProvider provider, IAmqpConnection conn, Action <ConsumerOptions> options) { if (options == null) { throw new ArgumentNullException("配置不能为null"); } if (provider == null) { throw new ArgumentNullException("服务提供者不能为null"); } if (conn == null) { throw new ArgumentNullException("AMQP连接不能为null"); } var config = new ConsumerOptions(); options(config); if (config.ConsumerConfigs.IsNullOrLength0()) { throw new ArgumentException("消费者配置数组不能为空"); } var consumers = new List <IConsumer>(config.ConsumerConfigs.Length); lifetime.ApplicationStarted.Register(() => { foreach (var con in config.ConsumerConfigs) { var title = $"交换机:{con.Exchange},队列:{con.Queue}"; if (con.ReceiveMessageType == null) { throw new ArgumentNullException($"[{title}]接收消息类型不能为null"); } var consumer = conn.CreateConsumer(con.Exchange, con.Queue); // 作为消费者服务,需要输入监听的交换机和队列 consumers.Add(consumer); if (con.BytesSerialization != null) { consumer.BytesSerialization = con.BytesSerialization; } else if (config.DefaultBytesSerialization != null) { consumer.BytesSerialization = config.DefaultBytesSerialization; } Console.WriteLine($"{title}开启监听..."); if (con.ReceiveMessageType == typeof(string)) // 接收字符串 { consumer.Subscribe(receiveMessageFun: (string msg) => { return(HandleMessage(con, provider, msg)); }, isAutoAck: con.Autoack); } else if (con.ReceiveMessageType == typeof(byte[])) // 接收字节数组 { consumer.Subscribe(receiveMessageFun: (byte[] msg) => { return(HandleMessage(con, provider, msg)); }, isAutoAck: con.Autoack); } else { consumer.Subscribe(receiveMessageFun: (object msg) => // 其他统一接收对象 { return(HandleMessage(con, provider, msg)); }, receiveMessageType: con.ReceiveMessageType, isAutoAck: con.Autoack); } } }); lifetime.ApplicationStopping.Register(() => { for (var i = 0; i < config.ConsumerConfigs.Length; i++) { var title = $"交换机:{config.ConsumerConfigs[i].Exchange},队列:{config.ConsumerConfigs[i].Queue}"; Console.WriteLine($"[{title}]停止监听"); consumers[i].Close(); consumers[i].Dispose(); } Console.WriteLine("AMQP停止连接"); conn.Close(); conn.Dispose(); }); return(lifetime); }
public ConnectionHandler(IAmqpConnection connection, IConnectionProvider connectionProvider) { this.connection = Preconditions.CheckNotNull(connection, nameof(connection)); this.connectionProvider = Preconditions.CheckNotNull(connectionProvider, nameof(connectionProvider)); }
public AmqpMessageFactory(IAmqpConnection connection) { this.connection = connection; }