1

I have a CloudQueueMessage and I want to insert "Hello World" - so I write:

CloudQueueMessage message = new CloudQueueMessage("Hello World");

it’s perfect.

My problem is that I want to put an object in place of "Hello World" like this:

CloudQueueMessage message = new CloudQueueMessage(new MyObject(parameter1, parameter2, ...));

But the compiler will not let me because it tells me:

The constructor CloudQueueMessage (MyObject) is undefined.

How can I achieve what I want?

David Makogon
  • 69,407
  • 21
  • 141
  • 189
francedema
  • 41
  • 4
  • Does this answer your question? [Passing object messages in Azure Queue Storage](https://stackoverflow.com/questions/8550702/passing-object-messages-in-azure-queue-storage) – Michael Freidgeim Oct 29 '21 at 05:21

3 Answers3

0

CloudQueueMessage may be constructed with either binary data or a string, not an object to be serialized (though you may serialize it to a string yourself, then store it as a message). That's why you're getting an error: there's no constructor that takes a MyObject as its parameter.

For more details on the creation of a CloudQueueMessage, see this msdn page.

David Makogon
  • 69,407
  • 21
  • 141
  • 189
  • So what are you doing?? must i have to use stringa? – francedema Mar 27 '12 at 18:22
  • It's really up to you, but strings are quite easy. You're talking about messages, not object storage, so it's very simple. For me, I usually create a simple tokenized string with a simple delimiter. For instance, I might be creating thumbnails and my message might look like: `"THUMB|BLOBURL|THUMBURL"` - trivial to string-split and parse. You can also go back and modify a message (let's say it's a multi-pass video rendering; you can leave a marker with the pass completed, so in case the task fails, you can restart where you left off). – David Makogon Mar 27 '12 at 19:07
0

You may check out this question.

It outlines a technique for having "Strongly typed Queues" using C#. However the approach is really trivial, and I am sure you can achieve a similar behavior in Java. As long as you can serialize your object to a binary array.

This is a question that might help you, if you need to. And this also.

Community
  • 1
  • 1
astaykov
  • 30,768
  • 3
  • 70
  • 86
0

Here is the code that I utilize for serializing objects to strings and back for when dealing with Azure Queues. Just make sure that your objects are small, so that they fit within the 64k requirement

protected CloudQueueMessage CreateCloudQueueMessage(TQueueableEntity message)
{
    using (var stringWriter = new StringWriter())
    {
        var xmlSerializer = new XmlSerializer(typeof(TQueueableEntity));
        xmlSerializer.Serialize(stringWriter, message);

        return new CloudQueueMessage(stringWriter.ToString());
    }
}

protected TQueueableEntity CreateObjectFromMessage(CloudQueueMessage message)
{
    var xmlSerializer = new XmlSerializer(typeof(TQueueableEntity));

    //Deserialize message
    using (var reader = new StringReader(message.AsString))
    {
        var result = (TQueueableEntity)xmlSerializer.Deserialize(reader);
        reader.Close();
        return result;
    }
}
Igorek
  • 15,716
  • 3
  • 54
  • 92