public async Task SubscribeAsync <T>(Func <T, SubscribeContext, Task> handler, SubscribeOptions subscribeOptions = null, CancellationToken cancellationToken = default(CancellationToken)) where T : class { string topic = GetTopic(typeof(T)); var groupId = subscribeOptions?.GroupId; groupId = !string.IsNullOrEmpty(groupId) ? groupId : _kafkaOptions.ConsumerConfig.GroupId; var threadCount = subscribeOptions?.ConsumerThreadCount ?? 0; threadCount = threadCount > 0 ? threadCount : _kafkaOptions.DefaultConsumerThreadCount; AssertUtils.IsTrue(threadCount > 0, "消费者线程数必须大于0"); ValidateSubscribe(topic, groupId); _logger.LogInformation($"订阅Topic:{topic},GroupId:{groupId},ConsumerThreadCount:{threadCount}"); for (int i = 0; i < threadCount; i++) { var consumer = new KafkaConsumer <string, KafkaMessageBusData>(_serviceProvider); consumer.OnMessage += consumeResult => { return(With.NoException(_logger, async() => { var obj = _kafkaOptions.Serializer.Deserialize <T>(consumeResult.Message.Value.Data); var subscribeContext = new SubscribeContext { Topic = topic, GroupId = groupId }; await handler(obj, subscribeContext); }, $"消费数据{consumeResult.Message.Value.Topic}")); }; _consumerList.Add(consumer); await consumer.Subscribe(topic, groupId, cancellationToken); } }
public static void SubscribeControl(WindowsFormsHost glControlHost) { Exceptions.CheckArgumentNull(glControlHost, "glControlHost"); GLControl glControl = glControlHost.Child as GLControl; if (glControl == null) { throw new ArgumentException("glControlHost"); } SubscribeContext subscribeContext = new SubscribeContext { OnInvalidateControlRequestRecived = () => glControl.Invalidate(), OnViewportDesiredSizeChanged = (dw, dh) => glControlHost.Dispatcher.BeginInvoke(new Action(() => { glControlHost.Width = dw; glControlHost.Height = dh; glControl.Width = dw; glControl.Height = dh; })) }; if (!SubscribeContexts.TryAdd(glControlHost, subscribeContext)) { throw new Exception("Элемент управления уже зарегистрирован."); } InvalidateControlRequestRecived += subscribeContext.OnInvalidateControlRequestRecived; ViewportDesiredSizeChanged += subscribeContext.OnViewportDesiredSizeChanged; glControl.Invalidate(); }
public void ShouldShallowCloneContextBag() { var context = new ContextBag(); context.Set("someKey", "someValue"); var testee = new SubscribeContext(new RootContext(null, null, null), typeof(object), context); testee.Extensions.Set("someKey", "updatedValue"); testee.Extensions.Set("anotherKey", "anotherValue"); string value; string anotherValue; context.TryGet("someKey", out value); Assert.AreEqual("someValue", value); Assert.IsFalse(context.TryGet("anotherKey", out anotherValue)); string updatedValue; string anotherValue2; testee.Extensions.TryGet("someKey", out updatedValue); testee.Extensions.TryGet("anotherKey", out anotherValue2); Assert.AreEqual("updatedValue", updatedValue); Assert.AreEqual("anotherValue", anotherValue2); }
private static void RemoveSubscriber(string username) { username = username.ToLower(); if (_Subscribers.ContainsKey(username)) { SubscribeContext outObj = null; _Subscribers.TryRemove(username, out outObj); } }
public void ShouldShallowCloneContextBag() { var context = new ContextBag(); context.Set("someKey", "someValue"); var testee = new SubscribeContext(new FakeRootContext(), new Type[0], context); testee.Extensions.Set("someKey", "updatedValue"); testee.Extensions.Set("anotherKey", "anotherValue"); context.TryGet("someKey", out string value); Assert.AreEqual("someValue", value); Assert.IsFalse(context.TryGet("anotherKey", out string _)); testee.Extensions.TryGet("someKey", out string updatedValue); testee.Extensions.TryGet("anotherKey", out string anotherValue2); Assert.AreEqual("updatedValue", updatedValue); Assert.AreEqual("anotherValue", anotherValue2); }
public HomeController(SubscribeContext context) { _context = context; }
public static void SubscribeControl(WindowsFormsHost glControlHost) { Exceptions.CheckArgumentNull(glControlHost, "glControlHost"); GLControl glControl = glControlHost.Child as GLControl; if (glControl == null) throw new ArgumentException("glControlHost"); SubscribeContext subscribeContext = new SubscribeContext { OnInvalidateControlRequestRecived = () => glControl.Invalidate(), OnViewportDesiredSizeChanged = (dw, dh) => glControlHost.Dispatcher.BeginInvoke(new Action(() => { glControlHost.Width = dw; glControlHost.Height = dh; glControl.Width = dw; glControl.Height = dh; })) }; if (!SubscribeContexts.TryAdd(glControlHost, subscribeContext)) throw new Exception("Элемент управления уже зарегистрирован."); InvalidateControlRequestRecived += subscribeContext.OnInvalidateControlRequestRecived; ViewportDesiredSizeChanged += subscribeContext.OnViewportDesiredSizeChanged; glControl.Invalidate(); }