public Task <Guid> SendAsync(object obj, Guid correlationId) { if (obj is byte[] bytes) { return(SendBytesAsync(bytes, correlationId)); } return(SendBytesAsync((byte[])SenderSerializer.Serialize(obj), correlationId)); }
public Task <Guid> SendAsync(object obj, Guid correlationId) { if (obj is byte[] bytes) { return(SendBytesAsync(bytes, correlationId)); } if (obj is MultiArray <byte> mArray) { return(SendBytesAsync(mArray.AsArray(), correlationId)); } return(SendBytesAsync(SenderSerializer.Serialize(obj).AsArray(), correlationId)); }
protected override async Task <bool> OnSendAsync(RequestMessage message) { if (_senders?.Any() != true) { throw new NullReferenceException("There aren't any senders queues."); } if (_senderOptions is null) { throw new NullReferenceException("SenderOptions is null."); } if (message.Header.ResponseQueue is null) { var recvQueue = _clientQueues.RecvQueue; if (recvQueue != null) { message.Header.ResponseQueue = new MQConnection(recvQueue.Route, recvQueue.Name) { Parameters = recvQueue.Parameters }; message.Header.ResponseExpected = true; message.Header.ResponseTimeoutInSeconds = _receiverOptions?.TimeoutInSec ?? -1; if (!UseSingleResponseQueue) { message.Header.ResponseQueue.Name += "-" + Core.InstanceIdString; } } else { message.Header.ResponseExpected = false; message.Header.ResponseTimeoutInSeconds = -1; } } message.Header.ContextGroupName = Core.ContextGroupName; var data = SenderSerializer.Serialize(message); var body = CreateMessageBody(data, message.CorrelationId); foreach (var queue in _senders) { var subscriber = await queue.GetSubscriberAsync().ConfigureAwait(false); Core.Log.LibVerbose("Sending {0} bytes to the Queue '{1}/{2}' with CorrelationId={3}", body.Length, queue.Route, queue.Name, message.Header.CorrelationId); await subscriber.PublishAsync(queue.Name, body).ConfigureAwait(false); } Core.Log.LibVerbose("Message with CorrelationId={0} sent", message.Header.CorrelationId); Counters.IncrementTotalBytesSent(data.Count); data.ReturnContentToPoolAndDispose(); return(true); }
/// <inheritdoc /> /// <summary> /// On Send message data /// </summary> /// <param name="message">Response message instance</param> /// <param name="e">Event Args</param> protected override async Task <int> OnSendAsync(ResponseMessage message, RequestReceivedEventArgs e) { if (e.ResponseQueues?.Any() != true) { return(-1); } var senderOptions = Config.ResponseOptions.ServerSenderOptions; if (senderOptions is null) { throw new NullReferenceException("ServerSenderOptions is null."); } var data = SenderSerializer.Serialize(message); var response = true; foreach (var queue in e.ResponseQueues) { try { var producer = _rQueue.GetOrAdd(queue.Route, qRoute => { Core.Log.LibVerbose("New Producer from QueueClient"); if (string.IsNullOrEmpty(qRoute)) { throw new UriFormatException($"The route for the connection to {qRoute} is null."); } var options = new KafkaOptions(new Uri(qRoute)); var router = new BrokerRouter(options); return(Extensions.InvokeWithRetry(() => new Producer(router), 5000, int.MaxValue).WaitAsync()); }); Core.Log.LibVerbose("Sending {0} bytes to the Queue '{1}/{2}' with CorrelationId={3}", data.Count, queue.Route, queue.Name, message.CorrelationId); await producer.SendMessageAsync(queue.Name, new[] { new Message { Key = message.CorrelationId.ToByteArray(), Value = data.AsArray() } }).ConfigureAwait(false); } catch (Exception ex) { response = false; Core.Log.Write(ex); } } return(response ? data.Count : -1); }
/// <inheritdoc /> /// <summary> /// On Send message data /// </summary> /// <param name="message">Response message instance</param> /// <param name="e">Event Args</param> protected override Task <int> OnSendAsync(ResponseMessage message, RequestReceivedEventArgs e) { if (e.ResponseQueues?.Any() != true) { return(TaskHelper.CompleteValueMinus1); } var correlationId = message.CorrelationId.ToString(); var data = SenderSerializer.Serialize(message); var response = true; foreach (var queue in e.ResponseQueues) { try { var rabbitQueue = _rQueue.GetOrAdd(queue.Route, q => { var rq = new RabbitMQueue(queue); rq.EnsureConnection(); return(rq); }); if (!rabbitQueue.EnsureConnection()) { continue; } rabbitQueue.EnsureExchange(); var props = rabbitQueue.Channel.CreateBasicProperties(); props.CorrelationId = correlationId; props.Priority = _priority; props.Expiration = _expiration; props.AppId = Core.ApplicationName; props.ContentType = SenderSerializer.MimeTypes[0]; props.DeliveryMode = _deliveryMode; props.Type = _label; Core.Log.LibVerbose("Sending {0} bytes to the Queue '{1}' with CorrelationId={2}", data.Count, rabbitQueue.Route + "/" + queue.Name, correlationId); rabbitQueue.Channel.BasicPublish(rabbitQueue.ExchangeName ?? string.Empty, queue.Name, props, (byte[])data); } catch (Exception ex) { response = false; Core.Log.Write(ex); } } return(response ? Task.FromResult(data.Count) : TaskHelper.CompleteValueMinus1); }
/// <inheritdoc /> /// <summary> /// On Send message data /// </summary> /// <param name="message">Response message instance</param> /// <param name="e">Event Args</param> protected override async Task <int> OnSendAsync(ResponseMessage message, RequestReceivedEventArgs e) { if (e.ResponseQueues?.Any() != true) { return(-1); } var senderOptions = Config.ResponseOptions.ServerSenderOptions; if (senderOptions == null) { throw new ArgumentNullException("ServerSenderOptions"); } var data = SenderSerializer.Serialize(message); var body = NSQueueClient.CreateMessageBody(data, message.CorrelationId); var response = true; foreach (var queue in e.ResponseQueues) { try { var nsqProducerPool = _rQueue.GetOrAdd(queue.Route, q => new ObjectPool <Producer>(pool => { Core.Log.LibVerbose("New Producer from QueueServer"); return(new Producer(q)); }, null, 1)); Core.Log.LibVerbose("Sending {0} bytes to the Queue '{1}' with CorrelationId={2}", data.Count, queue.Route + "/" + queue.Name, message.CorrelationId); var nsqProducer = nsqProducerPool.New(); await nsqProducer.PublishAsync(queue.Name, body).ConfigureAwait(false); nsqProducerPool.Store(nsqProducer); } catch (Exception ex) { response = false; Core.Log.Write(ex); } } return(response ? data.Count : -1); }
/// <inheritdoc /> /// <summary> /// On Send message data /// </summary> /// <param name="message">Response message instance</param> /// <param name="e">Event Args</param> protected override Task <int> OnSendAsync(ResponseMessage message, RequestReceivedEventArgs e) { if (e.ResponseQueues?.Any() != true) { return(TaskHelper.CompleteValueMinus1); } var senderOptions = Config.ResponseOptions.ServerSenderOptions; if (senderOptions == null) { throw new ArgumentNullException("ServerSenderOptions"); } var data = SenderSerializer.Serialize(message); var body = NATSQueueClient.CreateMessageBody(data, message.CorrelationId); var response = true; foreach (var queue in e.ResponseQueues) { try { var producerPool = _rQueue.GetOrAdd(queue.Route, q => new ObjectPool <IConnection>(pool => { Core.Log.LibVerbose("New Producer from QueueServer"); return(_factory.CreateConnection(queue.Route)); }, null, 1)); Core.Log.LibVerbose("Sending {0} bytes to the Queue '{1}' with CorrelationId={2}", data.Count, queue.Route + "/" + queue.Name, message.CorrelationId); var producer = producerPool.New(); producer.Publish(queue.Name, body); producerPool.Store(producer); } catch (Exception ex) { response = false; Core.Log.Write(ex); } } return(response ? Task.FromResult(data.Count) : TaskHelper.CompleteValueMinus1); }
/// <inheritdoc /> /// <summary> /// On Send message data /// </summary> /// <param name="message">Response message instance</param> /// <param name="e">Event Args</param> protected override async Task <int> OnSendAsync(ResponseMessage message, RequestReceivedEventArgs e) { if (e.ResponseQueues?.Any() != true) { return(-1); } var senderOptions = Config.ResponseOptions.ServerSenderOptions; if (senderOptions is null) { throw new NullReferenceException("ServerSenderOptions is null."); } var data = SenderSerializer.Serialize(message); var body = RedisQueueClient.CreateMessageBody(data, message.CorrelationId); var response = true; foreach (var queue in e.ResponseQueues) { try { var producer = _rQueue.GetOrAdd(queue.Route, _ => new RedisMQConnection(queue)); var subscriber = await producer.GetSubscriberAsync().ConfigureAwait(false); Core.Log.LibVerbose("Sending {0} bytes to the Queue '{1}/{2}' with CorrelationId={3}", data.Count, queue.Route, queue.Name, message.CorrelationId); await subscriber.PublishAsync(queue.Name, body).ConfigureAwait(false); } catch (Exception ex) { response = false; Core.Log.Write(ex); } } var length = data.Count; data.ReturnContentToPoolAndDispose(); return(response ? length : -1); }
/// <inheritdoc /> /// <summary> /// On Send message data /// </summary> /// <param name="message">Response message instance</param> /// <param name="e">Event Args</param> protected override Task <int> OnSendAsync(ResponseMessage message, RequestReceivedEventArgs e) { if (e.ResponseQueues?.Any() != true) { return(TaskHelper.CompleteValueMinus1); } var senderOptions = Config.ResponseOptions.ServerSenderOptions; if (senderOptions is null) { throw new NullReferenceException("ServerSenderOptions is null."); } var data = SenderSerializer.Serialize(message); var body = NATSQueueClient.CreateMessageBody(data, message.CorrelationId); var response = true; foreach (var queue in e.ResponseQueues) { try { var producer = _rQueue.GetOrAdd(queue.Route, qRoute => { Core.Log.LibVerbose("New Producer from QueueServer"); return(Extensions.InvokeWithRetry(() => _factory.CreateConnection(qRoute), 5000, int.MaxValue).WaitAsync()); }); Core.Log.LibVerbose("Sending {0} bytes to the Queue '{1}/{2}' with CorrelationId={3}", data.Count, queue.Route, queue.Name, message.CorrelationId); producer.Publish(queue.Name, body); } catch (Exception ex) { response = false; Core.Log.Write(ex); } } return(response ? Task.FromResult(data.Count) : TaskHelper.CompleteValueMinus1); }
protected override async Task <bool> OnSendAsync(RequestMessage message) { if (_senders?.Any() != true) { throw new NullReferenceException("There aren't any senders queues."); } if (_senderOptions is null) { throw new NullReferenceException("SenderOptions is null."); } if (message.Header.ResponseQueue is null) { var recvQueue = _clientQueues.RecvQueue; if (recvQueue != null) { message.Header.ResponseQueue = new MQConnection(recvQueue.Route, recvQueue.Name) { Parameters = recvQueue.Parameters }; message.Header.ResponseExpected = true; message.Header.ResponseTimeoutInSeconds = _receiverOptions?.TimeoutInSec ?? -1; if (!UseSingleResponseQueue) { message.Header.ResponseQueue.Name += "-" + Core.InstanceIdString; } } else { message.Header.ResponseExpected = false; message.Header.ResponseTimeoutInSeconds = -1; } } message.Header.ContextGroupName = Core.ContextGroupName; var data = SenderSerializer.Serialize(message); var correlationId = message.CorrelationId.ToString(); var replyTo = message.Header.ResponseQueue?.Name; if (_senders.Count == 1) { return(await SendTaskAsync(_senders[0]).ConfigureAwait(false)); } var tsk = new Task[_senders.Count]; for (var i = 0; i < tsk.Length; i++) { tsk[i] = SendTaskAsync(_senders[i]); } await Task.WhenAny(tsk).ConfigureAwait(false); Counters.IncrementTotalBytesSent(data.Count); data.ReturnContentToPoolAndDispose(); return(true); async Task <bool> SendTaskAsync(RabbitMQueue sender) { try { if (await sender.EnsureConnectionAsync(1000, 2).ConfigureAwait(false)) { sender.EnsureExchange(); var props = sender.Channel.CreateBasicProperties(); props.CorrelationId = correlationId; if (replyTo != null) { props.ReplyTo = replyTo; } props.Priority = _priority; props.Expiration = _expiration; props.AppId = Core.ApplicationName; props.ContentType = SenderSerializer.MimeTypes[0]; props.DeliveryMode = _deliveryMode; props.Type = _senderOptions.Label ?? string.Empty; Core.Log.LibVerbose("Sending {0} bytes to the Queue '{1}/{2}' with CorrelationId={3}", data.Count, sender.Route, sender.Name, message.Header.CorrelationId); sender.Channel.BasicPublish(sender.ExchangeName ?? string.Empty, sender.Name, props, data.ToArray()); return(true); } } catch (Exception ex) { Core.Log.Write(ex); } return(false); } }
protected override Task <bool> OnSendAsync(RequestMessage message) { if (_senders?.Any() != true) { throw new NullReferenceException("There aren't any senders queues."); } if (_senderOptions == null) { throw new ArgumentNullException("SenderOptions"); } if (message.Header.ResponseQueue == null) { var recvQueue = _clientQueues.RecvQueue; if (recvQueue != null) { message.Header.ResponseQueue = new MQConnection(recvQueue.Route, recvQueue.Name) { Parameters = recvQueue.Parameters }; message.Header.ResponseExpected = true; message.Header.ResponseTimeoutInSeconds = _receiverOptions?.TimeoutInSec ?? -1; if (!UseSingleResponseQueue) { message.Header.ResponseQueue.Name += "_" + message.CorrelationId; var pool = _routeConnection.GetOrAdd(_receiver.Route, r => new ObjectPool <RabbitMQueue>(p => new RabbitMQueue(_receiver))); var cReceiver = pool.New(); cReceiver.EnsureConnection(); cReceiver.Channel.QueueDeclare(message.Header.ResponseQueue.Name, false, false, true, null); pool.Store(cReceiver); } } else { message.Header.ResponseExpected = false; message.Header.ResponseTimeoutInSeconds = -1; } } var data = SenderSerializer.Serialize(message); var correlationId = message.CorrelationId.ToString(); var replyTo = message.Header.ResponseQueue?.Name; foreach (var sender in _senders) { if (!sender.EnsureConnection()) { continue; } sender.EnsureExchange(); var props = sender.Channel.CreateBasicProperties(); props.CorrelationId = correlationId; if (replyTo != null) { props.ReplyTo = replyTo; } props.Priority = _priority; props.Expiration = _expiration; props.AppId = Core.ApplicationName; props.ContentType = SenderSerializer.MimeTypes[0]; props.DeliveryMode = _deliveryMode; props.Type = _senderOptions.Label; Core.Log.LibVerbose("Sending {0} bytes to the Queue '{1}' with CorrelationId={2}", data.Count, sender.Route + "/" + sender.Name, message.Header.CorrelationId); sender.Channel.BasicPublish(sender.ExchangeName ?? string.Empty, sender.Name, props, (byte[])data); } return(TaskHelper.CompleteTrue); }