예제 #1
0
        /// <summary>
        ///
        /// <para>CustomPublish:</para>
        ///
        /// <para>Publishes the given message to given custom topic</para>
        ///
        /// <para>Check <seealso cref="IBPubSubServiceInterface.CustomPublish"/> for detailed documentation</para>
        ///
        /// </summary>
        public bool CustomPublish(string _CustomTopic, string _CustomMessage, Action <string> _ErrorMessageAction = null)
        {
            if (_CustomTopic != null && _CustomTopic.Length > 0 &&
                _CustomMessage != null && _CustomMessage.Length > 0 &&
                BUtility.CalculateStringMD5(_CustomTopic, out string TopicMD5, _ErrorMessageAction))
            {
                if (EnsureQueueExists(TopicMD5, out string QueueUrl, _ErrorMessageAction))
                {
                    string TimestampHash = null;
                    UniqueMessageDeliveryEnsurer?.Publish_PrependTimestampToMessage(ref _CustomMessage, out TimestampHash);

                    try
                    {
                        if (UniqueMessageDeliveryEnsurer != null)
                        {
                            if (UniqueMessageDeliveryEnsurer.Publish_EnsureUniqueDelivery(_CustomTopic, TimestampHash, _ErrorMessageAction))
                            {
                                using (var SendMessageTask = SQSClient.SendMessageAsync(QueueUrl, _CustomMessage))
                                {
                                    SendMessageTask.Wait();
                                }
                            }
                            else
                            {
                                _ErrorMessageAction?.Invoke("BPubSubServiceAWS->CustomPublish: UniqueMessageDeliveryEnsurer has failed.");
                                return(false);
                            }
                        }
                        else
                        {
                            using (var SendMessageTask = SQSClient.SendMessageAsync(QueueUrl, _CustomMessage))
                            {
                                SendMessageTask.Wait();
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        _ErrorMessageAction?.Invoke("BPubSubServiceAWS->CustomPublish: " + e.Message + ", Trace: " + e.StackTrace);
                        if (e.InnerException != null && e.InnerException != e)
                        {
                            _ErrorMessageAction?.Invoke("BPubSubServiceAWS->CustomPublish->Inner: " + e.InnerException.Message + ", Trace: " + e.InnerException.StackTrace);
                        }
                        return(false);
                    }
                    return(true);
                }
            }
            return(false);
        }
예제 #2
0
        /// <summary>
        ///
        /// <para>CustomPublish:</para>
        ///
        /// <para>Publishes the given message to given custom topic</para>
        ///
        /// <para>Check <seealso cref="IBPubSubServiceInterface.CustomPublish"/> for detailed documentation</para>
        ///
        /// </summary>
        public bool CustomPublish(
            string _CustomTopic,
            string _CustomMessage,
            Action <string> _ErrorMessageAction = null)
        {
            if (_CustomTopic != null && _CustomTopic.Length > 0 &&
                _CustomMessage != null && _CustomMessage.Length > 0)
            {
                var CustomTopicGoogleFriendly = GetGoogleFriendlyTopicName(_CustomTopic);

                string TimestampHash = null;
                UniqueMessageDeliveryEnsurer?.Publish_PrependTimestampToMessage(ref _CustomMessage, out TimestampHash);

                ByteString MessageByteString = null;
                try
                {
                    MessageByteString = ByteString.CopyFromUtf8(_CustomMessage);
                }
                catch (Exception e)
                {
                    _ErrorMessageAction?.Invoke("BPubSubServiceGC->CustomPublish: " + e.Message + ", Trace: " + e.StackTrace);
                    return(false);
                }

                var TopicInstance = new TopicName(ProjectID, CustomTopicGoogleFriendly);

                var MessageContent = new PubsubMessage()
                {
                    Data = MessageByteString
                };

                if (GetPublisher(out PublisherServiceApiClient Client, TopicInstance, _ErrorMessageAction))
                {
                    try
                    {
                        if (UniqueMessageDeliveryEnsurer != null)
                        {
                            if (UniqueMessageDeliveryEnsurer.Publish_EnsureUniqueDelivery(_CustomTopic, TimestampHash, _ErrorMessageAction))
                            {
                                using (var CreatedTask = Client.PublishAsync(TopicInstance, new PubsubMessage[] { MessageContent }))
                                {
                                    CreatedTask.Wait();
                                }
                            }
                            else
                            {
                                _ErrorMessageAction?.Invoke("BPubSubServiceGC->CustomPublish: UniqueMessageDeliveryEnsurer has failed.");
                                return(false);
                            }
                        }
                        else
                        {
                            using (var CreatedTask = Client.PublishAsync(TopicInstance, new PubsubMessage[] { MessageContent }))
                            {
                                CreatedTask.Wait();
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        if (e is RpcException && (e as RpcException).Status.StatusCode == StatusCode.NotFound)
                        {
                            lock (LockablePublisherTopicDictionaryObject(_CustomTopic))
                            {
                                PublisherTopicDictionary.Remove(TopicInstance.TopicId);
                            }
                        }
                        else
                        {
                            _ErrorMessageAction?.Invoke("BPubSubServiceGC->CustomPublish: " + e.Message + ", Trace: " + e.StackTrace);
                        }
                        return(false);
                    }
                    return(true);
                }
            }
            return(false);
        }
예제 #3
0
        public bool CustomPublish(string _CustomTopic, string _CustomMessage, Action <string> _ErrorMessageAction = null)
        {
            if (_CustomTopic != null && _CustomTopic.Length > 0 &&
                _CustomMessage != null && _CustomMessage.Length > 0)
            {
                _CustomTopic = GetAzureFriendlyTopicName(_CustomTopic);

                if (EnsureTopicExists(_CustomTopic, out ITopicClient _TopicClient, _ErrorMessageAction))
                {
                    string TimestampHash = null;
                    UniqueMessageDeliveryEnsurer?.Publish_PrependTimestampToMessage(ref _CustomMessage, out TimestampHash);

                    try
                    {
                        if (UniqueMessageDeliveryEnsurer != null)
                        {
                            if (UniqueMessageDeliveryEnsurer.Publish_EnsureUniqueDelivery(_CustomTopic, TimestampHash, _ErrorMessageAction))
                            {
                                var AzureMessage = new Message(Encoding.UTF8.GetBytes(_CustomMessage));
                                AzureMessage.Label = _CustomTopic;
                                using (var SendMessageTask = _TopicClient.SendAsync(AzureMessage))
                                {
                                    SendMessageTask.Wait();
                                }
                            }
                            else
                            {
                                _ErrorMessageAction?.Invoke("BPubSubServiceAzure->CustomPublish: UniqueMessageDeliveryEnsurer has failed.");
                                return(false);
                            }
                        }
                        else
                        {
                            var AzureMessage = new Message(Encoding.UTF8.GetBytes(_CustomMessage));
                            AzureMessage.Label = _CustomTopic;
                            using (var SendMessageTask = _TopicClient.SendAsync(AzureMessage))
                            {
                                SendMessageTask.Wait();
                            }
                        }
                        return(true);
                    }
                    catch (Exception e)
                    {
                        _ErrorMessageAction?.Invoke("BPubSubServiceAzure->CustomPublish: " + e.Message + ", Trace: " + e.StackTrace);
                        if (e.InnerException != null && e.InnerException != e)
                        {
                            _ErrorMessageAction?.Invoke("BPubSubServiceAzure->CustomPublish->Inner: " + e.InnerException.Message + ", Trace: " + e.InnerException.StackTrace);
                        }
                        return(false);
                    }
                    finally
                    {
                        using (var CloseTask = _TopicClient.CloseAsync())
                        {
                            CloseTask.Wait();
                        }
                    }
                }
            }
            return(false);
        }