0

I have an MSMQ which would be used almost as some form of "Schedule queue" for sending out tweets at specific time (Think of it as setting a time to send a specific tweet out).

I have sort of ran into a brick wall in how I would retrieve items from the queue. I am aware of the Peek() and Remove() methods, and they work, but they wouldn't remove the item which is next scheduled to be processed, they would simply only retrieve the the oldest item on the queue.

I have also had a look at using the TimeToBeReceived property, but it doesn't seem to make much difference.

If I was to add an message to the queue, with the TimeToBeReceived property of the next hour, I shouldn't expect to be able to retrieve it for the next hour, it the RetrieveNextItem() method I have written should either return nothing at all or the next item where the time has passed. Could anyone explain to me how I would accomplish this?

Please see my code below to get an general idea on what I already have.

public void AddItemToQueue(IScheduledTweet scheduledTweet)
{
    MessageQueue messageQueue = new MessageQueue(@".\Private$\TwitterBot");
    Message message = new Message();

    message.Body = scheduledTweet;

    message.TimeToBeReceived = scheduledTweet.Time.TimeOfDay;

    messageQueue.Send(scheduledTweet);
}

public IScheduledTweet RetrieveNextItem()
{
    IScheduledTweet returnValue;
    MessageQueue messageQueue;

    messageQueue = new MessageQueue(@".\Private$\TwitterBot");
    messageQueue.MessageReadPropertyFilter.TimeToBeReceived = true;
    messageQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(ScheduledTweet) });

    // I expect the message variable to be a message where its TimeToBeReceived property is either equal to the current time, or past the time.
    Message message = messageQueue.Peek();
    returnValue = (ScheduledTweet)message.Body;

    return returnValue;
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Sam Bunting
  • 845
  • 1
  • 15
  • 36
  • `TimeToBeReceived` doesn't do what you think it does. Read the MSDN documentation for it again. – Marcel N. Mar 15 '18 at 15:34
  • @MarcelN. Ah - I think your right. Do you have any idea how I would accomplish what I'm trying to do? – Sam Bunting Mar 15 '18 at 15:40
  • .. don't use MSMQ for what it's not supposed to do? – stuartd Mar 15 '18 at 15:47
  • @stuartd What would you recommend I do? – Sam Bunting Mar 15 '18 at 15:54
  • It is very unusual to want to do this. If you wait too long then you have a queue that grows without bounds. And you can't really guess at a "safe" delay since it entirely depends on what the other end is going. The odds that it delays *exactly* as long are zero, so you'll either wake up and find nothing or always fall behind. Use a Timer if you don't think it is a bad idea. – Hans Passant Mar 15 '18 at 16:43
  • If you want to do things on a schedule, why not use a scheduler? I've used [quartz.net](https://www.quartz-scheduler.net/) in the past it worked well. – stuartd Mar 15 '18 at 17:07

0 Answers0