Delay and Schedule Message Delivery with Apache ActiveMQ and Delphi

Apache ActiveMQ from version 5.4 has a persistent scheduler built into the ActiveMQ message broker. An ActiveMQ client can take advantage of a delayed delivery by using message properties.

By setting properties of the JMS message, a client can

  • set the time in milliseconds that a message will wait before being scheduled to be delivered by the broker
  • set the time in milliseconds to wait after the start time to wait before scheduling the message again
  • set the number of times to repeat scheduling a message for delivery
  • or use a Cron entry (for example “0 * * * *” to set the schedule

This new feature now is also available from Delphi, using Habari Client for ActiveMQ. Here is an example which shows how a message will be scheduled for delivery after 2000 milliseconds.

  Connection := TBTJMSConnection.MakeConnection;
  try
    try
    Connection.Start;
    Session := Connection.CreateSession(False, amAutoAcknowledge);

    // listen for messages
    ReplyQueue := Session.CreateQueue('Habari');
    Consumer := Session.CreateConsumer(ReplyQueue);

    // create the destination
    Destination := Session.CreateQueue('Habari');

    // create the message and set delay to 5 seconds
    JMSMessage := Session.CreateTextMessage('test msg');
    JMSMessage.SetIntProperty(AMQ_SCHEDULED_DELAY, 5000);
    Producer := Session.CreateProducer(Destination);

    Producer.Send(JMSMessage);
    Started := GetTickCount;

    WriteLn('Message has been sent to queue ' + Destination.QueueName);

    // wait for the delayed message
    JMSMessage := Consumer.Receive(6000) as ITextMessage;
    Elapsed := GetTickCount - Started;

    if Assigned(JMSMessage) then
    begin
      WriteLn(Format('Message text: %s (after %d msec)', [JMSMessage.Text,
        Elapsed]));
    end
    else
      WriteLn('Received no message on queue ' + ReplyQueue.QueueName);

    Connection.Stop;

    except
     on E:Exception do
       WriteLn(E.Message);
    end;
  finally
    Connection.Close;
  end;

Read more:

http://activemq.apache.org/delay-and-schedule-message-delivery.html

About Apache ActiveMQ

Apache ActiveMQ is the most popular and powerful open source Message Broker and Enterprise Integration Patterns provider. Apache ActiveMQ is fast, supports many Cross Language Clients and Protocols, comes with easy to use Enterprise Integration Patterns and many advanced features while fully supporting JMS 1.1 and J2EE 1.4. Apache ActiveMQ is released under the Apache 2.0 License.

Apache ActiveMQ is also a key component of the Apache Geronimo Web Application Server and IBM WebSphere Application Server Community Edition

Read more about Apache ActiveMQ here: http://activemq.apache.org/

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s