From 28cfdc09403f2b7a195d89619af53aed8aee80c9 Mon Sep 17 00:00:00 2001 From: danielmarbach Date: Mon, 15 Jun 2020 21:18:20 +0200 Subject: [PATCH] Implement BasicPublishBatch with ReadOnlyMemory --- .../client/api/IBasicPublishBatch.cs | 2 + .../client/impl/BasicPublishBatch.cs | 6 ++ .../impl/BasicPublishBatchExtensions.cs | 61 +++++++++++++++++++ .../Unit/APIApproval.Approve.verified.txt | 6 ++ projects/Unit/TestBasicPublishBatch.cs | 4 +- 5 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 projects/RabbitMQ.Client/client/impl/BasicPublishBatchExtensions.cs diff --git a/projects/RabbitMQ.Client/client/api/IBasicPublishBatch.cs b/projects/RabbitMQ.Client/client/api/IBasicPublishBatch.cs index 2f1e8d6a21..c31fb9c552 100644 --- a/projects/RabbitMQ.Client/client/api/IBasicPublishBatch.cs +++ b/projects/RabbitMQ.Client/client/api/IBasicPublishBatch.cs @@ -37,12 +37,14 @@ // The Initial Developer of the Original Code is Pivotal Software, Inc. // Copyright (c) 2007-2020 VMware, Inc. All rights reserved. //--------------------------------------------------------------------------- + using System; namespace RabbitMQ.Client { public interface IBasicPublishBatch { + [Obsolete("Use Add(string exchange, string routingKey, bool mandatory, IBasicProperties properties, ReadOnlyMemory body) instead. Will be replaced in version 7.0", false)] void Add(string exchange, string routingKey, bool mandatory, IBasicProperties properties, byte[] body); void Publish(); } diff --git a/projects/RabbitMQ.Client/client/impl/BasicPublishBatch.cs b/projects/RabbitMQ.Client/client/impl/BasicPublishBatch.cs index 09cf54eb01..3c2e0e3fca 100644 --- a/projects/RabbitMQ.Client/client/impl/BasicPublishBatch.cs +++ b/projects/RabbitMQ.Client/client/impl/BasicPublishBatch.cs @@ -56,6 +56,12 @@ internal BasicPublishBatch (ModelBase model) } public void Add(string exchange, string routingKey, bool mandatory, IBasicProperties basicProperties, byte[] body) + { + ReadOnlyMemory bodyAsMemory = body; + Add(exchange, routingKey, mandatory, basicProperties, bodyAsMemory); + } + + public void Add(string exchange, string routingKey, bool mandatory, IBasicProperties basicProperties, ReadOnlyMemory body) { IBasicProperties bp = basicProperties ?? _model.CreateBasicProperties(); var method = new BasicPublish diff --git a/projects/RabbitMQ.Client/client/impl/BasicPublishBatchExtensions.cs b/projects/RabbitMQ.Client/client/impl/BasicPublishBatchExtensions.cs new file mode 100644 index 0000000000..b56f53d246 --- /dev/null +++ b/projects/RabbitMQ.Client/client/impl/BasicPublishBatchExtensions.cs @@ -0,0 +1,61 @@ +// This source code is dual-licensed under the Apache License, version +// 2.0, and the Mozilla Public License, version 1.1. +// +// The APL v2.0: +// +//--------------------------------------------------------------------------- +// Copyright (c) 2007-2020 VMware, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +//--------------------------------------------------------------------------- +// +// The MPL v1.1: +// +//--------------------------------------------------------------------------- +// The contents of this file are subject to the Mozilla Public License +// Version 1.1 (the "License"); you may not use this file except in +// compliance with the License. You may obtain a copy of the License +// at https://www.mozilla.org/MPL/ +// +// Software distributed under the License is distributed on an "AS IS" +// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +// the License for the specific language governing rights and +// limitations under the License. +// +// The Original Code is RabbitMQ. +// +// The Initial Developer of the Original Code is Pivotal Software, Inc. +// Copyright (c) 2007-2020 VMware, Inc. All rights reserved. +//--------------------------------------------------------------------------- + +using System; +using RabbitMQ.Client.Impl; + +namespace RabbitMQ.Client +{ + public static class BasicPublishBatchExtensions + { + public static void Add(this IBasicPublishBatch batch, string exchange, string routingKey, bool mandatory, IBasicProperties properties, ReadOnlyMemory body) + { + if (batch is BasicPublishBatch batchInternal) + { + batchInternal.Add(exchange, routingKey, mandatory, properties, body); + return; + } + +#pragma warning disable 618 + batch.Add(exchange, routingKey, mandatory, properties, body.ToArray()); +#pragma warning restore 618 + } + } +} diff --git a/projects/Unit/APIApproval.Approve.verified.txt b/projects/Unit/APIApproval.Approve.verified.txt index b0e3246c3b..01aa46d89d 100644 --- a/projects/Unit/APIApproval.Approve.verified.txt +++ b/projects/Unit/APIApproval.Approve.verified.txt @@ -56,6 +56,10 @@ namespace RabbitMQ.Client public bool Redelivered { get; } public string RoutingKey { get; } } + public static class BasicPublishBatchExtensions + { + public static void Add(this RabbitMQ.Client.IBasicPublishBatch batch, string exchange, string routingKey, bool mandatory, RabbitMQ.Client.IBasicProperties properties, System.ReadOnlyMemory body) { } + } public class BinaryTableValue { public BinaryTableValue() { } @@ -291,6 +295,8 @@ namespace RabbitMQ.Client } public interface IBasicPublishBatch { + [System.Obsolete("Use Add(string exchange, string routingKey, bool mandatory, IBasicProperties prop" + + "erties, ReadOnlyMemory body) instead. Will be replaced in version 7.0", false)] void Add(string exchange, string routingKey, bool mandatory, RabbitMQ.Client.IBasicProperties properties, byte[] body); void Publish(); } diff --git a/projects/Unit/TestBasicPublishBatch.cs b/projects/Unit/TestBasicPublishBatch.cs index 8fb63547af..f8493dade8 100644 --- a/projects/Unit/TestBasicPublishBatch.cs +++ b/projects/Unit/TestBasicPublishBatch.cs @@ -53,8 +53,8 @@ public void TestBasicPublishBatchSend() Model.QueueDeclare(queue: "test-message-batch-a", durable: false); Model.QueueDeclare(queue: "test-message-batch-b", durable: false); IBasicPublishBatch batch = Model.CreateBasicPublishBatch(); - batch.Add("", "test-message-batch-a", false, null, new byte [] {}); - batch.Add("", "test-message-batch-b", false, null, new byte [] {}); + batch.Add("", "test-message-batch-a", false, null, new ReadOnlyMemory()); + batch.Add("", "test-message-batch-b", false, null, new ReadOnlyMemory()); batch.Publish(); Model.WaitForConfirmsOrDie(TimeSpan.FromSeconds(15)); BasicGetResult resultA = Model.BasicGet("test-message-batch-a", true);