Example #1
0
        public void Install(IWindsorContainer container, IConfigurationStore store)
        {
            //全局服务注册表
            ServiceBusSection serviceBusSection = (ServiceBusSection)ConfigurationManager.GetSection("serviceBus");

            container.Register(Component.For <ServiceBusRegistry>().LifestyleSingleton());
            var busRegistry = container.Resolve <ServiceBusRegistry>();

            this.serviceBusRegistry = busRegistry.Initital(serviceBusSection);

            //ServiceBus初始化
            container.Register(
                Component.For <ServiceBus>().UsingFactoryMethod(() =>
            {
                ServiceBus.Instance.IocResolver        = iocResolver;
                ServiceBus.Instance.Serializer         = container.Resolve <Serialization.ISerializer>();
                ServiceBus.Instance.Logger             = iocResolver.Resolve <ILogger>();
                ServiceBus.Instance.PerformanceCounter = iocResolver.Resolve <WindPerformanceCounter>();
                return(ServiceBus.Instance);
            }).LifestyleSingleton());
            this.serviceBus = container.Resolve <ServiceBus>();

            //获取服务程序集模块
            var moduleManager = iocResolver.Resolve <IWindModuleManager>();

            foreach (var windModuleInfo in moduleManager.Modules)
            {
                var serviceAssembAttr = ReflectionHelper.GetSingleAttributeOrDefault <ServiceAssemblyNameAttribute>(windModuleInfo.Type);
                if (serviceAssembAttr != null)
                {
                    string serviceAssemblyName = serviceAssembAttr.ServiceAssemblyName;
                    serviceAssemblyDic.Add(serviceAssemblyName, windModuleInfo.Assembly);

                    // 尝试注册未在配置文件中配置的本地程序集!!!
                    busRegistry.RegisterServiceAssembly(serviceAssemblyName);
                }
            }

            container.Kernel.ComponentRegistered += Kernel_ComponentRegistered;
        }
 protected ServiceBusSection GetServiceBusSection(string file)
 {
     return(ServiceBusSection.Open(string.Format(@".\ServiceBusSection\files\{0}", file)));
 }
Example #3
0
 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
         });
     }
 }
Example #4
0
        public static PlatformBusBuilder Create(IDependencyResolver resolver)
        {
            ServiceBusSection section = ServiceBusSection.Current;
            var bus = RabbitHutch.CreateBus(section.ConnectionString,
                                            serviceRegister =>
            {
                serviceRegister.Register <ITypeNameSerializer>(
                    serviceProvider => new PlatformTypeNameSerializer());
                serviceRegister.Register <IEasyNetQLogger>(serviceProvider => new PlatformLogger());
                serviceRegister.Register <IConventions>(
                    serviceProvider => new PlatformConventions(serviceProvider.Resolve <ITypeNameSerializer>()));
                serviceRegister.Register <IConsumerErrorStrategy>(
                    serviceProvider =>
                    new ConsumerErrorStrategy(serviceProvider.Resolve <IConnectionFactory>(),
                                              serviceProvider.Resolve <ISerializer>(), serviceProvider.Resolve <IEasyNetQLogger>(),
                                              serviceProvider.Resolve <IConventions>(), serviceProvider.Resolve <ITypeNameSerializer>()));
            });
            var advancedBus = bus.Advanced;

            advancedBus.MessageReturned += (bytes, properties, arg3) =>
                                           Logger.Error("Message was returned to sender with reason :[{0}].Message : [{1}]", arg3, Encoding.UTF8.GetString(bytes));

            var set = new HashSet <IBusSender>();

            var platformBus = new PlatformBus(set, advancedBus, resolver, section.SendRetryEnabled);

            IList <Action> consumeActions = new List <Action>();

            //TODO: NOTE: Or get it from DB!
            foreach (var exchangeItem in section.Exchanges.Cast <ExchangeElement>())
            {
                var rabbitExchange   = advancedBus.ExchangeDeclare(exchangeItem.Name, exchangeItem.Type);
                var platformExchange = GetPlatformExchangeFromConfig(exchangeItem);
                platformExchange.RabbitExchange = rabbitExchange;
                platformBus.Exchanges.Add(platformExchange);

                consumeActions.Add(() => CreateConsumers(resolver, advancedBus, platformExchange));
                AddSenders(advancedBus, rabbitExchange, exchangeItem, set, section);
            }

            lock (lockObj)
            {
                RedButton = () =>
                {
                    if (!started)
                    {
                        lock (lockObj)
                        {
                            if (!started)
                            {
                                foreach (var consumeAction in consumeActions)
                                {
                                    consumeAction.Invoke();
                                }

                                started = true;
                            }
                        }
                    }
                };
            }

            return(new PlatformBusBuilder(platformBus));
        }
        /// <summary>
        /// 根据配置文件初始化注册表
        /// </summary>
        /// <param name="serviceBusSection">配置节</param>
        public ServiceBusRegistry Initital(ServiceBusSection serviceBusSection)
        {
            if (serviceBusSection == null)
            {
                throw new ArgumentNullException("serviceBusSection");
            }

            lock (locker)
            {
                serviceBusDic.Clear();
                serviceAssemblyDic.Clear();

                //初始化serviceBus列表
                foreach (ServiceBusItemSection busSection in serviceBusSection.BusGroup)
                {
                    string serviceBusName        = busSection.Name.Trim();
                    ServiceBusServerInfo busInfo = new ServiceBusServerInfo(serviceBusName,
                                                                            //expo config
                                                                            new ServiceBusServerInfo.ExpoServer(
                                                                                busSection.ExpoServer.AppClassId,
                                                                                busSection.ExpoServer.CommandId,
                                                                                busSection.ExpoServer.CommandTimeout,
                                                                                busSection.ExpoServer.IsStart)
                                                                            //TODO: rabbitmq config...
                                                                            );
                    bool isSuccess = serviceBusDic.TryAdd(serviceBusName, busInfo);
                    if (isSuccess)
                    {
                        //当前ServiceBus
                        if (busSection.IsCurrent)
                        {
                            this.LocalBusServer = busInfo;
                        }
                    }
                    else
                    {
                        //TODO: serviceBus名称冲突处理
                        this.Logger.Error(string.Format("ServiceBus:[{0}] duplicated in configuration!", serviceBusName));
                    }
                }

                if (this.LocalBusServer == null)
                {
                    throw new WindServiceBusException("Can not located current busServer! Please set isCurrent attribute on busServer section.");
                }

                //初始化windService列表
                //遍历每个servicebus
                foreach (ServiceCollectionSection busServiceCollection in serviceBusSection.ServiceGroup)
                {
                    string serviceBusName = busServiceCollection.Name.Trim();

                    ServiceBusServerInfo busInfo = null;
                    bool isServiceBusExists      = this.serviceBusDic.TryGetValue(serviceBusName, out busInfo);
                    if (isServiceBusExists)
                    {
                        //遍历每个服务程序集申明
                        foreach (ServiceItemSection serviceAssemblyItem in busServiceCollection)
                        {
                            string serviceAssemblyName = serviceAssemblyItem.Name.Trim();
                            this.RegisterServiceAssembly(serviceAssemblyName, busInfo);
                        }
                    }
                    else
                    {
                        //TODO: 未找到serviceBus名称
                        this.Logger.Error(string.Format("ServiceBus:[{0}] not found in configuration!", serviceBusName));
                    }
                }
            }
            return(this);
        }