Пример #1
0
        public void Publish <TEvent>(TEvent @event, Dictionary <string, string> tags = null) where TEvent : notnull
        {
            using (var activity = _activitySource?.StartActivity("Send PubSub event to KubeMQ", ActivityKind.Producer))
            {
                if (typeof(TEvent) is IEnumerable)
                {
                    throw new ArgumentException("Collection are not accepted!");
                }

                var definition = GetDefiniton(typeof(TEvent));

                var channel = CreateChannel(definition);

                var serializer = GetSerializer(definition);

                string activityId = _applicationContext?.Principal?.ActivityID.ToString() ?? Guid.NewGuid().ToString();

                KubeMQEvent message = null;
                using (var serializerActivity = _activitySource?.StartActivity("Serialize.", ActivityKind.Producer))
                    message = new KubeMQEvent(null, string.Empty, serializer.Serialize(@event).ToArray(), BuildTags(typeof(TEvent), tags, activityId));

                using (var sendActivity = _activitySource?.StartActivity("Send to KubeMQ", ActivityKind.Producer))
                    channel.SendEvent(message);
            }
        }
Пример #2
0
        private async Task PublishBatchAsync(ChannelParameter channelParameter, IEnumerable <PubSubEvent> events)
        {
            using (var sendActivity = _activitySource?.StartActivity("Send Pub Sub events to KubeMQ", ActivityKind.Producer))
            {
                var definition = channelParameter as PubSubDefinition;

                if (null == definition)
                {
                    throw new InvalidCastException("The channel parameter is not a Publisher one.");
                }

                var channel = CreateChannel(definition);

                sendActivity?.AddTag("Address", definition.Address);

                var serializer = GetSerializer(definition);

                string activityId = _applicationContext?.Principal?.ActivityID.ToString() ?? Guid.NewGuid().ToString();

                using (var serializerActivity = _activitySource?.StartActivity("Serialize.", ActivityKind.Producer))
                    foreach (var @event in events)
                    {
                        var message = new KubeMQEvent(null, string.Empty, serializer.Serialize(@event.Value).ToArray(), BuildTags(@event.Value.GetType(), @event.Tags, activityId));

                        await channel.StreamEvent(message);
                    }

                await channel.ClosesEventStreamAsync();
            }
        }