Exemple #1
0
        /// <summary>
        /// Creates a new handler for handling the messages of the specified subscription
        /// </summary>
        /// <param name="subscriptionId">The id of the subscription to handle the messages for</param>
        /// <returns>A new handler for handling the messages of the specified subscription</returns>
        protected virtual EventHandler <StanMsgHandlerArgs> CreateEventHandlerFor(string subscriptionId)
        {
            return(async(sender, e) =>
            {
                this.Logger.LogInformation("An event with subject '{subject}' has been received from the underlying NATS Streaming sink.", e.Message.Subject);
                this.Logger.LogInformation("Dispatching the resulting cloud event with subject '{subject}' to the gateway...", e.Message.Subject);
                CloudEvent cloudEvent = CloudEventBuilder.FromEvent(this.EventFormatter.DecodeJObject(JsonConvert.DeserializeObject <JObject>(Encoding.UTF8.GetString(e.Message.Data))))
                                        .WithSubscriptionId(subscriptionId)
                                        .WithSequence(e.Message.Sequence)
                                        .HasBeenRedelivered(e.Message.Redelivered)
                                        .Build();
                CloudEventContent cloudEventContent = new CloudEventContent(cloudEvent, ContentMode.Structured, new JsonEventFormatter());
                using (HttpResponseMessage response = await this.HttpClient.PostAsync("pub", cloudEventContent))
                {
                    string content = await response.Content?.ReadAsStringAsync();

                    if (!response.IsSuccessStatusCode)
                    {
                        this.Logger.LogError($"An error occured while dispatching a cloud event of type '{{eventType}}' to the eventing controller: the remote server response with a '{{statusCode}}' status code.{Environment.NewLine}Details: {{responseContent}}", cloudEvent.Type, response.StatusCode, content);
                        response.EnsureSuccessStatusCode();
                    }
                }
                this.Logger.LogInformation("The cloud event with subject '{subject}' has been successfully dispatched to the gateway.", e.Message.Subject);
                e.Message.Ack();
            });
        }
Exemple #2
0
        /// <summary>
        /// Dispatches the <see cref="ResolvedEvent"/> to the <see cref="CloudEvent"/> gateway
        /// </summary>
        /// <param name="subscriptionId">The id of the subscription the <see cref="CloudEvent"/> originates from</param>
        /// <param name="e">The <see cref="ResolvedEvent"/> to dispatch</param>
        /// <returns>A new awaitable <see cref="Task"/></returns>
        protected virtual async Task DispatchEventAsync(string subscriptionId, ResolvedEvent e)
        {
            try
            {
                this.Logger.LogInformation("An event has been received from the underlying EventStore sink.");
                this.Logger.LogInformation("Dispatching the resulting cloud event to the gateway...");
                CloudEvent cloudEvent = CloudEventBuilder.FromEvent(this.EventFormatter.DecodeJObject(JsonConvert.DeserializeObject <JObject>(Encoding.UTF8.GetString(e.Event.Data))))
                                        .WithSubscriptionId(subscriptionId)
                                        .WithSequence((ulong)e.Event.EventNumber)
                                        .Build();
                CloudEventContent cloudEventContent = new CloudEventContent(cloudEvent, ContentMode.Structured, new JsonEventFormatter());
                using (HttpResponseMessage response = await this.HttpClient.PostAsync("pub", cloudEventContent))
                {
                    string content = await response.Content?.ReadAsStringAsync();

                    if (!response.IsSuccessStatusCode)
                    {
                        this.Logger.LogError($"An error occured while dispatching a cloud event of type '{{eventType}}' to the eventing controller: the remote server response with a '{{statusCode}}' status code.{Environment.NewLine}Details: {{responseContent}}", cloudEvent.Type, response.StatusCode, content);
                        response.EnsureSuccessStatusCode();
                    }
                }
                this.Logger.LogInformation("The cloud event has been successfully dispatched to the gateway.");
            }
            catch (HttpRequestException)
            {
                throw;
            }
            catch (Exception ex)
            {
                this.Logger.LogError($"An error occured while dispatching a cloud event to the gateway.{Environment.NewLine}Details: {{ex}}", ex.Message);
                throw;
            }
        }