/// <summary> /// Subscribes the specified handler. /// </summary> /// <typeparam name="T">事件参数类型</typeparam> /// <typeparam name="TH">The type of the th.</typeparam> /// <param name="handler">The handler.</param> public void Subscribe <T, TH>(Func <TH> handler) where TH : IIntegrationEventHandler <T> { var eventName = typeof(T).Name; var containsKey = _subsManager.HasSubscriptionsForEvent <T>(); if (!containsKey) { var channel = _consumerConnection.CreateConnect() as Consumer <Null, string>; channel.OnMessage += ConsumerClient_OnMessage; channel.Subscribe(eventName); } _subsManager.AddSubscription <T, TH>(handler, null); }
private void SubsManager_OnEventRemoved(object sender, ValueTuple <string, string> tuple) { if (!_consumerConnection.IsConnected) { _consumerConnection.TryConnect(); } using (var channel = _consumerConnection.CreateConnect() as Consumer <Null, string>) { channel.Unsubscribe(); if (_subsManager.IsEmpty) { _consumerConnection.Dispose(); } } }
public void Publish(IntegrationEvent @event) { if (!_producerConnection.IsConnected) { _producerConnection.TryConnect(); } var eventName = @event.GetType() .Name; var message = JsonConvert.SerializeObject(@event); var body = Encoding.UTF8.GetBytes(message); var policy = RetryPolicy.Handle <KafkaException>() .WaitAndRetry(5, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (ex, time) => { _logger.LogWarning(ex.ToString()); }); using (var conn = _producerConnection.CreateConnect() as Producer) { policy.Execute(() => { conn.ProduceAsync(eventName, null, body); }); } }
public void PublishTopic(MessageBase msg, string queueName, string borkerList) { if (!_producerConnection.IsConnected) { _producerConnection.TryConnect(); } var eventName = msg.GetType() .Name; Message <string, MessageBase> message = new Message <string, MessageBase>() { Key = eventName, Value = msg }; var policy = RetryPolicy.Handle <KafkaException>() .WaitAndRetry(5, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (ex, time) => { _logger.LogWarning(ex.ToString()); }); using (var conn = _producerConnection.CreateConnect() as IProducer <string, MessageBase>) { policy.Execute(() => { conn.ProduceAsync(eventName, message); }); } }
private void SubsManager_OnEventRemoved(object sender, string eventName) { if (!_consumerConnection.IsConnected) { _consumerConnection.TryConnect(); } using (var channel = _consumerConnection.CreateConnect() as IConsumer <string, MessageBase>) { channel.Unsubscribe(); if (_subsManager.IsEmpty) { _consumerConnection.Dispose(); } } }