public static void DynamicAddSender(PlatformBus advancedBus, OutMessage outMes, string exchName, string exchType = null) { if (advancedBus.Senders.Any(x => x.ExchangeName == exchName && x.RouteKey == outMes.Key)) { return; } if (string.IsNullOrEmpty(exchName) || string.IsNullOrEmpty(outMes.Name)) { throw new ArgumentNullException("exchName or name"); } if (string.IsNullOrEmpty(exchType)) { exchType = "topic"; } var exchange = advancedBus.CoreBus.ExchangeDeclare(exchName, exchType); if (!outMes.Timeout.HasValue) { outMes.Timeout = new TimeSpan(0, 0, 10); } advancedBus.Senders.Add( new BusSender(EndpointInfoFactory.Create(advancedBus.CoreBus, exchange, outMes.Name, outMes.Key, outMes.Persist, outMes.Timeout)) { ExchangeType = exchType }); }
private static void CreateConsumers(IDependencyResolver resolver, IAdvancedBus advancedBus, ExchangeItem exchangeItem) { if (exchangeItem.RabbitExchange == null) { throw new ArgumentException("rabbit exchange is null`"); } foreach (var income in exchangeItem.In) { var queue = advancedBus.QueueDeclare(string.Format("{0}.{1}", exchangeItem.Name, income.Key)); advancedBus.Bind(exchangeItem.RabbitExchange, queue, income.Key); var messageType = ReflectionHelper.ResolveType(income.Type); if (messageType == null) { throw new Exception("no such message type"); } Type consType = ReflectionHelper.ResolveType(income.React); var consumer = resolver.Resolve(consType); var endpointInfo = EndpointInfoFactory.Create(advancedBus, exchangeItem.RabbitExchange, income.Name, income.Key); var handlerReg = GetHandlerRegistration(endpointInfo, consumer, messageType); IList <object> consumeInvokeParams = new List <object>(); consumeInvokeParams.Add(queue); var action = ConsumeWrapperProvider.GetConsumeActionWrapper(handlerReg, resolver); consumeInvokeParams.Add(action); if (!ConsumerActions.TryAdd(consumer.GetType(), action)) { Logger.Warn("can't add consumer handler for {0}", consumer.GetType().ToString()); } Action <IConsumerConfiguration> consConf = configuration => configuration.WithPrefetchCount(exchangeItem.PrefetchCount); consumeInvokeParams.Add(consConf); //NOTE: Such ugly shit to get generic overloaded method var sbConsume = typeof(IAdvancedBus).GetMethods().Where(x => x.Name == "Consume").Select(m => new { Method = m, Params = m.GetParameters(), Args = m.GetGenericArguments() }).Where(x => x.Params.Length == 3 && x.Args.Length == 0 && x.Params[1].ParameterType.Name == action.GetType().Name) .Select(x => x.Method) .First(); sbConsume.Invoke(advancedBus, consumeInvokeParams.ToArray()); } }
private static void AddSenders(IAdvancedBus advancedBus, IExchange exchange, ExchangeElement exchangeItem, HashSet <IBusSender> set, ServiceBusSection configSection) { foreach (var outcome in exchangeItem.Out.Cast <OutgoingElement>()) { set.Add( new BusSender(EndpointInfoFactory.Create(advancedBus, exchange, outcome.Name, outcome.Key, outcome.Persist, outcome.Timeout)) { ExchangeType = exchangeItem.Type }); } }