Exemple #1
0
        Task PublishMessage(MulticastTransportOperation transportOperation)
        {
            var messageWrapper = BuildMessageWrapper(transportOperation, TimeSpan.MaxValue,
                                                     transportOperation.MessageType.ToString());

            var topicsToSendTo = SubscriptionManager.GetTypeHierarchy(transportOperation.MessageType);

            var messageStream = new MemoryStream();

            KafkaTransportInfrastructure.GetSerializer().Serialize(messageWrapper, messageStream);

            var publishTasks = new List <Task>(topicsToSendTo.Count);

            foreach (var t in topicsToSendTo)
            {
                Logger.Debug("Publish to " + t);
                var topic = producerFactory.GetProducer().Topic(t);

                publishTasks.Add(topic.Produce(messageStream.ToArray())
                                 .ContinueWith(
                                     result => Logger.Info("new partition and offset: " + result.Result.Partition + " " +
                                                           result.Result.Offset), TaskContinuationOptions.ExecuteSynchronously));
            }

            return(Task.WhenAll(publishTasks));
        }
Exemple #2
0
        Task SendMessage(UnicastTransportOperation transportOperation)
        {
            Logger.Debug("Send to " + transportOperation.Destination);

            var toBeReceived     = transportOperation.GetTimeToBeReceived();
            var timeToBeReceived = toBeReceived.HasValue && toBeReceived.Value < TimeSpan.MaxValue
                ? toBeReceived
                : null;

            if (timeToBeReceived != null && timeToBeReceived.Value == TimeSpan.Zero)
            {
                var messageType = transportOperation.Message.Headers[Headers.EnclosedMessageTypes].Split(',').First();
                Logger.WarnFormat("TimeToBeReceived is set to zero for message of type '{0}'. Cannot send operation.",
                                  messageType);
                return(Task.FromResult(0));
            }

            // TimeToBeReceived was not specified on message - go for maximum set by SDK
            if (timeToBeReceived == TimeSpan.MaxValue)
            {
                timeToBeReceived = null;
            }

            var messageWrapper = BuildMessageWrapper(transportOperation, toBeReceived, transportOperation.Destination);

            var topic = producerFactory.GetProducer().Topic(transportOperation.Destination);

            var messageStream = new MemoryStream();

            KafkaTransportInfrastructure.GetSerializer().Serialize(messageWrapper, messageStream);

            return(topic.Produce(messageStream.ToArray()));
        }
        internal static MessageWrapper UnWrap(this Message kafkaMessage)
        {
            MessageWrapper m;

            using (var stream = new MemoryStream(kafkaMessage.Payload))
            {
                m = KafkaTransportInfrastructure.GetSerializer().Deserialize(stream);
            }

            if (m == null)
            {
                throw new ArgumentNullException(nameof(kafkaMessage));
            }

            if (m.ReplyToAddress != null)
            {
                m.Headers[Headers.ReplyToAddress] = m.ReplyToAddress;
            }
            m.Headers[Headers.CorrelationId] = m.CorrelationId;

            if (m.TimeToBeReceived != TimeSpan.MaxValue)
            {
                m.Headers[Headers.TimeToBeReceived] = m.TimeToBeReceived.ToString();
            }
            m.Headers[Headers.MessageIntent] = m.MessageIntent.ToString(); // message intent extension method

            return(m);
        }
        async Task PublishMessage(MulticastTransportOperation transportOperation)
        {
            var messageWrapper = BuildMessageWrapper(transportOperation, TimeSpan.MaxValue, transportOperation.MessageType.ToString());

            var topicsToSendTo = SubscriptionManager.GetTypeHierarchy(transportOperation.MessageType);

            var messageStream = new MemoryStream();

            KafkaTransportInfrastructure.GetSerializer().Serialize(messageWrapper, messageStream);

            foreach (var t in topicsToSendTo)
            {
                Logger.Debug("Publish to " + t);
                var topic = producerFactory.GetProducer().Topic(t);

                await topic.Produce(messageStream.ToArray()).ContinueWith(result => Logger.Info("new partition and offset: " + result.Result.Partition + " " + result.Result.Offset));
            }
        }