public async Task SendAsync(string messageId, string topicName, object bodyObj)
        {
            string body;

            if (bodyObj != null && Helper.IsComplexType(bodyObj.GetType()))
            {
                body = _contentSerializer.Serialize(bodyObj);
            }
            else
            {
                body = bodyObj?.ToString();
            }

            _logger.LogDebug($"Callback message will publishing, name:{topicName},content:{body}");

            var callbackMessage = new CapMessageDto
            {
                Id      = messageId,
                Content = body
            };

            var content = _messagePacker.Pack(callbackMessage);

            var publishedMessage = new CapPublishedMessage
            {
                Id         = SnowflakeId.Default().NextId(),
                Name       = topicName,
                Content    = content,
                StatusName = StatusName.Scheduled
            };

            using (var scope = _serviceProvider.CreateScope())
            {
                var provider          = scope.ServiceProvider;
                var callbackPublisher = provider.GetService <ICallbackPublisher>();
                await callbackPublisher.PublishCallbackAsync(publishedMessage);
            }
        }
Exemple #2
0
        public void Publish <T>(string name, T value, IDictionary <string, string> headers)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (headers == null)
            {
                headers = new Dictionary <string, string>();
            }

            var messageId = SnowflakeId.Default().NextId().ToString();

            headers.Add(Headers.MessageId, messageId);
            headers.Add(Headers.MessageName, name);
            headers.Add(Headers.Type, typeof(T).FullName);
            headers.Add(Headers.SentTime, DateTimeOffset.Now.ToString());
            if (!headers.ContainsKey(Headers.CorrelationId))
            {
                headers.Add(Headers.CorrelationId, messageId);
                headers.Add(Headers.CorrelationSequence, 0.ToString());
            }

            var message = new Message(headers, value);

            long?tracingTimestamp = null;

            try
            {
                tracingTimestamp = TracingBefore(message);

                if (Transaction.Value?.DbTransaction == null)
                {
                    var mediumMessage = _storage.StoreMessage(name, message);

                    TracingAfter(tracingTimestamp, message);

                    _dispatcher.EnqueueToPublish(mediumMessage);
                }
                else
                {
                    var transaction = (CapTransactionBase)Transaction.Value;

                    var mediumMessage = _storage.StoreMessage(name, message, transaction.DbTransaction);

                    TracingAfter(tracingTimestamp, message);

                    transaction.AddToSent(mediumMessage);

                    if (transaction.AutoCommit)
                    {
                        transaction.Commit();
                    }
                }
            }
            catch (Exception e)
            {
                TracingError(tracingTimestamp, message, e);

                throw;
            }
        }