/// <summary>
        /// Adds and configures the consistence services for the consistency.
        /// </summary>
        /// <param name="services">The services available in the application.</param>
        /// <param name="setupAction">An action to configure the <see cref="CapOptions" />.</param>
        /// <returns>An <see cref="CapBuilder" /> for application services.</returns>
        public static InitQBuilder AddInitQ(this IServiceCollection services, Action <InitQOptions> setupAction)
        {
            if (setupAction == null)
            {
                throw new ArgumentNullException(nameof(setupAction));
            }
            var options = new InitQOptions();

            setupAction(options);


            services.Configure(setupAction);

            services.AddSingleton(typeof(ICacheService), new RedisCacheService(options.ConnectionString));

            services.AddHostedService <HostedService>();


            foreach (var item in options.ListSubscribe)
            {
                services.TryAddSingleton(item);
            }

            return(new InitQBuilder(services));
        }
示例#2
0
        public void FindInterfaceTypes(IServiceProvider provider, InitQOptions options)
        {
            var executorDescriptorList = new List <ConsumerExecutorDescriptor>();

            using (var scoped = provider.CreateScope())
            {
                var scopedProvider   = scoped.ServiceProvider;
                var consumerServices = scopedProvider.GetServices <IRedisSubscribe>();
                foreach (var service in consumerServices)
                {
                    var typeInfo = service.GetType().GetTypeInfo();
                    if (!typeof(IRedisSubscribe).GetTypeInfo().IsAssignableFrom(typeInfo))
                    {
                        continue;
                    }
                    executorDescriptorList.AddRange(GetTopicAttributesDescription(typeInfo));
                }
                Send(executorDescriptorList, provider, options);
            }
        }
示例#3
0
 private void Send(IEnumerable <ConsumerExecutorDescriptor> ExecutorDescriptorList, IServiceProvider serviceProvider, InitQOptions options)
 {
     foreach (var ConsumerExecutorDescriptor in ExecutorDescriptorList)
     {
         //线程
         Task.Run(() =>
         {
             using (var scope = serviceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope())
             {
                 var publish  = ConsumerExecutorDescriptor.Attribute.Name;
                 var provider = scope.ServiceProvider;
                 var obj      = ActivatorUtilities.GetServiceOrCreateInstance(provider, ConsumerExecutorDescriptor.ImplTypeInfo);
                 ParameterInfo[] parameterInfos = ConsumerExecutorDescriptor.MethodInfo.GetParameters();
                 //redis对象
                 var _redis = scope.ServiceProvider.GetService <ICacheService>();
                 while (true)
                 {
                     try
                     {
                         if (options.ShowLog)
                         {
                             Console.WriteLine($"执行方法:{obj.ToString()},key:{publish},执行时间{DateTime.Now}");
                         }
                         var count = _redis.ListLength(publish);
                         if (count > 0)
                         {
                             //从MQ里获取一条消息
                             var res = _redis.ListRightPop(publish);
                             //堵塞
                             Thread.Sleep(options.IntervalTime);
                             try
                             {
                                 Task.Run(() =>
                                 {
                                     if (parameterInfos.Length == 0)
                                     {
                                         ConsumerExecutorDescriptor.MethodInfo.Invoke(obj, null);
                                     }
                                     else
                                     {
                                         object[] parameters = new object[] { res };
                                         ConsumerExecutorDescriptor.MethodInfo.Invoke(obj, parameters);
                                     }
                                 });
                             }
                             catch (Exception ex)
                             {
                                 Console.WriteLine(ex.Message);
                             }
                         }
                         else
                         {
                             //线程挂起1s
                             Thread.Sleep(options.SuspendTime);
                         }
                     }
                     catch (Exception ex)
                     {
                         Console.WriteLine(ex.Message);
                     }
                 }
             }
         });
     }
 }