| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Buffers; |
| | | 6 | | using System.IO.Pipelines; |
| | | 7 | | using System.Net; |
| | | 8 | | using System.Threading; |
| | | 9 | | using System.Threading.Tasks; |
| | | 10 | | using CoreWCF.Channels.Configuration; |
| | | 11 | | using CoreWCF.Configuration; |
| | | 12 | | using CoreWCF.Queue.Common; |
| | | 13 | | using Microsoft.Extensions.DependencyInjection; |
| | | 14 | | using Microsoft.Extensions.Logging; |
| | | 15 | | using RabbitMQ.Client; |
| | | 16 | | using RabbitMQ.Client.Events; |
| | | 17 | | |
| | | 18 | | namespace CoreWCF.Channels |
| | | 19 | | { |
| | | 20 | | internal class RabbitMqTransportPump : QueueTransportPump |
| | | 21 | | { |
| | | 22 | | private const bool DefaultAutoAck = false; |
| | | 23 | | private readonly ILogger<RabbitMqTransportPump> _logger; |
| | | 24 | | private SslOption _sslOption; |
| | | 25 | | private QueueDeclareConfiguration _queueConfiguration; |
| | | 26 | | private ICredentials _credentials; |
| | | 27 | | private string _virtualHost; |
| | | 28 | | private ConnectionFactory _factory; |
| | | 29 | | private IConnection _connection; |
| | | 30 | | private IModel _channel; |
| | | 31 | | private EventingBasicConsumer _consumer; |
| | | 32 | | private readonly Uri _baseAddress; |
| | | 33 | | private bool _isAutoAck; |
| | | 34 | | |
| | 3 | 35 | | public RabbitMqTransportPump( |
| | 3 | 36 | | IServiceProvider serviceProvider, |
| | 3 | 37 | | IServiceDispatcher serviceDispatcher, |
| | 3 | 38 | | SslOption sslOption, |
| | 3 | 39 | | QueueDeclareConfiguration queueConfiguration, |
| | 3 | 40 | | ICredentials credentials, |
| | 3 | 41 | | string virtualHost) |
| | | 42 | | { |
| | 3 | 43 | | _baseAddress = serviceDispatcher.BaseAddress; |
| | 3 | 44 | | _logger = serviceProvider.GetRequiredService<ILogger<RabbitMqTransportPump>>(); |
| | 3 | 45 | | _sslOption = sslOption; |
| | 3 | 46 | | _queueConfiguration = queueConfiguration; |
| | 3 | 47 | | _credentials = credentials; |
| | 3 | 48 | | _virtualHost = virtualHost; |
| | 3 | 49 | | _isAutoAck = DefaultAutoAck; |
| | 3 | 50 | | } |
| | | 51 | | |
| | | 52 | | public override Task StartPumpAsync(QueueTransportContext queueTransportContext, CancellationToken token) |
| | | 53 | | { |
| | 3 | 54 | | var rabbitMqTransport = queueTransportContext.QueueBindingElement as RabbitMqTransportBindingElement; |
| | 3 | 55 | | if (rabbitMqTransport == null) |
| | | 56 | | { |
| | 0 | 57 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(rabbitMqTransport)); |
| | | 58 | | } |
| | | 59 | | |
| | 3 | 60 | | var connectionSettings = RabbitMqConnectionSettings.FromUri(_baseAddress, _credentials, _sslOption, _virtual |
| | 3 | 61 | | _factory = connectionSettings.GetConnectionFactory(); |
| | 3 | 62 | | _connection = _factory.CreateConnection(); |
| | 3 | 63 | | _channel = _connection.CreateModel(); |
| | 3 | 64 | | _channel.BasicQos(0, _queueConfiguration.PrefetchCount, _queueConfiguration.GlobalQosPrefetch); |
| | 3 | 65 | | _channel.QueueDeclare( |
| | 3 | 66 | | connectionSettings.QueueName, |
| | 3 | 67 | | _queueConfiguration.Durable, |
| | 3 | 68 | | _queueConfiguration.Exclusive, |
| | 3 | 69 | | _queueConfiguration.AutoDelete, |
| | 3 | 70 | | _queueConfiguration.ToDictionary()); |
| | 3 | 71 | | _channel.QueueBind( |
| | 3 | 72 | | connectionSettings.QueueName, |
| | 3 | 73 | | connectionSettings.Exchange, |
| | 3 | 74 | | connectionSettings.RoutingKey, |
| | 3 | 75 | | null); |
| | | 76 | | |
| | 3 | 77 | | _consumer = new EventingBasicConsumer(_channel); |
| | 3 | 78 | | _consumer.Received += (_, ea) => |
| | 3 | 79 | | { |
| | 3 | 80 | | ConsumeMessage(ea, queueTransportContext); |
| | 6 | 81 | | }; |
| | 3 | 82 | | _channel.BasicConsume(connectionSettings.QueueName, DefaultAutoAck, _consumer); |
| | 3 | 83 | | return Task.CompletedTask; |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | public override Task StopPumpAsync(CancellationToken token) |
| | | 87 | | { |
| | 3 | 88 | | _channel?.Close(); |
| | 3 | 89 | | _channel?.Dispose(); |
| | | 90 | | |
| | 3 | 91 | | _connection?.Close(); |
| | 3 | 92 | | _connection?.Dispose(); |
| | 3 | 93 | | return Task.CompletedTask; |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | private async void ConsumeMessage(BasicDeliverEventArgs eventArgs, QueueTransportContext queueTransportContext) |
| | | 97 | | { |
| | 3 | 98 | | _logger.LogInformation("Receiving message from RabbitMQ"); |
| | 3 | 99 | | var reader = PipeReader.Create(new ReadOnlySequence<byte>(eventArgs.Body)); |
| | 3 | 100 | | var deliveryTag = eventArgs.DeliveryTag; |
| | | 101 | | |
| | 3 | 102 | | await queueTransportContext.QueueMessageDispatcher(GetContext(reader, queueTransportContext, deliveryTag)); |
| | 3 | 103 | | } |
| | | 104 | | |
| | | 105 | | private QueueMessageContext GetContext(PipeReader reader, QueueTransportContext transportContext, ulong delivery |
| | | 106 | | { |
| | 3 | 107 | | var receiveContext = new RabbitMqReceiveContext(deliveryTag, _channel, _isAutoAck, _logger); |
| | 3 | 108 | | var context = new QueueMessageContext |
| | 3 | 109 | | { |
| | 3 | 110 | | QueueMessageReader = reader, |
| | 3 | 111 | | LocalAddress = new EndpointAddress(transportContext.ServiceDispatcher.BaseAddress), |
| | 3 | 112 | | QueueTransportContext = transportContext, |
| | 3 | 113 | | ReceiveContext = receiveContext |
| | 3 | 114 | | }; |
| | | 115 | | |
| | 3 | 116 | | return context; |
| | | 117 | | } |
| | | 118 | | } |
| | | 119 | | } |