public When_a_user_handler_is_failed() { consumerErrorStrategy = Substitute.For <IConsumerErrorStrategy>(); consumerErrorStrategy.HandleConsumerError(null, null).ReturnsForAnyArgs(AckStrategies.Ack); var handlerRunner = new HandlerRunner(consumerErrorStrategy); var consumer = Substitute.For <IBasicConsumer>(); channel = Substitute.For <IModel>(); consumer.Model.Returns(channel); context = new ConsumerExecutionContext( async(body, properties, info) => throw new Exception(), messageInfo, messageProperties, messageBody ); var handlerTask = handlerRunner.InvokeUserMessageHandlerAsync(context) .ContinueWith(async x => { var ackStrategy = await x.ConfigureAwait(false); return(ackStrategy(channel, 42)); }, TaskContinuationOptions.ExecuteSynchronously) .Unwrap(); if (!handlerTask.Wait(5000)) { throw new TimeoutException(); } }
protected virtual AckStrategy GetAckStrategy(ConsumerExecutionContext context, Task task) { try { if (task.IsFaulted) { logger.Error( task.Exception, "Exception thrown by subscription callback, receivedInfo={receivedInfo}, properties={properties}, message={message}", context.Info, context.Properties, Convert.ToBase64String(context.Body) ); return(consumerErrorStrategy.HandleConsumerError(context, task.Exception)); } if (task.IsCanceled) { return(consumerErrorStrategy.HandleConsumerCancelled(context)); } return(AckStrategies.Ack); } catch (Exception exception) { logger.Error(exception, "Consumer error strategy has failed"); return(AckStrategies.NackWithRequeue); } }
private async Task <AckStrategy> InvokeUserMessageHandlerInternalAsync( ConsumerExecutionContext context, CancellationToken cancellationToken ) { try { try { return(await context.Handler( context.Body, context.Properties, context.ReceivedInfo, cancellationToken ).ConfigureAwait(false)); } catch (OperationCanceledException) { return(consumerErrorStrategy.HandleConsumerCancelled(context)); } catch (Exception exception) { return(consumerErrorStrategy.HandleConsumerError(context, exception)); } } catch (Exception exception) { logger.Error(exception, "Consumer error strategy has failed"); return(AckStrategies.NackWithRequeue); } }
private async Task <AckStrategy> InvokeUserMessageHandlerInternalAsync(ConsumerExecutionContext context) { try { try { await context.UserHandler(context.Body, context.Properties, context.Info).ConfigureAwait(false); } catch (OperationCanceledException) { return(consumerErrorStrategy.HandleConsumerCancelled(context)); } catch (Exception exception) { logger.Error( exception, "Exception thrown by subscription callback, receivedInfo={receivedInfo}, properties={properties}, message={message}", context.Info, context.Properties, Convert.ToBase64String(context.Body) ); return(consumerErrorStrategy.HandleConsumerError(context, exception)); } } catch (Exception exception) { logger.Error(exception, "Consumer error strategy has failed"); return(AckStrategies.NackWithRequeue); } return(AckStrategies.Ack); }
private void HandleErrorInSubscriptionHandler( BasicDeliverEventArgs basicDeliverEventArgs, SubscriptionInfo subscriptionInfo, Exception exception) { logger.ErrorWrite(BuildErrorMessage(basicDeliverEventArgs, exception)); consumerErrorStrategy.HandleConsumerError(basicDeliverEventArgs, exception); DoAck(basicDeliverEventArgs, subscriptionInfo, ExceptionAckStrategy); }
private void HandleMessageDelivery(BasicDeliverEventArgs basicDeliverEventArgs) { var consumerTag = basicDeliverEventArgs.ConsumerTag; if (!subscriptions.ContainsKey(consumerTag)) { throw new EasyNetQException("No callback found for ConsumerTag {0}", consumerTag); } logger.DebugWrite("Subscriber Recieved {0}, CorrelationId {1}", basicDeliverEventArgs.RoutingKey, basicDeliverEventArgs.BasicProperties.CorrelationId); var subscriptionInfo = subscriptions[consumerTag]; try { var completionTask = subscriptionInfo.Callback( consumerTag, basicDeliverEventArgs.DeliveryTag, basicDeliverEventArgs.Redelivered, basicDeliverEventArgs.Exchange, basicDeliverEventArgs.RoutingKey, basicDeliverEventArgs.BasicProperties, basicDeliverEventArgs.Body); completionTask.ContinueWith(task => { if (task.IsFaulted) { var exception = task.Exception; logger.ErrorWrite(BuildErrorMessage(basicDeliverEventArgs, exception)); consumerErrorStrategy.HandleConsumerError(basicDeliverEventArgs, exception); } DoAck(basicDeliverEventArgs, subscriptionInfo); }); } catch (Exception exception) { logger.ErrorWrite(BuildErrorMessage(basicDeliverEventArgs, exception)); consumerErrorStrategy.HandleConsumerError(basicDeliverEventArgs, exception); DoAck(basicDeliverEventArgs, subscriptionInfo); } }
public When_the_handler_throws_an_exception() { var conventions = new Conventions(new TypeNameSerializer()) { ConsumerTagConvention = () => consumerTag }; consumerErrorStrategy = Substitute.For <IConsumerErrorStrategy>(); consumerErrorStrategy.HandleConsumerError(null, null) .ReturnsForAnyArgs(i => { basicDeliverEventArgs = (ConsumerExecutionContext)i[0]; raisedException = (Exception)i[1]; return(AckStrategies.Ack); }); mockBuilder = new MockBuilder(x => x .Register <IConventions>(_ => conventions) .Register(_ => consumerErrorStrategy) //.Register<IEasyNetQLogger>(_ => new ConsoleLogger()) ); mockBuilder.Bus.Subscribe <MyMessage>(subscriptionId, message => { throw originalException; }); const string text = "Hello there, I am the text!"; originalMessage = new MyMessage { Text = text }; var body = new JsonSerializer(new TypeNameSerializer()).MessageToBytes(originalMessage); // deliver a message mockBuilder.Consumers[0].HandleBasicDeliver( consumerTag, deliveryTag, false, // redelivered typeName, "#", new BasicProperties { Type = typeName, CorrelationId = correlationId }, body); // wait for the subscription thread to handle the message ... var autoResetEvent = new AutoResetEvent(false); mockBuilder.EventBus.Subscribe <AckEvent>(x => autoResetEvent.Set()); autoResetEvent.WaitOne(1000); }
private void HandleErrorInSubscriptionHandler(ConsumerExecutionContext context, Exception exception) { logger.ErrorWrite(BuildErrorMessage(context, exception)); try { var strategy = consumerErrorStrategy.HandleConsumerError(context, exception); DoAck(context, (model, deliveryTag) => ExceptionAckStrategy(model, deliveryTag, strategy)); } catch (Exception consumerErrorStrategyError) { logger.ErrorWrite("Exception in ConsumerErrorStrategy:\n{0}", consumerErrorStrategyError); } }
private AckStrategy GetAckStrategy(ConsumerExecutionContext context, Task task) { var ackStrategy = AckStrategies.Ack; try { if (task.IsFaulted) { Logger.Error(BuildErrorMessage(context, task.Exception)); ackStrategy = consumerErrorStrategy.HandleConsumerError(context, task.Exception); } else if (task.IsCanceled) { ackStrategy = consumerErrorStrategy.HandleConsumerCancelled(context); } } catch (Exception consumerErrorStrategyError) { Logger.Error("Exception in ConsumerErrorStrategy:\n{0}", consumerErrorStrategyError); return(AckStrategies.Nothing); } return(ackStrategy); }
protected virtual AckStrategy GetAckStrategy(ConsumerExecutionContext context, Task task) { var ackStrategy = AckStrategies.Ack; try { if (task.IsFaulted) { logger.ErrorWrite(BuildErrorMessage(context, task.Exception).EscapeBraces()); ackStrategy = consumerErrorStrategy.HandleConsumerError(context, task.Exception); } else if (task.IsCanceled) { ackStrategy = consumerErrorStrategy.HandleConsumerCancelled(context); } } catch (Exception consumerErrorStrategyError) { logger.ErrorWrite("Exception in ConsumerErrorStrategy:\n{0}", consumerErrorStrategyError); ackStrategy = AckStrategies.Nothing; } return(ackStrategy); }