Beispiel #1
0
        public RedisMessageBus(IServiceProvider serviceProvider, ILogger <RedisMessageBus> logger
                               , RedisMessageBusOptions options
                               , RedisStorage redisStorage)
        {
            _serviceProvider = serviceProvider;
            _logger          = logger;
            _options         = options;
            _redisStorage    = redisStorage;


            _backgroundProcessContext = new BackgroundProcessContext();
            _processExecuter          = new ProcessExecuter(_serviceProvider, _backgroundProcessContext);
        }
        public static string GetDelayTopic(RedisMessageBusOptions options, string key = null)
        {
            var count  = Math.Abs(Interlocked.Increment(ref DelayTaskIndex));
            var result = GetDelayTopicList(options);

            if (string.IsNullOrEmpty(key))
            {
                return(result[count % result.Count]);
            }
            else
            {
                return(result[Math.Abs(key.GetHashCode()) % result.Count]);
            }
        }
 public static List <string> GetDelayTopicList(RedisMessageBusOptions options)
 {
     if (DelayTopicList != null)
     {
         return(DelayTopicList);
     }
     DelayTopicList = new List <string>();
     //return $"{options.TopicPrefix}delay:jobid";
     for (int i = 0; i < options.DelayTopicCount; i++)
     {
         DelayTopicList.Add($"{options.TopicPrefix}delay{i}");
     }
     return(DelayTopicList);
 }
Beispiel #4
0
        private static IServiceCollection AddService(IServiceCollection services, RedisMessageBusOptions options)
        {
            if (options.ConnectionMultiplexer != null)
            {
                services.AddSingleton(options.ConnectionMultiplexer);
            }
            else if (!string.IsNullOrEmpty(options.ConnectionString))
            {
                var redis = ConnectionMultiplexer.Connect(options.ConnectionString);
                services.AddSingleton(redis);
            }
            else
            {
                throw new Exception("ConnectionMultiplexer或RedisConnectionString为空");
            }

            services.AddSingleton(options);
            services.AddSingleton <RedisStorage>();
            return(services);
        }
        public static string GetTopic(RedisMessageBusOptions options, Type type)
        {
            string topicName = null;

            if (TopicCache.TryGetValue(type, out topicName))
            {
                return(topicName);
            }

            topicName = type.Name;//默认等于该类型的名称
            var topicAttr = TopicAttribute.GetTopicAttribute(type);

            if (topicAttr != null && !string.IsNullOrEmpty(topicAttr.Name))
            {
                topicName = topicAttr.Name;
            }

            topicName = $"{options.TopicPrefix ?? ""}{topicName}";

            TopicCache.TryAdd(type, topicName);

            return(topicName);
        }
 public static string GetDelayChannel(RedisMessageBusOptions options)
 {
     return($"{options.TopicPrefix}DelayJobChannel");
 }
 public static string GetDelaySortedSetName(RedisMessageBusOptions options)
 {
     return($"{options.TopicPrefix}delay:jobid");
 }
 public static string GetJobHashId(RedisMessageBusOptions options, string jobId)
 {
     return($"{options.TopicPrefix}jobdata:{jobId}");
 }
Beispiel #9
0
 public static IServiceCollection AddRedisMessageBusPubSub(this IServiceCollection services, RedisMessageBusOptions options)
 {
     AddService(services, options);
     services.AddSingleton <IRedisMessageBus, RedisMessageBus_Subscriber>();
     return(services);
 }
Beispiel #10
0
        public static IServiceCollection AddRedisMessageBus(this IServiceCollection services, RedisMessageBusOptions options)
        {
            //验证
            AssertUtils.IsTrue(options.DelayTopicCount >= 1, "RedisMessageBus 参数DelayTopicCount配置错误");

            services.AddAddMultithreadExecutor(options.DefaultConsumerThreadCount);
            AddService(services, options);
            services.AddSingleton <IRedisMessageBus, RedisMessageBus>();
            return(services);
        }
Beispiel #11
0
 public RedisMessageBus_Subscriber(ILogger <RedisMessageBus_Subscriber> logger, RedisMessageBusOptions options, ConnectionMultiplexer connectionMultiplexer)
 {
     _logger  = logger;
     _options = options;
     _connectionMultiplexer = connectionMultiplexer;
     _subscriber            = _connectionMultiplexer.GetSubscriber();
 }