0

Azure Service Bus キューに受信できないメッセージがあります。そして、問題が何であるかについての指標が得られません。メッセージサイズと関係があると思います。以下のコードから、私が OpenFileDialog を使用していることがわかります。jpeg 画像を選択していますが、それらはキューに送信されています。現在、約 50KB 未満の小さな画像を送信すると、受信プロセスによって正常に表示されますが、100KB を超える大きな画像はキューに留まっています。MSDN によると、メッセージ サイズの制限は 256 KB であり、ここで何が起こっているのかわかりません。

私は2つのクラスを持っています。1 つは SendToQueue で、もう 1 つは RecvFromQueue です。これがコードです。

using System;
using System.IO;
using System.Windows.Forms;
using Microsoft.ServiceBus;
using Microsoft.ServiceBus.Messaging;
using Microsoft.WindowsAzure;

namespace ServiceBusQueueApp
{
    public class SendToQueue
    {
        private const string c_testqueue = "TestQueue";

        [STAThreadAttribute]
        static void Main(string[] args)
        {
            // Configure Queue Settings
            QueueDescription qd = new QueueDescription(c_testqueue)
            {
                MaxSizeInMegabytes = 5120,
                DefaultMessageTimeToLive = new TimeSpan(1, 1, 0)
            };

            // Create a new Queue with custom settings
            string connectionString =
                CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");

            var namespaceManager =
                NamespaceManager.CreateFromConnectionString(connectionString);

            if (!namespaceManager.QueueExists(c_testqueue))
            {
                namespaceManager.CreateQueue(qd);
            }

            namespaceManager.DeleteQueue(qd.Path);
            namespaceManager.CreateQueue(qd);

            QueueClient client = QueueClient.CreateFromConnectionString(connectionString, c_testqueue);

            double maxSize = Math.Pow(2, 18);

            OpenFileDialog openFile = new OpenFileDialog();
            while (true)
            {
                if (openFile.ShowDialog() == DialogResult.Cancel)
                {
                    break;
                }

                var messageBodyStream = new FileStream(openFile.FileName, System.IO.FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

                if (messageBodyStream.Length > maxSize)
                {
                    MessageBox.Show("File is larger than 256KB.");
                    continue;
                }
                BrokeredMessage msg =
                    new BrokeredMessage(messageBodyStream);
                msg.Properties["MyProperty"] = "Test Value";


                try
                {
                    //send msg to the queue
                    client.Send(msg);
                }
                catch (Exception exception)
                {
                    MessageBox.Show(exception.Message);
                    throw;
                }
            }

        }
    }
}


using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using Microsoft.ServiceBus;
using Microsoft.ServiceBus.Messaging;
using Microsoft.WindowsAzure;

namespace ServiceBusQueueApp
{
    class RecvFromQueue
    {

        private const string c_testqueue = "TestQueue";

        static void Main(string[] args)
        {
            // Create a new Queue with custom settings
            string connectionString =
                CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");

            var namespaceManager =
                NamespaceManager.CreateFromConnectionString(connectionString);

            if (!namespaceManager.QueueExists(c_testqueue))
            {
                MessageBox.Show("Queue does not exist.");
                throw new Exception("Queue does not exist.");
            }

            QueueClient client = QueueClient.CreateFromConnectionString(connectionString, c_testqueue);

            while (true)
            {
                BrokeredMessage message = client.Receive();

                if (message == null)
                {
                    continue;
                }
                try
                {
                    Stream fstream = message.GetBody<Stream>();
                    byte[] buffer = new byte[fstream.Length];
                    fstream.Read(buffer, 0, (int)fstream.Length);
                    File.WriteAllBytes(@"C:\users\roberthar\pictures\testpic.png", buffer);
                    fstream.Close();

                    Process paint = new Process();
                    paint.StartInfo.FileName = @"C:\Windows\System32\mspaint.exe";
                    paint.StartInfo.Arguments = @"C:\users\roberthar\pictures\testpic.png";
                    paint.Start();

                    Thread.Sleep(3000);

                    paint.Close();

                    // Remove message from queue
                    message.Complete();
                }
                catch (Exception exception)
                {
                    // Indicate a problem, unlock message in queue
                    message.Abandon();
                }
            }
        }
    }
}
4

2 に答える 2

0

同僚を助けるこの質問に出くわしました。(私はそれが別の開発者だったことを約束します!)

彼が queue.MessageCount をチェックするコードを実行しているときに ( myQMessageCount という名前の変数に設定)、コードに "while (myQMessageCount > 0)" があり、彼が msg.Complete (同じ while ループ内)

.MessageCount は、キュー内のすべてのメッセージの「合計」であり、Active (読み取ることができるはずのメッセージ) やデッド レターなどを含みます。

したがって、(1) 修正は、.MessageCount ではなく ActiveMessageCount をチェックするようにコードを変更することでした。

                Microsoft.ServiceBus.Messaging.QueueDescription qd = myMicrosoftdotServiceBusdotNamespaceManager.GetQueue(qName);
                string deadLetterQueueName = QueueClient.FormatDeadLetterPath(qd.Path);
                int activeMessageCount = qd.MessageCountDetails.ActiveMessageCount;
                int deadLetterMessageCount = qd.MessageCountDetails.DeadLetterMessageCount;
                int scheduledMessageCount = qd.MessageCountDetails.ScheduledMessageCount;
                int transferDeadLetterMessageCount = qd.MessageCountDetails.TransferDeadLetterMessageCount;
                int transferMessageCount = qd.MessageCountDetails.TransferMessageCount;

(2) 議論した後、ActiveMessageCount をチェックし続けることはおそらく賢明ではなく、返された null の BrokeredMessage をそのままにして、キューにメッセージがもうないことをチェックします。

ともかく。この回答は、作成中のカスタム読み取りキュー コードで行き詰まる可能性がある将来の読者のために、ここに投稿しています。

于 2017-09-15T15:42:57.683 に答える