0

I'm facing a strange issue, and I ran out of the possible causes. The scenario is

  1. Fetch incoming message from queue
  2. Process it and then add new message to another queue

but the thing is, if I finish the long running task for the incoming message, and then try to add new message to another queue, I don't receive it. If I just add a face message to that another queue, then I am able to receive the real message after the long-running operation is finished. But why ? I don't want to put any fake messages to the queue, but without that my scenario doesn't work. Any ideas ?

public class WorkerRole : RoleEntryPoint
{
    // QueueClient is thread-safe. Recommended that you cache 
    // rather than recreating it on every request
    Microsoft.ServiceBus.Messaging.QueueClient Client;
    ManualResetEvent CompletedEvent = new ManualResetEvent(false);

    public override void Run()
    {
        MyResult result = null;

        var queueClient = new Microsoft.Azure.ServiceBus.QueueClient("QueueConnectionString", "QueueName");

        Client.OnMessage(async (receivedMessage) =>
            {
                try
                {
                    using (Stream stream = receivedMessage.GetBody<Stream>())
                    {
                        using (StreamReader reader = new StreamReader(stream))
                        {
                            string json = reader.ReadToEnd();

                            OCRQueueItem_Incoming item = JsonConvert.DeserializeObject<IncomingClass>(json);
                            var someClass = new OCRManager();

                            var message = new Message(Encoding.UTF8.GetBytes("test 1"));

                            await queueClient.SendAsync(message);

                            result = new SomeManager().RunLongRunningTask(item); //it runs for 1-2min                         
                        }
                    }
                }
                catch (Exception ex) { }
                finally
                {
                    var json = JsonConvert.SerializeObject(result);

                    var message = new Message(Encoding.UTF8.GetBytes(json));

                    await queueClient.SendAsync(message);
                }
            });

        CompletedEvent.WaitOne();
    }

    public override bool OnStart()
    {
        ServicePointManager.DefaultConnectionLimit = 12;

        string connectionString = CloudConfigurationManager.GetSetting("Queue.ConnectionString");           

        Client = Microsoft.ServiceBus.Messaging.QueueClient.Create(connectionString);
        return base.OnStart();
    }

    public override void OnStop()
    {
        Client.Close();
        CompletedEvent.Set();
        base.OnStop();
    }
}
Tony
  • 12,405
  • 36
  • 126
  • 226
  • You should not be putting any "fake" messages. And what is the porpose of `result = new SomeManager().RunLongRunningTask(item)`? If that's your test consumer, it shouldn't be in the callback. – Sean Feldman Apr 24 '18 at 22:53
  • yeah I know that I shoud not send any fake messages, but without that I am not able to receive the real message. Why I shouldn't use my long running consumer in the callback ? – Tony Apr 25 '18 at 08:33
  • The 20 seconds it takes (or longer) holds the incoming message. You really should be processing the incoming message, fire off a new one, and complete the incoming message. Do you mind code on GH? That way one can pick at the details and submit a PR. – Sean Feldman Apr 25 '18 at 19:59

0 Answers0