private ICallbackExecutor GetExecutor(string messageType, ConsumeMessageMetadata metadata) { try { return(_consumers[messageType]); } catch (KeyNotFoundException) { var excp = new PayQueueException("Received message was not defined in contract."); excp.Data["Queue"] = metadata.Queue; excp.Data["Exchange"] = metadata.Exchange; excp.Data["Type"] = messageType; throw excp; } }
private AsyncEventHandler <BasicDeliverEventArgs> GetReceived(IConsumeExecutor executor, string queue, IModel channel) { return(async(model, ea) => { var metadata = new ConsumeMessageMetadata() { Queue = queue, Exchange = ea.Exchange }; Func <string> messageType = () => (string)ea.BasicProperties.Headers["message_type"]; try { await executor.Execute(GetExchangePublisher(), messageType, ea.Body.ToArray(), metadata); channel.BasicAck(ea.DeliveryTag, false); } catch (Exception e) { var dataArr = ea.Body.ToArray(); channel.BasicNack(ea.DeliveryTag, false, true); var errMsg = new PayQueueError() { ServiceLabel = _conf.ServiceLabel, ConsumeMessageType = messageType(), ConsumeParams = Encoding.UTF8.GetString(dataArr, 0, dataArr.Length), ExceptionType = e.GetType().ToString(), ExceptionMessage = e.Message, ExceptionStacktrace = e.StackTrace, Exchange = ea.Exchange }; var data = JsonSerializer.Serialize(errMsg); await new PooledExchangePublisher(_poolChannel.Writer) .PublishError(_conf.ErrorExchange, _conf.ErrorQueue, Encoding.UTF8.GetBytes(data)); } }); }
public async Task Execute(IExchangePublisher exchangePublisher, Func <string> messageType, byte[] data, ConsumeMessageMetadata metadata) { await GetExecutor(messageType(), metadata).Execute(exchangePublisher, data); }