private IRabbitMqMessageConsumer TeyGetOrSetMessageConsumer(Type eventType) { var(exchangeName, queueName) = GetSubscribeConfigure(eventType); var key = $"{exchangeName}_{queueName}"; if (RabbitMqMessageConsumerDic.ContainsKey(key)) { return(RabbitMqMessageConsumerDic[key]); } lock (_lock) { if (RabbitMqMessageConsumerDic.ContainsKey(key)) { return(RabbitMqMessageConsumerDic[key]); } var rabbitMqMessageConsumer = _rabbitMqMessageConsumerFactory.Create( new RabbitMqExchangeDeclareConfigure(exchangeName, "direct", true), new RabbitMqQueueDeclareConfigure(queueName)); rabbitMqMessageConsumer.OnMessageReceived(Consumer_Received); RabbitMqMessageConsumerDic.TryAdd(key, rabbitMqMessageConsumer); return(rabbitMqMessageConsumer); } }
public void Subscribe() { var consumers = typeFinder.Find(t => t.IsClass && !t.IsAbstract && typeof(IRabbitMQConsumer).IsAssignableFrom(t)); foreach (var item in consumers) { var obj = iocResolver.Resolve(item); Type messageType = (Type)item.InvokeMember("GetMessageType", System.Reflection.BindingFlags.InvokeMethod, null, obj, null); ExchangeDeclareConfiguration exchange = (ExchangeDeclareConfiguration)item.InvokeMember("GetExchangeDeclare", System.Reflection.BindingFlags.InvokeMethod, null, obj, null); QueueDeclareConfiguration queue = (QueueDeclareConfiguration)item.InvokeMember("GetQueueDeclare", System.Reflection.BindingFlags.InvokeMethod, null, obj, null); QOSConfiguration qOS = (QOSConfiguration)item.InvokeMember("GetQOSConfiguration", System.Reflection.BindingFlags.InvokeMethod, null, obj, null); string ConnectionName = (string)item.InvokeMember("GetConnectionName", System.Reflection.BindingFlags.InvokeMethod, null, obj, null); var consumer = consumerFactory.Create( exchange, //new ExchangeDeclareConfiguration( // ExchangeName, // ExchangeType, // durable: Durable //), queue, //new QueueDeclareConfiguration( // QueueName, // durable: Durable, // exclusive: Exclusive, // autoDelete: AutoDelete //), qOS, ConnectionName ); consumer.OnMessageReceived((model, ea) => { var eventName = ea.RoutingKey; var eventData = objectSerializer.Deserialize(messageType, ea.Body); var task = (Task)item.InvokeMember("ConsumeAsync", System.Reflection.BindingFlags.InvokeMethod, null, obj, new[] { eventData }); return(task); }); string routingKey = (string)item.InvokeMember("GetRoutingKey", System.Reflection.BindingFlags.InvokeMethod, null, obj, null); consumer.BindAsync(routingKey); Consumers.Add(item, consumer); Console.WriteLine("1"); } }
private void TeyCreateMessageConsumer(Type eventType) { var(exchangeName, queueName) = GetExchangeNameAndQueueName(eventType); var key = $"{exchangeName}_{queueName}"; lock (_lock) { if (!RabbitMqMessageConsumerDic.ContainsKey(key)) { var rabbitMqMessageConsumer = _rabbitMqMessageConsumerFactory.Create( new RabbitMqExchangeDeclareConfigure(exchangeName, "direct", true), new RabbitMqQueueDeclareConfigure(queueName)); rabbitMqMessageConsumer.OnMessageReceived(Consumer_Received); RabbitMqMessageConsumerDic.TryAdd(key, rabbitMqMessageConsumer); } RabbitMqMessageConsumerDic.TryGetValue(key, out var rabbitMqMessageConsumer1); var eventName = MessageNameAttribute.GetNameOrDefault(eventType); rabbitMqMessageConsumer1?.BindAsync(eventName); } }
private void TryCreateMessageConsumer(Type eventType) { var exchangeName = _options.Value.ExchangeName; var queueName = MessageGroupAttribute.GetGroupOrDefault(eventType); var key = $"{exchangeName}_{queueName}"; lock (_lock) { if (!RabbitMqMessageConsumerDic.TryGetValue(key, out var rabbitMqMessageConsumer)) { rabbitMqMessageConsumer = _rabbitMqMessageConsumerFactory.Create( new RabbitMqExchangeDeclareConfigure(exchangeName, "direct"), new RabbitMqQueueDeclareConfigure(queueName)); rabbitMqMessageConsumer.OnMessageReceived(Consumer_Received); RabbitMqMessageConsumerDic.TryAdd(key, rabbitMqMessageConsumer); } var eventName = MessageNameAttribute.GetNameOrDefault(eventType); rabbitMqMessageConsumer.BindAsync(eventName); } }
public RabbitMqDistributedEventBus( IOptions <RabbitMqEventBusOptions> options, IConnectionPool connectionPool, IRabbitMqSerializer serializer, IHybridServiceScopeFactory serviceScopeFactory, IOptions <DistributedEventBusOptions> distributedEventBusOptions, IRabbitMqMessageConsumerFactory messageConsumerFactory) : base(serviceScopeFactory) { ConnectionPool = connectionPool; Serializer = serializer; MessageConsumerFactory = messageConsumerFactory; DistributedEventBusOptions = distributedEventBusOptions.Value; RabbitMqEventBusOptions = options.Value; HandlerFactories = new ConcurrentDictionary <Type, List <IEventHandlerFactory> >(); EventTypes = new ConcurrentDictionary <string, Type>(); Consumer = MessageConsumerFactory.Create( new ExchangeDeclareConfiguration( RabbitMqEventBusOptions.ExchangeName, type: "direct", durable: true ), new QueueDeclareConfiguration( RabbitMqEventBusOptions.ClientName, durable: true, exclusive: false, autoDelete: false ), RabbitMqEventBusOptions.ConnectionName ); Consumer.OnMessageReceived(ProcessEventAsync); SubscribeHandlers(DistributedEventBusOptions.Handlers); }