protected async Task Move(ReceiveContext context, Action <PublishRequest, SendHeaders> preSend) { if (!context.TryGetPayload(out ModelContext modelContext)) { throw new ArgumentException("The ReceiveContext must contain a BrokeredMessageContext (from Azure Service Bus)", nameof(context)); } await _topologyFilter.Send(modelContext, Pipe.Empty <ModelContext>()).ConfigureAwait(false); var topicArn = await modelContext.GetTopic(_destination).ConfigureAwait(false); var message = modelContext.CreateTransportMessage(topicArn, context.GetBody()); if (context.TryGetPayload(out AmazonSqsMessageContext messageContext)) { foreach (string key in messageContext.Attributes.Keys) { message.MessageAttributes[key].StringValue = messageContext.Attributes[key].StringValue; message.MessageAttributes[key].BinaryValue = messageContext.Attributes[key].BinaryValue; message.MessageAttributes[key].DataType = messageContext.Attributes[key].DataType; } } SendHeaders headers = new AmazonSnsHeaderAdapter(message.MessageAttributes); headers.SetHostHeaders(); preSend(message, headers); var task = Task.Run(() => modelContext.Publish(message)); context.AddPendingTask(task); }
async Task ISendTransport.Move(ReceiveContext context, IPipe <SendContext> pipe) { IPipe <ModelContext> modelPipe = Pipe.New <ModelContext>(p => { p.UseFilter(_filter); p.UseExecuteAsync(async modelContext => { try { IBasicProperties properties; string routingKey = ""; RabbitMqBasicConsumeContext basicConsumeContext; if (context.TryGetPayload(out basicConsumeContext)) { properties = basicConsumeContext.Properties; routingKey = basicConsumeContext.RoutingKey; } else { properties = modelContext.Model.CreateBasicProperties(); properties.Headers = new Dictionary <string, object>(); } var moveContext = new RabbitMqMoveContext(context, properties, _exchange, routingKey ?? ""); await pipe.Send(moveContext).ConfigureAwait(false); byte[] body; using (var memoryStream = new MemoryStream()) { using (var bodyStream = context.GetBody()) { await bodyStream.CopyToAsync(memoryStream).ConfigureAwait(false); } body = memoryStream.ToArray(); } var task = modelContext.BasicPublishAsync(_exchange, "", true, properties, body, true); context.AddPendingTask(task); } catch (Exception ex) { if (_log.IsErrorEnabled) { _log.Error("Faulted moving message to error queue: " + _exchange, ex); } throw; } }); }); await _modelCache.Send(modelPipe, context.CancellationToken).ConfigureAwait(false); }
protected async Task Move(ReceiveContext context, Action <IMessage, SendHeaders> preSend) { if (!context.TryGetPayload(out SessionContext sessionContext)) { throw new ArgumentException("The ReceiveContext must contain a BrokeredMessageContext (from Azure Service Bus)", nameof(context)); } await _topologyFilter.Send(sessionContext, Pipe.Empty <SessionContext>()).ConfigureAwait(false); var queue = await sessionContext.GetQueue(_destination).ConfigureAwait(false); var producer = await sessionContext.CreateMessageProducer(queue).ConfigureAwait(false); byte[] body = context.GetBody(); var message = producer.CreateBytesMessage(); if (context.TryGetPayload(out ActiveMqMessageContext messageContext)) { foreach (string key in messageContext.Properties.Keys) { message.Properties[key] = messageContext.Properties[key]; } } SendHeaders headers = new ActiveMqHeaderAdapter(message.Properties); headers.SetHostHeaders(); preSend(message, headers); message.Content = body; var task = Task.Run(() => producer.Send(message)); var closeTask = task.ContinueWith(_ => { producer.Close(); producer.Dispose(); }); context.AddPendingTask(task); context.AddPendingTask(closeTask); }
protected async Task Move(ReceiveContext context, Action <IBasicProperties, SendHeaders> preSend) { if (!context.TryGetPayload(out ModelContext modelContext)) { throw new ArgumentException("The ReceiveContext must contain a BrokeredMessageContext (from Azure Service Bus)", nameof(context)); } await _topologyFilter.Send(modelContext, Pipe.Empty <ModelContext>()).ConfigureAwait(false); IBasicProperties properties; string routingKey = ""; byte[] body; if (context.TryGetPayload(out RabbitMqBasicConsumeContext basicConsumeContext)) { properties = basicConsumeContext.Properties; routingKey = basicConsumeContext.RoutingKey; body = basicConsumeContext.Body; } else { properties = modelContext.Model.CreateBasicProperties(); properties.Headers = new Dictionary <string, object>(); body = context.GetBody(); } SendHeaders headers = new RabbitMqSendHeaders(properties); headers.SetHostHeaders(); preSend(properties, headers); var task = modelContext.BasicPublishAsync(_exchange, routingKey, true, properties, body, true); context.AddPendingTask(task); }
public void Respond <T>(T message) where T : class { var task = RespondAsync(message); _receiveContext.AddPendingTask(task); }
async Task ISendTransport.Move(ReceiveContext context, IPipe <SendContext> pipe) { IPipe <ModelContext> modelPipe = Pipe.New <ModelContext>(p => { p.UseFilter(_filter); p.UseExecuteAsync(async modelContext => { Guid?messageId = context.TransportHeaders.Get("MessageId", default(Guid?)); try { IBasicProperties properties; RabbitMqBasicConsumeContext basicConsumeContext; if (context.TryGetPayload(out basicConsumeContext)) { properties = basicConsumeContext.Properties; } else { properties = modelContext.Model.CreateBasicProperties(); properties.Headers = new Dictionary <string, object>(); } var moveContext = new RabbitMqMoveContext(context, properties); await pipe.Send(moveContext).ConfigureAwait(false); // properties.Headers["Content-Type"] = context.ContentType.MediaType; // if (messageId.HasValue) // properties.MessageId = messageId.ToString(); byte[] body; using (var memoryStream = new MemoryStream()) { using (Stream bodyStream = context.GetBody()) { bodyStream.CopyTo(memoryStream); } body = memoryStream.ToArray(); } Task task = modelContext.BasicPublishAsync(_sendSettings.ExchangeName, "", true, properties, body); context.AddPendingTask(task); if (_log.IsDebugEnabled) { context.InputAddress.LogMoved(modelContext.ConnectionContext.HostSettings.GetSendAddress(_sendSettings), messageId?.ToString() ?? "N/A", "Moved"); } } catch (Exception ex) { if (_log.IsErrorEnabled) { _log.Error("Move To Error Queue Fault: " + _sendSettings.ExchangeName, ex); } throw; } }); }); await _modelCache.Send(modelPipe, context.CancellationToken).ConfigureAwait(false); }